-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.js
58 lines (50 loc) · 1.72 KB
/
identity.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
var nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
var R = require('ramda')
const hex = a => {
return Buffer.from(nacl.util.encodeBase64(a)).toString('hex')
}
const unhex = a => {
return nacl.util.decodeBase64(Buffer.from(a, 'hex').toString())
}
var generateKeys = function (){
const keys = nacl.box.keyPair()
const publicKey = hex(keys.publicKey)
const secretKey = hex(keys.secretKey)
return {secretKey: secretKey, publicKey: publicKey}
}
var encrypt = function(message, identity, theirPublicKey) {
const secretKey = unhex(identity.secretKey)
const publicKey = unhex(theirPublicKey)
const msg = nacl.util.decodeUTF8(message)
const nonce = nacl.randomBytes(24)
const box = nacl.util.encodeBase64(nacl.box(msg, nonce, publicKey, secretKey))
return {box: box, nonce: nacl.util.encodeBase64(nonce), publicKey: identity.publicKey}
}
var decrypt = function(message, mySecretKey) {
console.log(`${message.box}, mySecret Key = ${mySecretKey}`)
const result = nacl.box.open(
nacl.util.decodeBase64(message.box),
nacl.util.decodeBase64(message.nonce),
unhex(message.publicKey),
unhex(mySecretKey)
)
if(!result) throw('Error decoding the message!')
return nacl.util.encodeUTF8(result)
}
const test = () => {
const id1 = generateKeys()
console.log(`id1 = ${JSON.stringify(id1)}`)
const id2 = generateKeys();
[id1, id2].map(x => console.log(`id = ${JSON.stringify(x)}`))
const message = 'hello world'
const encrypted = encrypt(message, id1, id2.publicKey)
console.log('encrypted = ', JSON.stringify(encrypted))
const unencrypted = decrypt(encrypted, id2.secretKey)
console.log(`unencrypted = ${unencrypted}`)
}
module.exports = {
generateKeys,
encrypt,
decrypt
}