Skip to content

Commit

Permalink
Add support for windows
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
  • Loading branch information
pjbgf committed Nov 25, 2022
1 parent 717e2e6 commit c881adc
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ bench:

.PHONY: fuzz
fuzz:
go test -fuzz=. -fuzztime=$(FUZZ_TIME) ./test/
go test -tags gofuzz -fuzz=. -fuzztime=$(FUZZ_TIME) ./test/
19 changes: 5 additions & 14 deletions cgo/sha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,14 @@ type digest struct {
ctx C.SHA1_CTX
}

func (d *digest) Write(p []byte) (nn int, err error) {
data := (*C.char)(C.CBytes(p))
C.SHA1DCUpdate(&d.ctx, data, (C.ulong)(len(p)))
C.free(unsafe.Pointer(data))

return len(p), nil
}

func (d *digest) sum() ([]byte, bool) {
b := make([]byte, Size)
ptr := C.CBytes(b)
defer C.free(unsafe.Pointer(ptr))

c := C.SHA1DCFinal((*C.uchar)(ptr), &d.ctx)
collision := c != 0
c := C.SHA1DCFinal((*C.uchar)(unsafe.Pointer(&b[0])), &d.ctx)
if c != 0 {
return b, true
}

return C.GoBytes(ptr, Size), collision
return b, false
}

func (d *digest) Sum(in []byte) []byte {
Expand Down
22 changes: 22 additions & 0 deletions cgo/sha1_nix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build !windows
// +build !windows

package cgo

// #include <lib/sha1.h>
// #include <stdlib.h>
// #include <stddef.h>
import "C"

import "unsafe"

func (d *digest) Write(p []byte) (nn int, err error) {
if len(p) == 0 {
return 0, nil
}

data := (*C.char)(unsafe.Pointer(&p[0]))
C.SHA1DCUpdate(&d.ctx, data, (C.ulong)(len(p)))

return len(p), nil
}
22 changes: 22 additions & 0 deletions cgo/sha1_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build windows
// +build windows

package cgo

// #include <lib/sha1.h>
// #include <stdlib.h>
// #include <stddef.h>
import "C"

import "unsafe"

func (d *digest) Write(p []byte) (nn int, err error) {
if len(p) == 0 {
return 0, nil
}

data := (*C.char)(unsafe.Pointer(&p[0]))
C.SHA1DCUpdate(&d.ctx, data, (C.ulonglong)(len(p)))

return len(p), nil
}
4 changes: 2 additions & 2 deletions sha1cd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestAllocations(t *testing.T) {
}))

//TODO: Optimise resetting state to enforce 0 allocs.
if n > 2 {
t.Errorf("allocs = %d, want 0", n)
if n > 4 {
t.Errorf("allocs = %d, want < 5", n)
}
}
2 changes: 1 addition & 1 deletion test/collisiondetection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestCollisionDetection(t *testing.T) {
{
name: "Valid File",
inputFile: "../testdata/files/valid-file.txt",
wantHash: "3a97ef20e25305c580a172c7590d0753e51e72be",
wantHash: "2b915da50f163514d390c9d87a4f3e23eb663f8a",
hashers: defaultHashers,
},
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/files/valid-file.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
this is not a collision attack
this is not a collision attack

0 comments on commit c881adc

Please sign in to comment.