-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.js
executable file
·48 lines (41 loc) · 1.19 KB
/
digest.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
/*
Promise<any> digest(AlgorithmIdentifier algorithm, CryptoOperationData data);
*/
var digest = function(algorithm,data){
var digestpromise = new Promise(function(resolve,reject){
if (!algorithm){
reject("algorithm should not be null");
}
if (!algorithm.hasOwnProperty("name")){
reject ("Name of algorithm is not provided");
}
if (!data){
reject("Data should not be null");
}
if (typeof(data)!="object"){
reject("Data should be object");
}
var algo = algorithm.name;
//Checking if the algo name in present in the suggested algorithms
if (digestalgos.indexOf(algo)==-1){
//Not correct. Check how to reject a DOMException
reject("The algorithm is not supported");
}
var result;
data = convertArrayBufferViewToPlainText(data);
switch (algo){
case "SHA-1":
result = hashing.SHA1(data);
result = convertHexToString(result);
result = convertPlainTextToArrayBufferView(result);
break;
case "SHA-256":
result = hashing.SHA256(data);
result = convertHexToString(result);
result = convertPlainTextToArrayBufferView(result);
break;
}
resolve(result);
});
return digestpromise;
};