Skip to content

Instantly share code, notes, and snippets.

@levex

levex/test.mjs Secret

Last active May 10, 2023 13:01
Show Gist options
  • Save levex/6a22f20eea40e0eb337a02ba906f79d8 to your computer and use it in GitHub Desktop.
Save levex/6a22f20eea40e0eb337a02ba906f79d8 to your computer and use it in GitHub Desktop.
deno crypto ECDH tester
const {
createECDH,
getCurves,
} = await import('node:crypto');
let isThisDeno = typeof Deno !== "undefined";
let curves = getCurves();
for (let curve of curves) {
console.log(curve);
console.log("=========");
console.log("");
// Generate Alice's keys...
let alice = createECDH(curve);
let aliceKey = alice.generateKeys();
console.log("aliceKey ", aliceKey.toString('hex'));
// Generate Bob's keys...
let bob = createECDH(curve);
let bobKey = bob.generateKeys();
console.log("bobKey ", bobKey.toString('hex'));
// Exchange and generate the secret...
let aliceSecret = alice.computeSecret(bobKey);
let bobSecret = bob.computeSecret(aliceKey);
// OK
console.log('aliceECDHSecret ', aliceSecret.toString('hex'));
console.log('bobECDHSecret ', bobSecret.toString('hex'));
console.log('publicKeySize ', aliceKey.length);
if (!isThisDeno)
console.log('privateKeySize ', alice.getPrivateKey().length);
console.log('sharedSecretSize ', aliceSecret.length);
console.log("");
}
@kt3k
Copy link

kt3k commented Apr 26, 2023

nit:

-let isThisDeno = typeof Deno !== undefined;
+let isThisDeno = typeof Deno !== "undefined";

@levex
Copy link
Author

levex commented May 10, 2023

Thanks @kt3k !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment