From 261a94d80664247b8b55041205797b98182a7ce0 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Fri, 6 Jun 2014 14:34:42 -0700 Subject: [PATCH] add ECIES example --- examples/ECIES.js | 30 ++++++++++++++++++++++++++++++ test/test.examples.js | 1 + 2 files changed, 31 insertions(+) create mode 100644 examples/ECIES.js diff --git a/examples/ECIES.js b/examples/ECIES.js new file mode 100644 index 00000000000..9e00b9b8e13 --- /dev/null +++ b/examples/ECIES.js @@ -0,0 +1,30 @@ +var run = function() { + bitcore = typeof (bitcore) === 'undefined' ? require('../bitcore') : bitcore; + + console.log('ECIES: Elliptic Curve Integrated Encryption Scheme'); + console.log('A way of encrypting with a public key and decrypting with a private key.'); + + var key = bitcore.Key.generateSync(); + console.log('Private key: ' + key.private.toString('hex')); + console.log('Public key: ' + key.public.toString('hex')); + + var message = new Buffer('This is a message to be encrypted'); + console.log('Message: "' + message.toString() + '"'); + + var encrypted = bitcore.ECIES.encrypt(key.public, message); + console.log('Encrypted (with public key): ' + encrypted.toString('hex')); + + var decrypted = bitcore.ECIES.decrypt(key.private, encrypted); + console.log('Decrypted (with private key): "' + decrypted.toString() + '"'); +}; + + +// This is just for browser & mocha compatibility +if (typeof module !== 'undefined') { + module.exports.run = run; + if (require.main === module) { + run(); + } +} else { + run(); +} diff --git a/test/test.examples.js b/test/test.examples.js index 171515dde60..da84eecc5de 100644 --- a/test/test.examples.js +++ b/test/test.examples.js @@ -7,6 +7,7 @@ var unmute = require('./mute').unmute; var examples = [ 'Address', + 'ECIES', 'HierarchicalKey', 'PeerManager', 'Rpc',