Skip to content

Instantly share code, notes, and snippets.

@asanso
Last active January 6, 2020 15:06
Show Gist options
  • Save asanso/5c7ab0cf961a571fe7e38b4416ce312d to your computer and use it in GitHub Desktop.
Save asanso/5c7ab0cf961a571fe7e38b4416ce312d to your computer and use it in GitHub Desktop.
/*
* Arguments: None
*
* Returns: Promise resolving to:
* {
* privateKey: CryptoKey,
* publicKey: CryptoKey,
* }
*/
async function newKeyPair() {
const kp = await window.crypto.subtle.generateKey(
{
name: "DH",
//NOTE: THIS IS A SMALL PRIME FOR TESTING ONLY! DO NOT USE IT FOR REAL!
//See http://datatracker.ietf.org/doc/rfc3526/ for better primes
prime: new Uint8Array([
255,255,255,255,255,255,255,255,201,15,218,162,33,104,194,52,196,198,98,139,
128,220,28,209,41,2,78,8,138,103,204,116,2,11,190,166,59,19,155,34,81,74,8,
121,142,52,4,221,239,149,25,179,205,58,67,27,48,43,10,109,242,95,20,55,79,225,
53,109,109,81,194,69,228,133,181,118,98,94,126,198,244,76,66,233,166,55,237,
107,11,255,92,182,244,6,183,237,238,56,107,251,90,137,159,165,174,159,36,17,
124,75,31,230,73,40,102,81,236,228,91,61,194,0,124,184,161,99,191,5,152,218,
72,54,28,85,211,154,105,22,63,168,253,36,207,95,131,101,93,35,220,163,173,
150,28,98,243,86,32,133,82,187,158,213,41,7,112,150,150,109,103,12,53,78,74,
188,152,4,241,116,108,8,202,35,115,39,255,255,255,255,255,255,255,255
]),
generator: new Uint8Array([2]),
},
false, //whether the key is extractable (i.e. can be used in exportKey)
["deriveKey", "deriveBits"] //can be any combination of "deriveKey" and "deriveBits"
);
return {
privateKey: kp.privateKey,
publicKey: kp.publicKey
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment