| 1 |
package hash |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/sha1" |
| 6 |
"crypto/sha512" |
| 7 |
"encoding/hex" |
| 8 |
"hash" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestRegisterHash(t *testing.T) { |
| 14 |
// Reset default hash to avoid side effects. |
| 15 |
defer reset() |
| 16 |
|
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
hash crypto.Hash |
| 20 |
new func() hash.Hash |
| 21 |
wantErr string |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "sha1", |
| 25 |
hash: crypto.SHA1, |
| 26 |
new: sha1.New, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "sha1", |
| 30 |
hash: crypto.SHA1, |
| 31 |
wantErr: "cannot register hash: f is nil", |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "sha512", |
| 35 |
hash: crypto.SHA512, |
| 36 |
new: sha512.New, |
| 37 |
wantErr: "unsupported hash function", |
| 38 |
}, |
| 39 |
} |
| 40 |
|
| 41 |
for _, tt := range tests { |
| 42 |
t.Run(tt.name, func(t *testing.T) { |
| 43 |
err := RegisterHash(tt.hash, tt.new) |
| 44 |
if tt.wantErr == "" && err != nil { |
| 45 |
t.Errorf("unexpected error: %v", err) |
| 46 |
} else if tt.wantErr != "" && err == nil { |
| 47 |
t.Errorf("expected error: %v got: nil", tt.wantErr) |
| 48 |
} else if err != nil && !strings.Contains(err.Error(), tt.wantErr) { |
| 49 |
t.Errorf("expected error: %v got: %v", tt.wantErr, err) |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Verifies that the SHA1 implementation used is collision-resistant |
| 56 |
// by default. |
| 57 |
func TestSha1Collision(t *testing.T) { |
| 58 |
defer reset() |
| 59 |
|
| 60 |
tests := []struct { |
| 61 |
name string |
| 62 |
content string |
| 63 |
hash string |
| 64 |
before func() |
| 65 |
}{ |
| 66 |
{ |
| 67 |
name: "sha-mbles-1: with collision detection", |
| 68 |
content: "99040d047fe81780012000ff4b65792069732070617274206f66206120636f6c6c6973696f6e212049742773206120747261702179c61af0afcc054515d9274e7307624b1dc7fb23988bb8de8b575dba7b9eab31c1674b6d974378a827732ff5851c76a2e60772b5a47ce1eac40bb993c12d8c70e24a4f8d5fcdedc1b32c9cf19e31af2429759d42e4dfdb31719f587623ee552939b6dcdc459fca53553b70f87ede30a247ea3af6c759a2f20b320d760db64ff479084fd3ccb3cdd48362d96a9c430617caff6c36c637e53fde28417f626fec54ed7943a46e5f5730f2bb38fb1df6e0090010d00e24ad78bf92641993608e8d158a789f34c46fe1e6027f35a4cbfb827076c50eca0e8b7cca69bb2c2b790259f9bf9570dd8d4437a3115faff7c3cac09ad25266055c27104755178eaeff825a2caa2acfb5de64ce7641dc59a541a9fc9c756756e2e23dc713c8c24c9790aa6b0e38a7f55f14452a1ca2850ddd9562fd9a18ad42496aa97008f74672f68ef461eb88b09933d626b4f918749cc027fddd6c425fc4216835d0134d15285bab2cb784a4f7cbb4fb514d4bf0f6237cf00a9e9f132b9a066e6fd17f6c42987478586ff651af96747fb426b9872b9a88e4063f59bb334cc00650f83a80c42751b71974d300fc2819a2e8f1e32c1b51cb18e6bfc4db9baef675d4aaf5b1574a047f8f6dd2ec153a93412293974d928f88ced9363cfef97ce2e742bf34c96b8ef3875676fea5cca8e5f7dea0bab2413d4de00ee71ee01f162bdb6d1eafd925e6aebaae6a354ef17cf205a404fbdb12fc454d41fdd95cf2459664a2ad032d1da60a73264075d7f1e0d6c1403ae7a0d861df3fe5707188dd5e07d1589b9f8b6630553f8fc352b3e0c27da80bddba4c64020d", |
| 69 |
hash: "4f3d9be4a472c4dae83c6314aa6c36a064c1fd14", |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "sha-mbles-1: with default SHA1", |
| 73 |
content: "99040d047fe81780012000ff4b65792069732070617274206f66206120636f6c6c6973696f6e212049742773206120747261702179c61af0afcc054515d9274e7307624b1dc7fb23988bb8de8b575dba7b9eab31c1674b6d974378a827732ff5851c76a2e60772b5a47ce1eac40bb993c12d8c70e24a4f8d5fcdedc1b32c9cf19e31af2429759d42e4dfdb31719f587623ee552939b6dcdc459fca53553b70f87ede30a247ea3af6c759a2f20b320d760db64ff479084fd3ccb3cdd48362d96a9c430617caff6c36c637e53fde28417f626fec54ed7943a46e5f5730f2bb38fb1df6e0090010d00e24ad78bf92641993608e8d158a789f34c46fe1e6027f35a4cbfb827076c50eca0e8b7cca69bb2c2b790259f9bf9570dd8d4437a3115faff7c3cac09ad25266055c27104755178eaeff825a2caa2acfb5de64ce7641dc59a541a9fc9c756756e2e23dc713c8c24c9790aa6b0e38a7f55f14452a1ca2850ddd9562fd9a18ad42496aa97008f74672f68ef461eb88b09933d626b4f918749cc027fddd6c425fc4216835d0134d15285bab2cb784a4f7cbb4fb514d4bf0f6237cf00a9e9f132b9a066e6fd17f6c42987478586ff651af96747fb426b9872b9a88e4063f59bb334cc00650f83a80c42751b71974d300fc2819a2e8f1e32c1b51cb18e6bfc4db9baef675d4aaf5b1574a047f8f6dd2ec153a93412293974d928f88ced9363cfef97ce2e742bf34c96b8ef3875676fea5cca8e5f7dea0bab2413d4de00ee71ee01f162bdb6d1eafd925e6aebaae6a354ef17cf205a404fbdb12fc454d41fdd95cf2459664a2ad032d1da60a73264075d7f1e0d6c1403ae7a0d861df3fe5707188dd5e07d1589b9f8b6630553f8fc352b3e0c27da80bddba4c64020d", |
| 74 |
hash: "8ac60ba76f1999a1ab70223f225aefdc78d4ddc0", |
| 75 |
before: func() { |
| 76 |
RegisterHash(crypto.SHA1, sha1.New) |
| 77 |
}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for _, tt := range tests { |
| 82 |
t.Run(tt.name, func(t *testing.T) { |
| 83 |
if tt.before != nil { |
| 84 |
tt.before() |
| 85 |
} |
| 86 |
|
| 87 |
h := New(crypto.SHA1) |
| 88 |
data, err := hex.DecodeString(tt.content) |
| 89 |
if err != nil { |
| 90 |
t.Fatal(err) |
| 91 |
} |
| 92 |
|
| 93 |
h.Reset() |
| 94 |
h.Write(data) |
| 95 |
sum := h.Sum(nil) |
| 96 |
got := hex.EncodeToString(sum) |
| 97 |
|
| 98 |
if tt.hash != got { |
| 99 |
t.Errorf("\n got: %q\nwanted: %q", got, tt.hash) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
} |