simple-fs-server/util/secret.ts

28 lines
763 B
TypeScript
Raw Normal View History

2023-01-05 18:18:07 +09:00
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;
}
}