forked from mpgn/discord-e2e-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-key.js
40 lines (37 loc) · 1016 Bytes
/
generate-key.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
/*
This script should be only run into the dev console
More information: https://github.com/mpgn/discord-e2e-encryption
*/
async function generateKey(passphrase, salt){
// Import password as CryptoKey object
let enc = new TextEncoder()
let masterkey = await window.crypto.subtle.importKey(
"raw",
enc.encode(passphrase),
{name: "PBKDF2"},
false,
["deriveBits", "deriveKey"]
)
return window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
salt: enc.encode(salt),
"iterations": 100000,
"hash": "SHA-256"
},
masterkey,
{ "name": "AES-GCM", "length": 256},
false,
[ "encrypt", "decrypt" ]
);
}
async function exportKey(key) {
return window.crypto.subtle.exportKey(
"jwk", //can be "jwk" or "raw"
key //extractable must be true
)
}
var key = await generateKey("your password", "your salt")
exportKey(key).then(function (result) {
console.log(result)
})