-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
55 lines (49 loc) · 1.44 KB
/
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
const hmac = require(".");
const crypto = require("crypto");
const key = Buffer.from("Hello");
const message = Buffer.from("World!");
process.stdout.write("Sync test...");
const toCheck = hmac.hmacSync(message, key, function(content){
const syncTest = crypto.createHash("sha256");
syncTest.update(content);
return syncTest.digest();
}, 64);
const hmac1 = crypto.createHmac("sha256", key);
hmac1.update(message);
const toCompare = hmac1.digest();
function compare(buffer1, buffer2){
const length = Math.max(buffer1.length, buffer2.length);
for(let i=0;i<length;i++){
const val1 = buffer1[i] || 0;
const val2 = buffer2[i] || 0;
if(val1 !== val2) return false;
}
return true;
}
if(compare(toCheck, toCompare)){
console.log(" Test passed!");
}else{
console.error(" Not passed!");
process.exit(1);
}
const hmac2 = crypto.createHmac("sha256", key);
hmac2.update(message);
const toCompare2 = hmac2.digest();
process.stdout.write("Async test...");
hmac.hmac(message, key, function(content){
return new Promise(r => {
const syncTest = crypto.createHash("sha256");
syncTest.update(content);
const digest = syncTest.digest();
setTimeout(() => {
r(digest);
}, 500);
});
}, 64).then(toCheck2 => {
if(compare(toCheck2, toCompare2)){
console.log(" Test passed!");
}else{
console.error(" Not passed!");
process.exit(1);
}
});