| 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 sha256() void = { |
| 11 |
let sha = sha256(); |
| 12 |
const vectors = [ |
| 13 |
("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), |
| 14 |
("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"), |
| 15 |
("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"), |
| 16 |
("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"), |
| 17 |
("hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"), |
| 18 |
("Hare is a cool language", "3f6fe31611580448e33af475ce0e66c7d55a156c6ec43c794225cc3084e04635"), |
| 19 |
("'UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things' - Doug Gwyn", "5cfa9eccaafa0a7d9d965e36b0a54cc1dd97dd1dff7e11d5e631bdea7f2ef328"), |
| 20 |
("'Life is too short to run proprietary software' - Bdale Garbee", "79ecc26605c1fa5156821c5da9ebc959d8a46050ee49f47da57bf9391a558ceb"), |
| 21 |
("'The central enemy of reliability is complexity.' - Geer et al", "80b2fd9ae9e9c2ccd801c923f5e3684d56c6b05edc2eb480634b0af10f9c810b"), |
| 22 |
("'A language that doesn’t have everything is actually easier to program in than some that do.' - Dennis Ritchie", "10ebb04c1ddd55528d0c8db05a1f5fad6c04ebc20cfc4a53308f9a05a90cc438"), |
| 23 |
]; |
| 24 |
|
| 25 |
for (let i = 0z; i < len(vectors); i += 1) { |
| 26 |
const vector = vectors[i]; |
| 27 |
hash::reset(&sha); |
| 28 |
hash::write(&sha, strings::toutf8(vector.0)); |
| 29 |
|
| 30 |
let sum: [SZ]u8 = [0...]; |
| 31 |
hash::sum(&sha, sum); |
| 32 |
|
| 33 |
let shahex = hex::encodestr(sum)!; |
| 34 |
defer free(shahex); |
| 35 |
if (shahex != vector.1) { |
| 36 |
fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; |
| 37 |
abort(); |
| 38 |
}; |
| 39 |
}; |
| 40 |
}; |
| 41 |
|
| 42 |
@test fn sha256_1gb() void = { |
| 43 |
test::require("slow"); |
| 44 |
|
| 45 |
let sha = sha256(); |
| 46 |
const input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"; |
| 47 |
const expected = "50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e"; |
| 48 |
hash::reset(&sha); |
| 49 |
for (let i = 0z; i < 16777216; i += 1) { |
| 50 |
hash::write(&sha, strings::toutf8(input)); |
| 51 |
}; |
| 52 |
let sum: [SZ]u8 = [0...]; |
| 53 |
hash::sum(&sha, sum); |
| 54 |
|
| 55 |
let shahex = hex::encodestr(sum)!; |
| 56 |
if (shahex != expected) { |
| 57 |
fmt::errorfln("Biggo vector: {} != {}", shahex, expected)!; |
| 58 |
abort(); |
| 59 |
}; |
| 60 |
}; |