ECDH
January 24, 2019 ยท View on GitHub
Operations
Generate key
const keys = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256", // P-256, P-384, or P-521
},
false,
["deriveKey", "deriveBits"],
);
Import key
const publicKey = await crypto.subtle.importKey(
"jwk",
{
crv: "P-521",
ext: true,
key_ops: [],
kty: "EC",
x: "Adqn62IVQX8LIauAXrUtxH05DHlRygKcsP9qWAnd9tfJvpaG7bzIs16WMEUe1V-f4AxbQJceU4xCP8dJppK_fzdC",
y: "AEo3s1eExCOvpuBtBWnWlr7TuFhq_fMzqX9eqDHiy8qWl4I_koQtMePodrAc85mVrJAjvsa77Y3Ul3QtIWpXXBqa",
},
{
name: "ECDH",
namedCurve: "P-521",
},
false,
[],
);
Export key
const jwk = await crypto.subtle.exportKey(
"jwk",
publicKey,
);
Derive bits
const derivedBits = await crypto.subtle.deriveBits(
{
name: "ECDH",
public: publicKey,
},
privateKey,
128,
);
Derive key
const derivedKey = await crypto.subtle.deriveKey(
{
name: "ECDH",
public: publicKey,
},
privateKey,
{
name: "AES-CBC",
length: 128,
},
false,
["encrypt", "decrypt"],
);