File tree Expand file tree Collapse file tree 7 files changed +54
-19
lines changed Expand file tree Collapse file tree 7 files changed +54
-19
lines changed Original file line number Diff line number Diff line change 1010
1111.PHONY : fuzz
1212fuzz :
13- go test -fuzz=. -fuzztime=$(FUZZ_TIME ) ./test/
13+ go test -tags gofuzz - fuzz=. -fuzztime=$(FUZZ_TIME ) ./test/
Original file line number Diff line number Diff 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-
4234func (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
5344func (d * digest ) Sum (in []byte ) []byte {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 1- this is not a collision attack
1+ this is not a collision attack
You can’t perform that action at this time.
0 commit comments