Skip to content

Commit c881adc

Browse files
committed
Add support for windows
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 717e2e6 commit c881adc

File tree

7 files changed

+54
-19
lines changed

7 files changed

+54
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ bench:
1010

1111
.PHONY: fuzz
1212
fuzz:
13-
go test -fuzz=. -fuzztime=$(FUZZ_TIME) ./test/
13+
go test -tags gofuzz -fuzz=. -fuzztime=$(FUZZ_TIME) ./test/

cgo/sha1.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,14 @@ type digest struct {
3131
ctx C.SHA1_CTX
3232
}
3333

34-
func (d *digest) Write(p []byte) (nn int, err error) {
35-
data := (*C.char)(C.CBytes(p))
36-
C.SHA1DCUpdate(&d.ctx, data, (C.ulong)(len(p)))
37-
C.free(unsafe.Pointer(data))
38-
39-
return len(p), nil
40-
}
41-
4234
func (d *digest) sum() ([]byte, bool) {
4335
b := make([]byte, Size)
44-
ptr := C.CBytes(b)
45-
defer C.free(unsafe.Pointer(ptr))
46-
47-
c := C.SHA1DCFinal((*C.uchar)(ptr), &d.ctx)
48-
collision := c != 0
36+
c := C.SHA1DCFinal((*C.uchar)(unsafe.Pointer(&b[0])), &d.ctx)
37+
if c != 0 {
38+
return b, true
39+
}
4940

50-
return C.GoBytes(ptr, Size), collision
41+
return b, false
5142
}
5243

5344
func (d *digest) Sum(in []byte) []byte {

cgo/sha1_nix.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package cgo
5+
6+
// #include <lib/sha1.h>
7+
// #include <stdlib.h>
8+
// #include <stddef.h>
9+
import "C"
10+
11+
import "unsafe"
12+
13+
func (d *digest) Write(p []byte) (nn int, err error) {
14+
if len(p) == 0 {
15+
return 0, nil
16+
}
17+
18+
data := (*C.char)(unsafe.Pointer(&p[0]))
19+
C.SHA1DCUpdate(&d.ctx, data, (C.ulong)(len(p)))
20+
21+
return len(p), nil
22+
}

cgo/sha1_windows.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package cgo
5+
6+
// #include <lib/sha1.h>
7+
// #include <stdlib.h>
8+
// #include <stddef.h>
9+
import "C"
10+
11+
import "unsafe"
12+
13+
func (d *digest) Write(p []byte) (nn int, err error) {
14+
if len(p) == 0 {
15+
return 0, nil
16+
}
17+
18+
data := (*C.char)(unsafe.Pointer(&p[0]))
19+
C.SHA1DCUpdate(&d.ctx, data, (C.ulonglong)(len(p)))
20+
21+
return len(p), nil
22+
}

sha1cd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func TestAllocations(t *testing.T) {
203203
}))
204204

205205
//TODO: Optimise resetting state to enforce 0 allocs.
206-
if n > 2 {
207-
t.Errorf("allocs = %d, want 0", n)
206+
if n > 4 {
207+
t.Errorf("allocs = %d, want < 5", n)
208208
}
209209
}

test/collisiondetection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestCollisionDetection(t *testing.T) {
5757
{
5858
name: "Valid File",
5959
inputFile: "../testdata/files/valid-file.txt",
60-
wantHash: "3a97ef20e25305c580a172c7590d0753e51e72be",
60+
wantHash: "2b915da50f163514d390c9d87a4f3e23eb663f8a",
6161
hashers: defaultHashers,
6262
},
6363
}

testdata/files/valid-file.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
this is not a collision attack
1+
this is not a collision attack

0 commit comments

Comments
 (0)