-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.test.js
112 lines (98 loc) · 3.53 KB
/
hash.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) 2021-2023, Alden Torres
*
* Licensed under the terms of the MIT license.
* Copy of the license at https://opensource.org/licenses/MIT
*/
import {
str2bin,
bin2hex,
libecc_promise,
} from "./util.js";
import {
hash_sha256,
hash_sha512,
} from "./hash.js";
import assert from "assert";
// Test vectors
// https://www.di-mgt.com.au/sha_testvectors.html
async function hash_ecc_hash_sha256(s) {
await libecc_promise;
let input = str2bin(s);
let out = hash_sha256(input);
return bin2hex(out);
}
describe("ecc_hash_sha256", () => {
it("input abc", async () => {
const r = await hash_ecc_hash_sha256(
"abc"
);
assert.strictEqual(r,
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
});
it("input empty string", async () => {
const r = await hash_ecc_hash_sha256(
""
);
assert.strictEqual(r,
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
});
it("input abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", async () => {
const r = await hash_ecc_hash_sha256(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
);
assert.strictEqual(r,
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
);
});
it("input abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", async () => {
const r = await hash_ecc_hash_sha256(
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
);
assert.strictEqual(r,
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
);
});
});
async function hash_ecc_hash_sha512(s) {
await libecc_promise;
let input = str2bin(s);
let out = hash_sha512(input);
return bin2hex(out);
}
describe("ecc_hash_sha512", () => {
it("input abc", async () => {
const r = await hash_ecc_hash_sha512(
"abc"
);
assert.strictEqual(r,
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
);
});
it("input empty string", async () => {
const r = await hash_ecc_hash_sha512(
""
);
assert.strictEqual(r,
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
);
});
it("input abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", async () => {
const r = await hash_ecc_hash_sha512(
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
);
assert.strictEqual(r,
"204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445"
);
});
it("input abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", async () => {
const r = await hash_ecc_hash_sha512(
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
);
assert.strictEqual(r,
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
);
});
});