From c881adc484d41469cf15decb32169dec2f737563 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Fri, 25 Nov 2022 08:26:50 +0000 Subject: [PATCH] Add support for windows Signed-off-by: Paulo Gomes --- Makefile | 2 +- cgo/sha1.go | 19 +++++-------------- cgo/sha1_nix.go | 22 ++++++++++++++++++++++ cgo/sha1_windows.go | 22 ++++++++++++++++++++++ sha1cd_test.go | 4 ++-- test/collisiondetection_test.go | 2 +- testdata/files/valid-file.txt | 2 +- 7 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 cgo/sha1_nix.go create mode 100644 cgo/sha1_windows.go diff --git a/Makefile b/Makefile index 106d6a7..b551f15 100644 --- a/Makefile +++ b/Makefile @@ -10,4 +10,4 @@ bench: .PHONY: fuzz fuzz: - go test -fuzz=. -fuzztime=$(FUZZ_TIME) ./test/ + go test -tags gofuzz -fuzz=. -fuzztime=$(FUZZ_TIME) ./test/ diff --git a/cgo/sha1.go b/cgo/sha1.go index df3cb38..e51d060 100644 --- a/cgo/sha1.go +++ b/cgo/sha1.go @@ -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 { diff --git a/cgo/sha1_nix.go b/cgo/sha1_nix.go new file mode 100644 index 0000000..39e4239 --- /dev/null +++ b/cgo/sha1_nix.go @@ -0,0 +1,22 @@ +//go:build !windows +// +build !windows + +package cgo + +// #include +// #include +// #include +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 +} diff --git a/cgo/sha1_windows.go b/cgo/sha1_windows.go new file mode 100644 index 0000000..3743b96 --- /dev/null +++ b/cgo/sha1_windows.go @@ -0,0 +1,22 @@ +//go:build windows +// +build windows + +package cgo + +// #include +// #include +// #include +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 +} diff --git a/sha1cd_test.go b/sha1cd_test.go index 75e53de..4aea8c4 100644 --- a/sha1cd_test.go +++ b/sha1cd_test.go @@ -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) } } diff --git a/test/collisiondetection_test.go b/test/collisiondetection_test.go index 3efd09c..2ec8b96 100644 --- a/test/collisiondetection_test.go +++ b/test/collisiondetection_test.go @@ -57,7 +57,7 @@ func TestCollisionDetection(t *testing.T) { { name: "Valid File", inputFile: "../testdata/files/valid-file.txt", - wantHash: "3a97ef20e25305c580a172c7590d0753e51e72be", + wantHash: "2b915da50f163514d390c9d87a4f3e23eb663f8a", hashers: defaultHashers, }, } diff --git a/testdata/files/valid-file.txt b/testdata/files/valid-file.txt index 134a7fe..f2e88ce 100644 --- a/testdata/files/valid-file.txt +++ b/testdata/files/valid-file.txt @@ -1 +1 @@ -this is not a collision attack +this is not a collision attack