-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.js
executable file
·98 lines (85 loc) · 3.11 KB
/
verify.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
/*
Promise<any> verify(AlgorithmIdentifier algorithm, Key key, CryptoOperationData signature, CryptoOperationData data);
*/
var algorithms = require('./algorithms');
var functions = require('./functions');
var aes = require('./DJCL/src/aes').aes;
var encoding = require('./DJCL/src/encoding').encoding;
var hashing = require('./DJCL/src/hashing').hashing;
var rsa = require('./DJCL/src/rsa').rsa;
var Q = require('q');
exports.verify = function(algorithm,key,signature,data){
var verifypromise = new Q.Promise(function(resolve,reject){
if (!algorithm){
reject('Algorithm not provided');
}
else if (!algorithm.name){
reject('Algorithm name not provided');
}
var algo = algorithm.name;
//If algorithm for sign is not in the suggested algorithms list,reject with DOMException Error
if (algorithms.verifyalgos.indexOf(algo)==-1){
//Not correct. Check how to reject a DOMException
reject("The algorithm is not supported");
}
if (!key.hasOwnProperty("usages")){
reject("usages is not provided in key");
}
else if(key.usages.indexOf("verify")==-1){
reject ("InvalidAccessError");
}
switch (algo){
case "HMAC":
//normalize the algorithm : means just check if the specified algorithm has all the specified
//attributes in it.
var hashalgo = key.algorithm.hash.name;
//Convert back the arrayBufferView into string
//hashing.HMAC(key,msg) => key and data are both ASCII strings.
//key.data will be CryptoOperationData i.e. ArrayBuffer
//data will be CryptoOperationData i.e. ArrayBuffer
data = functions.convertArrayBufferViewToPlainText(data);
var keydata = functions.convertArrayBufferViewToPlainText(key.data);
var result;
switch (hashalgo){
case "SHA-1":
hashing.hmac_hash = hashing.sha1;
result = hashing.HMAC(keydata,data);
break;
case "SHA-256":
hashing.hmac_hash = hashing.sha256;
result = hashing.HMAC(keydata,data);
break;
}
//result is in hex and signature in CryptoOperationData
//Convert result to string and signature to string
result = functions.convertHexToString(result);
signature = functions.convertArrayBufferViewToPlainText(signature);
console.log(result);
console.log(signature);
if (result == signature){
resolve(true);
}
else {
resolve(false);
}
break;
case "RSASSA-PKCS1-v1_5":
if (!key.hasOwnProperty("type")){
reject ("Type of key is not provided");
}
else if (key.type != "public"){
reject("InvalidAccessError");
}
else {
data = functions.convertArrayBufferViewToPlainText(data);
//var privateKey = functions.convertArrayBufferViewToPlainText(key.data);
var publicKey;
publicKey = functions.convertArrayBufferViewToPlainText(key.publicKey.data);
signature = functions.convertStringToHex(functions.convertArrayBufferViewToPlainText(signature));
var result = rsa.verify_pkcs1_v1_5(data,signature,publicKey);
resolve(result);
}
}
});
return verifypromise;
};