-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
32 lines (30 loc) · 1.05 KB
/
index.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
const crypto = require('crypto-browserify')
function encrypt (publicKey, privateKey, buf) {
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
const ecdhA = crypto.createECDH('secp256k1')
ecdhA.generateKeys('hex', 'compressed')
ecdhA.setPrivateKey(privateKey, 'hex')
const secret = ecdhA.computeSecret(publicKey, 'hex')
const iv = crypto.randomBytes(16)
const cipher = crypto.createCipheriv('aes-256-ctr', secret, iv)
const crypted = Buffer.concat([iv, cipher.update(buf), cipher.final()])
return crypted
}
function decrypt (publicKey, privateKey, crypted) {
if (!Buffer.isBuffer(crypted)) throw new Error(`Must be a buffer!`)
const ecdhB = crypto.createECDH('secp256k1')
ecdhB.generateKeys('hex')
ecdhB.setPrivateKey(privateKey, 'hex')
const secret = ecdhB.computeSecret(publicKey, 'hex')
const iv = crypted.slice(0, 16)
const decipher = crypto.createCipheriv('aes-256-ctr', secret, iv)
const buf = Buffer.concat([
decipher.update(crypted.slice(16)),
decipher.final()
])
return buf
}
module.exports = {
encrypt,
decrypt
}