-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_v2.ts
35 lines (24 loc) · 1.42 KB
/
encrypt_v2.ts
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
// use the Millr noble-secp256k1
import * as secp from '@noble/secp256k1';
import * as hkdf from "futoin-hkdf"
// File aes.ts is a complete copy of micro-aes-gcm/index.ts file, as I cannot make it work with the normal import
import * as aes from './aes';
import { convertMnemonicToKeyPair } from './mnemonics_to_keys'
import { alice_mnemonic, bob_mnemonic } from './mnemonics'
const runEncryptv2 = async(): Promise<void> => {
const alice = await convertMnemonicToKeyPair(alice_mnemonic)
const bob = await convertMnemonicToKeyPair(bob_mnemonic)
const data = Buffer.from('this is a test')
console.log(bob.pubkey)
// Alice wants to send an encrypted message to Bob, need her own Private key and Bob's Public key
const sharedKeyForAlice = secp.getSharedSecret(alice.privkey, bob.pubkey, true)
const derivedSharedKeyForAlice = hkdf.default(Buffer.from(sharedKeyForAlice), 32, { hash: 'SHA-256' })
// Uses a random IV (12 bits)
const encryptedData = await aes.encrypt(derivedSharedKeyForAlice, data)
// Bob wants to decrypt, need his own Private key and Alice's Public key
const sharedKeyForBob = secp.getSharedSecret(bob.privkey, alice.pubkey, true)
const derivedSharedKeyForBob = hkdf.default(Buffer.from(sharedKeyForBob), 32, { hash: 'SHA-256' })
const decryptedData = await aes.decrypt(derivedSharedKeyForBob, encryptedData)
console.log(Buffer.from(decryptedData).toString())
}
runEncryptv2()