helpers.js 596 B

12345678910111213141516171819
  1. import { decode } from '../util/base64url.js';
  2. export const unprotected = Symbol();
  3. export function assertNotSet(value, name) {
  4. if (value) {
  5. throw new TypeError(`${name} can only be called once`);
  6. }
  7. }
  8. export function decodeBase64url(value, label, ErrorClass) {
  9. try {
  10. return decode(value);
  11. }
  12. catch {
  13. throw new ErrorClass(`Failed to base64url decode the ${label}`);
  14. }
  15. }
  16. export async function digest(algorithm, data) {
  17. const subtleDigest = `SHA-${algorithm.slice(-3)}`;
  18. return new Uint8Array(await crypto.subtle.digest(subtleDigest, data));
  19. }