Skip to content

Instantly share code, notes, and snippets.

@perry-mitchell
Created November 12, 2016 16:19
Show Gist options
  • Select an option

  • Save perry-mitchell/0d4a1da2348eac2bbd9dcd5761c3877c to your computer and use it in GitHub Desktop.

Select an option

Save perry-mitchell/0d4a1da2348eac2bbd9dcd5761c3877c to your computer and use it in GitHub Desktop.

Revisions

  1. perry-mitchell created this gist Nov 12, 2016.
    43 changes: 43 additions & 0 deletions ecdh_iocane.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // To use this, you will need to `npm install iocane`!

    const crypto = require("crypto");

    let aliceECDH = crypto.createECDH("secp256k1");
    aliceECDH.generateKeys();

    let alicePublicKey = aliceECDH.getPublicKey(null, "compressed"),
    alicePrivateKey = aliceECDH.getPrivateKey(null, "compressed");

    console.log("Alice Public: ", alicePublicKey.length, alicePublicKey.toString("hex"));
    console.log("Alice Private:", alicePrivateKey.length, alicePrivateKey.toString("hex"));

    let bobECDH = crypto.createECDH("secp256k1");
    bobECDH.generateKeys();

    let bobPublicKey = bobECDH.getPublicKey(null, "compressed"),
    bobPrivateKey = bobECDH.getPrivateKey(null, "compressed");

    console.log("Bob Public: ", bobPublicKey.length, bobPublicKey.toString("hex"));
    console.log("Bob Private: ", bobPrivateKey.length, bobPrivateKey.toString("hex"));

    // On Alice's side
    let secret1 = aliceECDH.computeSecret(bobPublicKey);
    console.log("Alice Secret: ", secret1.length, secret1.toString("hex"));

    // On Bob's side
    let secret2 = bobECDH.computeSecret(alicePublicKey);
    console.log("Bob Secret: ", secret2.length, secret2.toString("hex"));

    const iocane = require("iocane").crypto;

    iocane
    .encryptWithPassword("Hi there, Bob!", secret1)
    .then(function(encrypted) {
    console.log("Encrypted:", encrypted);
    // send to Bob
    iocane
    .decryptWithPassword(encrypted, secret1)
    .then(function(message) {
    console.log("Decrypted:", message);
    });
    });