-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
107 lines (89 loc) · 3.09 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>DeriveKey</title>
<script>
var result = {};
function deriveAKey(password, hash, iterations, salt) {
password = stringToArrayBuffer(password);
salt = stringToArrayBuffer(salt || "salt");
hash = hash || "SHA-256";
iterations = iterations || 10000;
var t = new Date().getTime();
console.log('PASS:', arrayBufferToHexString(password));
console.log('SALT:', arrayBufferToHexString(salt));
console.log('HASH:', hash);
console.log('ITER:', iterations);
// First, create a PBKDF2 "key" containing the password
window.crypto.subtle.importKey(
"raw",
password,
{"name": "PBKDF2"},
false,
["deriveBits"]).
// Derive a key from the password
then(function(baseKey){
result.baseKey = baseKey;
console.log("BASE KEY", baseKey);
return window.crypto.subtle.deriveBits({
"name": "PBKDF2",
"salt": salt,
"iterations": iterations,
"hash": hash
},
baseKey,
160
);
// return window.crypto.subtle.deriveKey({
// "name": "PBKDF2",
// "salt": stringToArrayBuffer(salt),
// "iterations": iterations,
// "hash": hash
// },
// baseKey,
// {"name": "AES-CBC", "length": 128}, // Key we want
// true, // Extrable
// ["encrypt", "decrypt"] // For new key
// );
}).
// Export it so we can display it
// then(function(aesKey) {
// result.aesKey = aesKey;
// console.log("AES KEY", aesKey);
// return window.crypto.subtle.exportKey("raw", aesKey);
// }).
// Display it in hex format
then(function(keyBytes) {
result.keyBytes = new Uint8Array(keyBytes);
console.log('DERIVED', arrayBufferToHexString(keyBytes));
t = new Date().getTime() - t;
console.log("DONE IN", t);
}).
catch(function(err) {
result.err = err;
console.log("Key derivation failed", err);
t = new Date().getTime() - t;
console.log("ERR IN", t);
});
}
// Utility functions
function stringToArrayBuffer(string) {
var encoder = new TextEncoder("utf-8");
return encoder.encode(string);
}
function arrayBufferToHexString(arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
var hexString = "";
var nextHexByte;
for (var i=0; i<byteArray.byteLength; i++) {
nextHexByte = byteArray[i].toString(16); // Integer to base 16
if (nextHexByte.length < 2) {
nextHexByte = "0" + nextHexByte; // Otherwise 10 becomes just a instead of 0a
}
hexString += nextHexByte;
}
return hexString;
}
</script>
</head>
</html>