simple-fs-server/util/secret.ts

32 lines
721 B
TypeScript
Raw Normal View History

2023-01-05 18:18:07 +09:00
export async function generateSecretKey() {
2023-01-06 18:24:27 +09:00
const key = await crypto.subtle.generateKey(
{ name: "HMAC", hash: "SHA-512" },
true,
["sign", "verify"],
);
return key;
}
export async function prepareSecretKey() {
const key = Deno.env.get("SECRET_KEY");
if (key) {
const jwk = JSON.parse(key) as JsonWebKey;
{
const key = await crypto.subtle.importKey(
"jwk",
jwk,
2023-01-05 18:18:07 +09:00
{ name: "HMAC", hash: "SHA-512" },
true,
["sign", "verify"],
);
2023-01-06 18:24:27 +09:00
return key;
}
} else {
const key = await generateSecretKey();
const out = await crypto.subtle.exportKey("jwk", key);
Deno.env.set("SECRET_KEY", JSON.stringify(out));
2023-01-05 18:18:07 +09:00
return key;
2023-01-06 18:24:27 +09:00
}
2023-01-05 18:18:07 +09:00
}