Skip to content

Instantly share code, notes, and snippets.

@jackcoble
Created August 31, 2020 18:00
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 jackcoble/0cf5ae7afb02c691cec0841a71a1d4f8 to your computer and use it in GitHub Desktop.
Save jackcoble/0cf5ae7afb02c691cec0841a71a1d4f8 to your computer and use it in GitHub Desktop.
Shared Secret Key with ECDH
const secp256k1 = require("secp256k1");
const shared1 = secp256k1.ecdh(
// Bob's public key
Uint8Array.from(
Buffer.from(
"0458f41cfe6cbf9d9cbea548debfec39d5c4a16e6728bc8b251560cc7fbc36b92c79627fd0147444d7bf5f421755f5d3e2b15b3982a735ab86edb40840065cbb1d",
"hex"
)
),
// Alice's private key
Uint8Array.from(
Buffer.from(
"45fa0a3ec999835f9a39a6913297823af60ba064412653d63df8dfd9e89445aa",
"hex"
)
)
);
const shared2 = secp256k1.ecdh(
// Alice's public key
Uint8Array.from(
Buffer.from(
"04525a4462fafd7f081b93b560e4ac2f3670952d3a81136c28bcb111362180b4f90bf000c71a04bd2874f5196c7bd81d468cfbbcf10cd9e895b74d06aab599b08b",
"hex"
)
),
// Bob's private key
Uint8Array.from(
Buffer.from(
"12dfdbb27aa0eb2b14e8e6daa29b359b02e9e376bd7064ea8f6765e331e63cfd",
"hex"
)
)
);
console.log("Alice Shared Secret:", toHexString(shared1));
console.log("Bob Shared Secret:", toHexString(shared2));
// Helper method
function toHexString(byteArray) {
return Array.prototype.map.call(byteArray, function (byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment