hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+test.ha (4444B)


      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 
      9 @test fn sha512() void = {
     10 	let sha = sha512();
     11 	const vectors = [
     12 		("abc", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"),
     13 		("", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"),
     14 		("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445"),
     15 		("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"),
     16 		("'UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things' - Doug Gwyn",
     17 			"f3d99b807bd734bcec25d64109ff5d78405d5b76a34332270c7062a4c9e6d1711ee6ed230e688fef257cf19e94e0163671c99f4c785d50e8adddef9f37060023"),
     18 		("'Life is too short to run proprietary software' - Bdale Garbee", "b1eeae774232d4ba74410e7d951fc3e7de165dbc9498babb243e4a87983595a79f5970ed67ebf2e275059928b4f0309926f85dad45126875530e8d2350a93a2b"),
     19 		("'The central enemy of reliability is complexity.' - Geer et al", "7eee0bc24a069679eb272aeb698136f8e3bca79b4220c70bf091607b78358fe4b3621a9e28295d73157806a20470a17cc21b9a3eefa7408c6cf1f288d1403a0d"),
     20 	];
     21 
     22 	for (let i = 0z; i < len(vectors); i += 1) {
     23 		const vector = vectors[i];
     24 		hash::reset(&sha);
     25 		hash::write(&sha, strings::toutf8(vector.0));
     26 
     27 		let sum: [SZ]u8 = [0...];
     28 		hash::sum(&sha, sum);
     29 
     30 		let shahex = hex::encodestr(sum);
     31 		defer free(shahex);
     32 		if (shahex != vector.1) {
     33 			fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!;
     34 			abort();
     35 		};
     36 	};
     37 };
     38 
     39 @test fn sha512_224() void = {
     40 	let sha = sha512_224();
     41 	const vectors = [
     42 		("", "6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4"),
     43 		("abc", "4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa"),
     44 		("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "23fec5bb94d60b23308192640b0c453335d664734fe40e7268674af9")
     45 	];
     46 
     47 	for (let i = 0z; i < len(vectors); i += 1) {
     48 		const vector = vectors[i];
     49 		hash::reset(&sha);
     50 		hash::write(&sha, strings::toutf8(vector.0));
     51 
     52 		let sum: [SZ224]u8 = [0...];
     53 		hash::sum(&sha, sum);
     54 
     55 		let shahex = hex::encodestr(sum);
     56 		defer free(shahex);
     57 		if (shahex != vector.1) {
     58 			fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!;
     59 			abort();
     60 		};
     61 	};
     62 };
     63 
     64 @test fn sha512_256() void = {
     65 	let sha = sha512_256();
     66 	const vectors = [
     67 		("", "c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a"),
     68 		("abc", "53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23"),
     69 		("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a"),
     70 	];
     71 
     72 	for (let i = 0z; i < len(vectors); i += 1) {
     73 		const vector = vectors[i];
     74 		hash::reset(&sha);
     75 		hash::write(&sha, strings::toutf8(vector.0));
     76 
     77 		let sum: [SZ256]u8 = [0...];
     78 		hash::sum(&sha, sum);
     79 
     80 		let shahex = hex::encodestr(sum);
     81 		defer free(shahex);
     82 		if (shahex != vector.1) {
     83 			fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!;
     84 			abort();
     85 		};
     86 	};
     87 
     88 };
     89 
     90 @test fn sha384() void = {
     91 	let sha = sha384();
     92 	const vectors = [
     93 		("", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"),
     94 		("abc", "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"),
     95 		("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039"),
     96 	];
     97 
     98 	for (let i = 0z; i < len(vectors); i += 1) {
     99 		const vector = vectors[i];
    100 		hash::reset(&sha);
    101 		hash::write(&sha, strings::toutf8(vector.0));
    102 
    103 		let sum: [SZ384]u8 = [0...];
    104 		hash::sum(&sha, sum);
    105 
    106 		let shahex = hex::encodestr(sum);
    107 		defer free(shahex);
    108 		if (shahex != vector.1) {
    109 			fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!;
    110 			abort();
    111 		};
    112 	};
    113 };