summaryrefslogtreecommitdiff
path: root/std/jwt/_signature.ts
blob: 81c1309d14fe25ec7a610a6014bd45c9c9f5cd70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { Algorithm } from "./_algorithm.ts";
import { HmacSha256 } from "../hash/sha256.ts";
import { HmacSha512 } from "../hash/sha512.ts";
import { encode as convertUint8ArrayToBase64url } from "../encoding/base64url.ts";
import { decodeString as convertHexToUint8Array } from "../encoding/hex.ts";

export function convertHexToBase64url(input: string): string {
  return convertUint8ArrayToBase64url(convertHexToUint8Array(input));
}

function encrypt(
  algorithm: Algorithm,
  key: string,
  message: string,
): string {
  switch (algorithm) {
    case "none":
      return "";
    case "HS256":
      return new HmacSha256(key).update(message).toString();
    case "HS512":
      return new HmacSha512(key).update(message).toString();
    default:
      throw new RangeError(
        `The algorithm of '${algorithm}' in the header is not supported.`,
      );
  }
}

/**
 * Create a signature
 * @param algorithm
 * @param key
 * @param input
 */
export async function create(
  algorithm: Algorithm,
  key: string,
  input: string,
): Promise<string> {
  return convertHexToBase64url(await encrypt(algorithm, key, input));
}

/**
 * Verify a signature
 * @param signature
 * @param key
 * @param alg
 * @param signingInput
 */
export async function verify({
  signature,
  key,
  algorithm,
  signingInput,
}: {
  signature: string;
  key: string;
  algorithm: Algorithm;
  signingInput: string;
}): Promise<boolean> {
  return signature === (await encrypt(algorithm, key, signingInput));
}