-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.js
executable file
·76 lines (68 loc) · 2.3 KB
/
sign.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
/*
Promise<any> sign(AlgorithmIdentifier algorithm, Key key, CryptoOperationData data);
*/
var sign = function(algorithm,key,data){
var signpromise = new 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 (signalgos.indexOf(algo)==-1){
//Not correct. Check how to reject a DOMException
reject(new DOMException(DOMException.NOT_SUPPORTED_ERR,"The algorithm is not supported"));
}
if (!key.hasOwnProperty("usages")){
reject("usages is not provided in key");
}
else if(key.usages.indexOf("sign")==-1){
reject ("InvalidAccessError");
}
switch (algo){
case "HMAC":
//normalize the algorithm : means just check if the specified algorithm has all the specified
//attributes in it.
//TODO : Check all the key attributes
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 = convertArrayBufferViewToPlainText(data);
var keydata = 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 = convertHexToString(result);
result = convertPlainTextToArrayBufferView(result);
resolve(result);
break;
case "RSASSA-PKCS1-v1_5":
if (!key.hasOwnProperty("type")){
reject ("Type of key is not provided");
}
else if (key.type != "private"){
reject("InvalidAccessError");
}
else {
data = convertArrayBufferViewToPlainText(data);
var privateKey = convertArrayBufferViewToPlainText(key.data);
var result = rsa.sign_pkcs1_v1_5(data,privateKey);
resolve(result);
}
break;
}
});
return signpromise;
};