-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto.js
30 lines (29 loc) · 1.46 KB
/
crypto.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
// decryptMsg is called by the receiver to decrypt the message. It takes the encrypted message
// (msgObj.msg), the sender's public key (msgObj.id), and the unique nonce used to encrypt the
// message (msgObj.n). If this message is from ourselves (me), it will use the msgObj.for key to
// decrypt it.
//
// This function can only decrypt messages meant for us, or messages sent by us.
async function decryptMsg(msgObj) {
let other_pub = null;
if (msgObj.id != me) {
other_pub = bs58.decode(msgObj.id).subarray(6);
} else {
other_pub = bs58.decode(msgObj.for).subarray(6);
}
let secret = await nobleEd25519.getSharedSecret(_priv_key, other_pub);
let encryptedBytes = aesjs.utils.hex.toBytes(msgObj.msg);
let aesCtr = new aesjs.ModeOfOperation.ctr(secret, new aesjs.Counter(parseInt(msgObj.n)));
return aesjs.utils.utf8.fromBytes(aesCtr.decrypt(encryptedBytes));
}
// encryptMsg is called by the sender to encrypt a message. It takes the message (msg) and the
// receiver's public key (to). It returns an array containing the unique nonce and the encrypted
// message.
async function encryptMsg(msg, to) {
let other_pub = bs58.decode(to).subarray(6);
let secret = await nobleEd25519.getSharedSecret(_priv_key, other_pub);
let uniqueN = window.crypto.getRandomValues(new Uint16Array(1))[0];
let aesCtr = new aesjs.ModeOfOperation.ctr(secret, new aesjs.Counter(uniqueN));
let encryptedBytes = aesCtr.encrypt(aesjs.utils.utf8.toBytes(msg));
return [encryptedBytes, uniqueN];
}