-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add examples for deriveBits #39
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Web Crypto API example</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
|
||
<body> | ||
<main> | ||
<h1>Web Crypto: deriveBits</h1> | ||
|
||
<section class="description"> | ||
<p>This page shows how to use the <code>deriveBits()</code> function of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API">Web Crypto API</a>. It contains two separate examples, one for PBKDF2 and one for ECDH.</p> | ||
|
||
<p>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.</p> | ||
|
||
<hr/> | ||
<h2>PBKDF2 example</h2> | ||
<p>The PBKDF2 algorithm is used here to derive some bits from a password.</p> | ||
<p>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.</p> | ||
|
||
<hr/> | ||
<h2>ECDH example</h2> | ||
<p>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.</p> | ||
<p>With this example we've created two ECDH key pairs, one for Alice and one for Bob.</p> | ||
<p>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.</p> | ||
|
||
</section> | ||
|
||
<section class="examples"> | ||
|
||
<section class="derive-bits pbkdf2"> | ||
<h2>PBKDF2</h2> | ||
<section class="derive-bits-controls"> | ||
<div class="derived-bits">Derived bits:<span class="derived-bits-value"></span></div> | ||
<input class="derive-bits-button" type="button" value="Derive bits"> | ||
</section> | ||
</section> | ||
|
||
<section class="derive-bits ecdh"> | ||
<h2>ECDH</h2> | ||
<section class="derive-bits-controls"> | ||
<div class="derived-bits">Shared secret:<span class="derived-bits-value"></span></div> | ||
<input class="derive-bits-button" type="button" value="Derive bits"> | ||
</section> | ||
</section> | ||
|
||
</section> | ||
</main> | ||
|
||
</body> | ||
<script src="pbkdf2.js"></script> | ||
<script src="ecdh.js"></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
(() => { | ||
|
||
let salt; | ||
|
||
/* | ||
Get some key material to use as input to the deriveBits method. | ||
The key material is a password supplied by the user. | ||
*/ | ||
function getKeyMaterial() { | ||
const password = window.prompt("Enter your password"); | ||
const enc = new TextEncoder(); | ||
return window.crypto.subtle.importKey( | ||
"raw", | ||
enc.encode(password), | ||
{name: "PBKDF2"}, | ||
false, | ||
["deriveBits", "deriveKey"] | ||
); | ||
} | ||
|
||
/* | ||
Derive some bits from a password supplied by the user. | ||
*/ | ||
async function getDerivedBits() { | ||
const keyMaterial = await getKeyMaterial(); | ||
salt = window.crypto.getRandomValues(new Uint8Array(16)); | ||
const derivedBits = await window.crypto.subtle.deriveBits( | ||
{ | ||
"name": "PBKDF2", | ||
salt: salt, | ||
"iterations": 100000, | ||
"hash": "SHA-256" | ||
}, | ||
keyMaterial, | ||
256 | ||
); | ||
|
||
const buffer = new Uint8Array(derivedBits, 0, 5); | ||
const derivedBitsValue = document.querySelector(".pbkdf2 .derived-bits-value"); | ||
derivedBitsValue.classList.add("fade-in"); | ||
derivedBitsValue.addEventListener("animationend", () => { | ||
derivedBitsValue.classList.remove("fade-in"); | ||
}); | ||
derivedBitsValue.textContent = `${buffer}...[${derivedBits.byteLength} bytes total]`; | ||
} | ||
|
||
const deriveBitsButton = document.querySelector(".pbkdf2 .derive-bits-button"); | ||
deriveBitsButton.addEventListener("click", () => { | ||
getDerivedBits(); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 51 |
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* General setup */ | ||
|
||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
html,body { | ||
font-family: sans-serif; | ||
line-height: 1.2rem; | ||
} | ||
|
||
/* Layout and styles */ | ||
|
||
h1 { | ||
color: green; | ||
margin-left: .5rem; | ||
} | ||
|
||
.description, .derive-bits { | ||
margin: 0 .5rem; | ||
} | ||
|
||
.description > p { | ||
margin-top: 0; | ||
} | ||
|
||
.derive-bits { | ||
box-shadow: -1px 2px 5px gray; | ||
padding: .5rem; | ||
margin-bottom: 2rem; | ||
} | ||
|
||
input[type="button"] { | ||
width: 5rem; | ||
} | ||
|
||
.derived-bits-value { | ||
padding-left: .5rem; | ||
font-family: monospace; | ||
} | ||
|
||
/* Whole page grid */ | ||
main { | ||
display: grid; | ||
grid-template-columns: 40rem 1fr; | ||
grid-template-rows: 4rem 1fr; | ||
} | ||
|
||
h1 { | ||
grid-column: 1/2; | ||
grid-row: 1; | ||
} | ||
|
||
.examples { | ||
grid-column: 1; | ||
grid-row: 2; | ||
} | ||
|
||
.description { | ||
grid-column: 2; | ||
grid-row: 2; | ||
} | ||
|
||
/* derive-bits controls grid */ | ||
.derive-bits-controls { | ||
display: grid; | ||
grid-template-columns: 1fr 5rem; | ||
grid-template-rows: 1fr; | ||
} | ||
|
||
.derived-bits { | ||
grid-column-start: 1; | ||
grid-row-start: 1; | ||
} | ||
|
||
.derive-bits-button { | ||
grid-column-start: 2; | ||
grid-row-start: 1; | ||
} | ||
|
||
/* Animate output display */ | ||
.fade-in { | ||
animation: fadein .5s; | ||
} | ||
|
||
@keyframes fadein { | ||
from { | ||
opacity: 0; | ||
} | ||
to { | ||
opacity: 1; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mdn:master