Skip to content

Instantly share code, notes, and snippets.

/start.md Secret

Created November 10, 2015 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4ce25876e51fae678cc3 to your computer and use it in GitHub Desktop.
Save anonymous/4ce25876e51fae678cc3 to your computer and use it in GitHub Desktop.

Setup

npm install -g coffee-script
npm install bitcore-lib bitcore-ecies

Decode

coffee test.coffee decode 7jDFoiJz...(base58 message)

Encode

coffee test.coffee encode "Hello, world!" KzEad1...(private key)
#!coffee
vm = require 'vm'
{ PrivateKey, encoding } = require 'bitcore-lib'
ECIES = require 'bitcore-ecies'
[_, __, method, input, privateKey] = process.argv
if not input
throw 'input is empty!'
switch method
when 'decode'
decoded = encoding.Base58.decode input
sandbox = vm.createContext {
encryptMethod: ''
decryptKey: ''
encryptedMsg: ''
}
vm.runInContext decoded, sandbox
if sandbox.encryptMethod isnt 'ecies'
throw 'unsupported encrypt method!'
key = new PrivateKey(sandbox.decryptKey or privateKey)
msg = new Buffer sandbox.encryptedMsg, 'base64'
decrypted = ECIES().privateKey(key).publicKey(key.publicKey).decrypt(msg)
console.log decrypted.toString 'utf-8'
when 'encode'
if not privateKey
throw 'private key is required!'
buffer = new Buffer input, 'utf-8'
key = new PrivateKey privateKey
encrypted = ECIES().privateKey(key).publicKey(key.publicKey).encrypt(buffer)
struct = [
"encryptMethod='ecies';"
"decryptKey='#{key.toWIF()}';"
"encryptedMsg='#{encrypted.toString('base64')}';"
].join ''
output = encoding.Base58.encode new Buffer(struct)
console.log output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment