Last active
March 24, 2018 02:57
-
-
Save tgoldenberg/9fca82fcacddf5030d8d3dd676e7bc80 to your computer and use it in GitHub Desktop.
Encrypt messages with ECIES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import decode from 'jwt-decode'; | |
import jwt from 'njwt'; | |
import { ec } from 'elliptic'; | |
var eccrypto = require('eccrypto'); | |
var crypto = require('crypto'); | |
// sender | |
var privKey1 = crypto.randomBytes(32); | |
var pubKey1 = eccrypto.getPublic(privKey1); | |
// recipient | |
var privKey2 = crypto.randomBytes(32); | |
var pubKey2 = eccrypto.getPublic(privKey2); | |
// create web token of JSON content | |
var message = "Hello world"; | |
var token = njwt.create({ message }, 'HS512').compact(); | |
// encrypto token and send to recipient | |
eccrypto.encrypt(pubKey2, Buffer(token)).then(function(encrypted) { | |
console.log('> Encrypted message: ', encrypted); | |
// decrypt from recipient end | |
eccrypto.decrypt(privKey2, encrypted).then(function(content) { | |
console.log('> Decrypted message: ', content); | |
// decode web token | |
var decoded = decode(content); | |
console.log('> Decoded token: ', decoded); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment