32 lines
721 B
TypeScript
32 lines
721 B
TypeScript
export async function generateSecretKey() {
|
|
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,
|
|
{ name: "HMAC", hash: "SHA-512" },
|
|
true,
|
|
["sign", "verify"],
|
|
);
|
|
return key;
|
|
}
|
|
} else {
|
|
const key = await generateSecretKey();
|
|
const out = await crypto.subtle.exportKey("jwk", key);
|
|
Deno.env.set("SECRET_KEY", JSON.stringify(out));
|
|
return key;
|
|
}
|
|
}
|