// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use crypto::mac;
use crypto::sha1;
use crypto::sha256;
use hash;
use strings;

fn assert_hmac_sha1(keystr: str, vectors: [](str, [20]u8)) void = {
	let key = strings::toutf8(keystr);
	let h = sha1::sha1();
	let buf: [sha1::BLOCKSZ]u8 = [0x1...];

	for (let i = 0z; i < len(vectors); i += 1) {
		hash::reset(&h);
		let hmac = hmac(&h, key, buf);
		defer mac::finish(&hmac);

		const vector = vectors[i];

		mac::write(&hmac, strings::toutf8(vector.0));

		let sum: [sha1::SZ]u8 = [0...];
		mac::sum(&hmac, sum);

		assert(bytes::equal(vector.1, sum));
	};
};

@test fn hmac_sha1_empty_key() void = {
	const vectors: [_](str, [20]u8) = [
		(
			"",
			[
				0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08,
				0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63,
				0x70, 0x69, 0x0e, 0x1d,
			]
		),
		(
			"abc",
			[
				0x9b, 0x4a, 0x91, 0x8f, 0x39, 0x8d, 0x74, 0xd3,
				0xe3, 0x67, 0x97, 0x0a, 0xba, 0x3c, 0xbe, 0x54,
				0xe4, 0xd2, 0xb5, 0xd9,
			]
		),
		(
			"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijkl"
			"mnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq"
			"rstu",
			[
				0xf4, 0x50, 0x13, 0xac, 0xc6, 0xa6, 0xa5, 0x3a,
				0x49, 0xbf, 0xc0, 0x7b, 0x1e, 0xd8, 0xf5, 0x39,
				0x5b, 0xf3, 0x9d, 0x96,
			]
		),
	];
	assert_hmac_sha1("", vectors);
};

@test fn hmac_sha1() void = {
	const vectors: [_](str, [20]u8) = [
		(
			"",
			[
				0xc5, 0x89, 0xa2, 0x7a, 0xb0, 0xaf, 0x9f, 0xe7,
				0xe0, 0x38, 0x68, 0x24, 0x73, 0x5d, 0x22, 0x0d,
				0x04, 0x1b, 0x70, 0xf0,
			]
		),
		(
			"abc",
			[
				0x85, 0x2d, 0x02, 0xc6, 0xfd, 0x3b, 0x51, 0xdf,
				0x64, 0x4a, 0xfb, 0x13, 0x81, 0x76, 0xb3, 0x07,
				0x61, 0xca, 0x95, 0x27,
			]
		),
		(
			"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijkl"
			"mnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq"
			"rstu",
			[
				0x41, 0x29, 0x43, 0xf0, 0x84, 0x0d, 0xc1, 0xa1,
				0x33, 0x8a, 0x16, 0xec, 0x3d, 0x73, 0x0b, 0x76,
				0x25, 0xac, 0xf0, 0xaa,
			]
		),
	];

	assert_hmac_sha1("trustno1", vectors);
};

@test fn hmac_sha1_large_key() void = {
	const vectors: [_](str, [20]u8) = [
		(
			"",
			[
				0x24, 0xe4, 0x38, 0xcb, 0x07, 0x54, 0xef, 0x48,
				0x97, 0x20, 0x05, 0x65, 0xd5, 0xf2, 0x7f, 0x6e,
				0x62, 0x77, 0xfc, 0x95
			],
		),
		(
			"abc",
			[
				0xe3, 0x1a, 0x77, 0xff, 0xf1, 0x4b, 0xbf, 0xba,
				0x5d, 0x2f, 0x8b, 0x44, 0xa6, 0x33, 0xd4, 0xc9,
				0xfc, 0x8a, 0xd0, 0xe9,
			]
		),
		(
			"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijkl"
			"mnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq"
			"rstu",
			[
				0x5a, 0x64, 0x69, 0x18, 0xa0, 0xe3, 0xaf, 0xe9,
				0xab, 0x45, 0x1d, 0xf3, 0xcd, 0xdd, 0x1e, 0x7e,
				0xb9, 0x19, 0xa7, 0x4b
			]
		),
	];

	let key = "xxabcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
		"jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
	assert_hmac_sha1(key, vectors);
};

@test fn sha256() void = {
	const expected: [_]u8 = [
		0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, 0x77, 0x2f,
		0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, 0xff, 0x16, 0x97, 0xc4,
		0x93, 0x71, 0x56, 0x53, 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92,
		0xc5, 0xad,
	];

	let hmac = sha256([]);
	defer mac::finish(&hmac);

	let sum: [sha256::SZ]u8 = [0...];
	mac::sum(&hmac, sum);

	assert(bytes::equal(expected, sum));
};
