diff --git a/web-crypto/derive-bits/ecdh.js b/web-crypto/derive-bits/ecdh.js
new file mode 100644
index 00000000..0b565be8
--- /dev/null
+++ b/web-crypto/derive-bits/ecdh.js
@@ -0,0 +1,56 @@
+(() => {
+
+ async function deriveSharedSecret(privateKey, publicKey) {
+ const sharedSecret = await window.crypto.subtle.deriveBits(
+ {
+ name: "ECDH",
+ namedCurve: "P-384",
+ public: publicKey
+ },
+ privateKey,
+ 256
+ );
+
+ const buffer = new Uint8Array(sharedSecret, 0, 5);
+ const sharedSecretValue = document.querySelector(".ecdh .derived-bits-value");
+ sharedSecretValue.classList.add("fade-in");
+ sharedSecretValue.addEventListener("animationend", () => {
+ sharedSecretValue.classList.remove("fade-in");
+ });
+ sharedSecretValue.textContent = `${buffer}...[${sharedSecret.byteLength} bytes total]`;
+ }
+
+ // Generate 2 ECDH key pairs: one for Alice and one for Bob
+ // In more normal usage, they would generate their key pairs
+ // separately and exchange public keys securely
+ const generateAlicesKeyPair = window.crypto.subtle.generateKey(
+ {
+ name: "ECDH",
+ namedCurve: "P-384"
+ },
+ false,
+ ["deriveBits"]
+ );
+
+ const generateBobsKeyPair = window.crypto.subtle.generateKey(
+ {
+ name: "ECDH",
+ namedCurve: "P-384"
+ },
+ false,
+ ["deriveBits"]
+ );
+
+ Promise.all([generateAlicesKeyPair, generateBobsKeyPair]).then(values => {
+ const alicesKeyPair = values[0];
+ const bobsKeyPair = values[1];
+
+ const deriveBitsButton = document.querySelector(".ecdh .derive-bits-button");
+ deriveBitsButton.addEventListener("click", () => {
+ // Alice then generates a secret using her private key and Bob's public key.
+ // Bob could generate the same secret using his private key and Alice's public key.
+ deriveSharedSecret(alicesKeyPair.privateKey, bobsKeyPair.publicKey);
+ });
+ });
+
+})();
diff --git a/web-crypto/derive-bits/index.html b/web-crypto/derive-bits/index.html
new file mode 100644
index 00000000..b195bf1c
--- /dev/null
+++ b/web-crypto/derive-bits/index.html
@@ -0,0 +1,55 @@
+
+
+
+
+ Web Crypto API example
+
+
+
+
+
+
Web Crypto: deriveBits
+
+
+
This page shows how to use the deriveBits() function of the Web Crypto API. It contains two separate examples, one for PBKDF2 and one for ECDH.
+
+
It's important to note that although both are defined in the API as key derivation functions, PBKDF2 and ECDH have very different use cases and characteristics.
+
+
+
PBKDF2 example
+
The PBKDF2 algorithm is used here to derive some bits from a password.
+
When you click "Derive bits" the example prompts you for a password and then derives some bits from the password using PBKDF2, and a representation of the derived bits is displayed.
+
+
+
ECDH example
+
The ECDH algorithm is more commonly called a "key agreement" algorithm. It enables two parties (conventionally called "Alice" and "Bob"), each of whom has a public/private key pair, to establish a shared secret key.
+
With this example we've created two ECDH key pairs, one for Alice and one for Bob.
+
When you click "Derive bits" the example uses Alice's private key and Bob's public key to derive a shared secret, and a representation of the shared secret is displayed.