support public key

This commit is contained in:
Nebel 2022-10-03 20:35:33 +09:00
parent b482192b05
commit 738585dcaf
2 changed files with 26 additions and 7 deletions

View file

@ -40,8 +40,8 @@
<label style="display: block"> <label style="display: block">
Public Key Use Public Key Use
<select name="use"> <select name="use">
<option value="sig">signature</option> <option value="sig">Signature</option>
<option value="enc">encryption</option> <option value="enc">Encryption</option>
<option value="">-</option> <option value="">-</option>
</select> </select>
</label> </label>
@ -60,7 +60,14 @@
</label> </label>
<input type="submit" value="Generate" style="display: block" /> <input type="submit" value="Generate" style="display: block" />
</form> </form>
<pre id="output"></pre> <label>
Private Key
<pre id="private"></pre>
</label>
<label>
Public Key
<pre id="public"></pre>
</label>
</main> </main>
<footer> <footer>
by <a href="https://github.com/kou029w/" rel="noreferrer">kou029w</a> by <a href="https://github.com/kou029w/" rel="noreferrer">kou029w</a>

View file

@ -7,14 +7,17 @@ import {
} from "jose"; } from "jose";
const form = document.querySelector("form") as HTMLFormElement; const form = document.querySelector("form") as HTMLFormElement;
const output = document.querySelector("#output") as HTMLPreElement; const publicKeyOutput = document.querySelector("#public") as HTMLPreElement;
const privateKeyOutput = document.querySelector("#private") as HTMLPreElement;
async function onSubmit(e: SubmitEvent) { async function onSubmit(e: SubmitEvent) {
e.preventDefault(); e.preventDefault();
const data = new FormData(form); const data = new FormData(form);
const alg = data.get("alg") as string; const alg = data.get("alg") as string;
const use = (data.get("use") as string) || undefined; const use = (data.get("use") as string) || undefined;
const { privateKey } = await generateKeyPair(alg, { extractable: true }); const { privateKey, publicKey } = await generateKeyPair(alg, {
extractable: true,
});
const jwk = await exportJWK(privateKey); const jwk = await exportJWK(privateKey);
const createKid = { const createKid = {
"rfc7638-s256": () => calculateJwkThumbprint(jwk, "sha256"), "rfc7638-s256": () => calculateJwkThumbprint(jwk, "sha256"),
@ -25,8 +28,17 @@ async function onSubmit(e: SubmitEvent) {
"rfc9278-s512": () => calculateJwkThumbprintUri(jwk, "sha512"), "rfc9278-s512": () => calculateJwkThumbprintUri(jwk, "sha512"),
"date-time": () => new Date().toISOString(), "date-time": () => new Date().toISOString(),
}[data.get("kid-method") as string]; }[data.get("kid-method") as string];
output.textContent = JSON.stringify( const kid = await createKid?.();
{ ...{ alg, use, kid: await createKid?.() }, ...jwk }, privateKeyOutput.textContent = JSON.stringify(
{ ...{ alg, use, kid }, ...jwk },
null,
" "
);
publicKeyOutput.textContent = JSON.stringify(
{
...{ alg, use, kid },
...(await exportJWK(publicKey)),
},
null, null,
" " " "
); );