-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnostr-tools-methods.js
63 lines (49 loc) · 1.77 KB
/
nostr-tools-methods.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
import {Base64} from "./lib/js-base64/base64.mjs";
function hexToBytes(hex) {
let bytes = [];
for (let c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return new Uint8Array(bytes);
}
async function encryptNostr(sharedSecretHex, message) {
try {
const sharedSecret = hexToBytes(sharedSecretHex);
const encoder = new TextEncoder();
const data = encoder.encode(message);
const iv = crypto.getRandomValues(new Uint8Array(16)); // Initialization vector for AES-CBC
const key = await crypto.subtle.importKey('raw', sharedSecret, 'AES-CBC', false, ['encrypt']);
const encrypted = await crypto.subtle.encrypt({
name: 'AES-CBC',
iv: iv
}, key, data);
const encryptedBase64 = Base64.fromUint8Array(new Uint8Array(encrypted));
const ivBase64 = Base64.fromUint8Array(iv);
return `${encryptedBase64}?iv=${ivBase64}`;
} catch (e) {
console.log(e)
throw e;
}
}
async function decryptNostr(sharedSecretHex, encryptedMessage) {
try {
const sharedSecret = hexToBytes(sharedSecretHex);
let [ctb64, ivb64] = encryptedMessage.split("?iv=")
const iv = Base64.toUint8Array(ivb64);
const ciphertext = Base64.toUint8Array(ctb64);
const key = await crypto.subtle.importKey('raw', sharedSecret, 'AES-CBC', false, ['decrypt']);
const decrypted = await crypto.subtle.decrypt({
name: 'AES-CBC',
iv: iv
}, key, ciphertext);
const decoder = new TextDecoder();
return decoder.decode(decrypted);
}
catch (e)
{
console.log(e)
throw e;
}
}
window.encryptNostr = encryptNostr;
window.decryptNostr = decryptNostr;