| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use encoding::hex; |
| 5 |
use fmt; |
| 6 |
use hash; |
| 7 |
use strings; |
| 8 |
use test; |
| 9 |
|
| 10 |
@test fn sha1() void = { |
| 11 |
let sha = sha1(); |
| 12 |
const vectors = [ |
| 13 |
("", "da39a3ee5e6b4b0d3255bfef95601890afd80709"), |
| 14 |
("abc", "a9993e364706816aba3e25717850c26c9cd0d89d"), |
| 15 |
("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"), |
| 16 |
("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"), |
| 17 |
("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "a49b2446a02c645bf419f995b67091253a04a259"), |
| 18 |
// From Pro Git Chapter 10.2 |
| 19 |
// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects#_object_storage |
| 20 |
// output of: echo -n 'what is up, doc?' | git hash-object --stdin |
| 21 |
("blob 16\0what is up, doc?", "bd9dbf5aae1a3862dd1526723246b20206e5fc37"), |
| 22 |
("Hare is a cool language", "947feae3d0d65cc083c8f3e87858206e36aae908"), |
| 23 |
("'UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things' - Doug Gwyn", "05c8dd2605161bdd0b5d70f1f225f4dd69a01e3b"), |
| 24 |
("'Life is too short to run proprietary software' - Bdale Garbee", "91ad4bdc2fbe2b731cbe8bf2958099391c7af3b8"), |
| 25 |
("'The central enemy of reliability is complexity.' - Geer et al", "4b6eb2aa55ef59cc59be6d181c64141e7c1e5eab"), |
| 26 |
]; |
| 27 |
|
| 28 |
for (let i = 0z; i < len(vectors); i += 1) { |
| 29 |
const vector = vectors[i]; |
| 30 |
hash::reset(&sha); |
| 31 |
hash::write(&sha, strings::toutf8(vector.0)); |
| 32 |
|
| 33 |
let sum: [SZ]u8 = [0...]; |
| 34 |
hash::sum(&sha, sum); |
| 35 |
|
| 36 |
let shahex = hex::encodestr(sum)!; |
| 37 |
defer free(shahex); |
| 38 |
if (shahex != vector.1) { |
| 39 |
fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; |
| 40 |
abort(); |
| 41 |
}; |
| 42 |
}; |
| 43 |
}; |
| 44 |
|
| 45 |
@test fn sha1_1gb() void = { |
| 46 |
test::require("slow"); |
| 47 |
|
| 48 |
const input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"; |
| 49 |
const expected = "7789f0c9ef7bfc40d93311143dfbe69e2017f592"; |
| 50 |
|
| 51 |
let sha = sha1(); |
| 52 |
|
| 53 |
for (let i = 0z; i < 16777216; i += 1) |
| 54 |
hash::write(&sha, strings::toutf8(input)); |
| 55 |
|
| 56 |
let sum: [SZ]u8 = [0...]; |
| 57 |
hash::sum(&sha, sum); |
| 58 |
|
| 59 |
let shahex = hex::encodestr(sum)!; |
| 60 |
|
| 61 |
if (shahex != expected) { |
| 62 |
fmt::errorfln("1GB vector: {} != {}", shahex, expected)!; |
| 63 |
abort(); |
| 64 |
}; |
| 65 |
}; |