-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the WebCrypt conversion functions
- Loading branch information
Showing
5 changed files
with
210 additions
and
32 deletions.
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
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
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
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,60 @@ | ||
import { | ||
JWKKeyPair, MultikeyPair, | ||
cryptoToMultikey, multikeyToCrypto | ||
} from "../index"; | ||
|
||
/** ----------------------------- */ | ||
|
||
/************* Debugging help ***********/ | ||
// deno-lint-ignore no-explicit-any | ||
export function str(inp: any): void { | ||
console.log(JSON.stringify(inp, null, 4)); | ||
} | ||
|
||
/** | ||
* Convert a CryptoKey Pair into a JWK Pair. Not really used by these tools, but handy to have it to help debugging. | ||
* @param newPair | ||
* @returns | ||
*/ | ||
|
||
async function toJWK(newPair: CryptoKeyPair): Promise<JWKKeyPair> { | ||
const publicKey: JsonWebKey = await crypto.subtle.exportKey("jwk", newPair.publicKey); | ||
const privateKey: JsonWebKey = await crypto.subtle.exportKey("jwk", newPair.privateKey); | ||
return { public: publicKey, secret: privateKey }; | ||
} | ||
|
||
async function main(): Promise<void> { | ||
const onePair = async (label: string, pair: CryptoKeyPair): Promise<void> => { | ||
// Do a round-trip | ||
const mk: MultikeyPair = await cryptoToMultikey(pair); | ||
const newPair: CryptoKeyPair = await multikeyToCrypto(mk); | ||
|
||
// For debugging, both keypairs are converted into JWK | ||
const keyPair = await toJWK(pair); | ||
const mkPair = await toJWK(newPair); | ||
|
||
console.log(`----\n${label}:`); | ||
console.log(`Original key in JWK:`) | ||
str(keyPair); | ||
console.log(`Generated key in JWK:`) | ||
str(mkPair); | ||
|
||
if (label === "EDDSA") { | ||
console.log(`Values are equal? ${keyPair.secret?.x === mkPair.secret?.x && keyPair?.secret?.d === keyPair?.secret?.d}`) | ||
} else { | ||
console.log(`Values are equal? ${keyPair.secret?.x === mkPair.secret?.x && keyPair.secret?.y === mkPair.secret?.y && keyPair?.secret?.d === keyPair?.secret?.d}`) | ||
} | ||
} | ||
|
||
const eddsaPair: CryptoKeyPair = await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"]) as CryptoKeyPair; | ||
await onePair("EDDSA", eddsaPair); | ||
|
||
const p256: CryptoKeyPair = await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, ["sign", "verify"]) as CryptoKeyPair; | ||
await onePair("ECDSA P-256", p256); | ||
|
||
const p384: CryptoKeyPair = await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-384" }, true, ["sign", "verify"]) as CryptoKeyPair; | ||
await onePair("ECDSA P-384", p384); | ||
} | ||
|
||
main() | ||
|
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