-
Notifications
You must be signed in to change notification settings - Fork 291
Add Example: drive key with ecdh #1269
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f3b3580
Add Exampke: drive key with ecdh
fwqaaq 2adffec
Format
fwqaaq 9775cd6
Merge branch 'main' into ecdh-drive-key
thisisjofrank fb46c68
Merge branch 'main' into ecdh-drive-key
thisisjofrank bb6ea3a
Update examples/scripts/ecdh_drive_key.ts
fwqaaq 43b4fd9
Merge branch 'main' into ecdh-drive-key
thisisjofrank 1b0307d
Update ecdh_drive_key.ts
thisisjofrank c6785ea
Update ecdh_drive_key.ts
fwqaaq 5e8a6a5
Update examples/scripts/ecdh_drive_key.ts
fwqaaq 3074ddb
Update examples/scripts/ecdh_drive_key.ts
fwqaaq 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 hidden or 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,122 @@ | ||
/** | ||
* @title ECDH Drive Key | ||
* @difficulty intermediate | ||
* @tags cli, web | ||
* @run <url> | ||
* @group Cryptography | ||
* | ||
* This example demonstrates how to derive a shared secret key using the Elliptic Curve Diffie-Hellman (ECDH) algorithm. | ||
*/ | ||
|
||
const data = "Hello, Deno 2.0!"; | ||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
|
||
// Derive symmetric keys from ECDH key pairs | ||
async function deriveSymmetricKey( | ||
privateKey: CryptoKey, | ||
publicKey: CryptoKey, | ||
): Promise<CryptoKey> { | ||
return await crypto.subtle.deriveKey( | ||
{ | ||
name: "ECDH", | ||
public: publicKey, | ||
}, | ||
privateKey, | ||
{ | ||
name: "AES-GCM", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"], | ||
); | ||
} | ||
|
||
// Encrypt and decrypt messages using AES-GCM | ||
async function encryptMessage( | ||
key: CryptoKey, | ||
iv: Uint8Array, | ||
data: Uint8Array, | ||
): Promise<ArrayBuffer> { | ||
return await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv, | ||
}, | ||
key, | ||
data, | ||
); | ||
} | ||
|
||
async function decryptMessage( | ||
key: CryptoKey, | ||
iv: Uint8Array, | ||
data: ArrayBuffer, | ||
): Promise<ArrayBuffer> { | ||
return await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv, | ||
}, | ||
key, | ||
data, | ||
); | ||
} | ||
|
||
// Get a human-readable string | ||
async function getBase64Key(key: CryptoKey): Promise<string> { | ||
const buffer = await crypto.subtle.exportKey("raw", key); | ||
return btoa(String.fromCharCode(...new Uint8Array(buffer))); | ||
} | ||
|
||
// Generate an ECDH key pair for Alice | ||
const aliceKey = await crypto.subtle.generateKey( | ||
{ | ||
name: "ECDH", | ||
namedCurve: "P-256", // Use the NIST P-256 curve, or P-384 or P-521 | ||
}, | ||
true, | ||
["deriveKey"], | ||
); | ||
// Generate an ECDH key pair for Bob | ||
const bobKey = await crypto.subtle.generateKey( | ||
{ | ||
name: "ECDH", | ||
namedCurve: "P-256", // Use the NIST P-256 curve, or P-384 or P-521 | ||
}, | ||
true, | ||
["deriveKey"], | ||
); | ||
// Drive symmetric keys for Alice | ||
const AliceSymmetricKey = await deriveSymmetricKey( | ||
aliceKey.privateKey, | ||
bobKey.publicKey, | ||
); | ||
|
||
// Output the derived symmetric key for Alice | ||
console.log("Alice's symmetric key:", await getBase64Key(AliceSymmetricKey)); | ||
|
||
// Drive symmetric keys for Bob | ||
const BobSymmetricKey = await deriveSymmetricKey( | ||
bobKey.privateKey, | ||
aliceKey.publicKey | ||
) | ||
|
||
// Output the derived symmetric key for Bob | ||
console.log("Bob's symmetric key:", await getBase64Key(BobSymmetricKey)); | ||
|
||
// Generate a random initialization vector (IV) for AES-GCM | ||
// The IV must be unique for each encryption operation but doesn't need to be secret. | ||
// The IV length for AES-GCM is typically 12 bytes. | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); // 12-byte IV for AES-GCM | ||
|
||
// Encrypt and decrypt a message using the derived symmetric keys | ||
const dataBuffer = encoder.encode(data); | ||
const encryptedData = await encryptMessage(AliceSymmetricKey, iv, dataBuffer); | ||
console.log("Encrypted Data:", new Uint8Array(encryptedData)); | ||
|
||
const decryptedData = await decryptMessage(BobSymmetricKey, iv, encryptedData); | ||
const decryptedMessage = decoder.decode(decryptedData); | ||
console.log("Verification of Decrypted Message:", decryptedMessage === data); | ||
console.log("Verification of Decrypted Message:", decryptedMessage === data); | ||
|
||
fwqaaq marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.