github-webhook-filter/lib/crypto.ts

25 lines
825 B
TypeScript
Raw Normal View History

2022-09-02 20:24:33 +00:00
import config from "./config.ts";
import { hex } from "../deps.ts";
const signKey = await crypto.subtle.importKey(
"raw",
hex.decode(new TextEncoder().encode(config.signKey)),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign", "verify"],
);
const encoder = new TextEncoder();
export async function sign(input: string): Promise<string> {
const inputData = encoder.encode(input);
const sig = await crypto.subtle.sign("HMAC", signKey, inputData);
return new TextDecoder().decode(hex.encode(new Uint8Array(sig)));
}
2022-09-02 23:36:32 +00:00
export async function verify(input: string, signature: string): Promise<boolean> {
const signatureData = hex.decode(encoder.encode(signature));
const inputData = encoder.encode(input);
return await crypto.subtle.verify("HMAC", signKey, signatureData, inputData);
}