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