Module hmac

source · []
Expand description

Code related to HMAC

Functions

Export an HMAC key into the specified format

example
const jwk = await HMAC.exportKey("jwk", key.self);
example
const jwk = await key.exportKey("jwk");

Parameters

Returns Promise<JsonWebKey | ArrayBuffer>

Generate a new HMAC key

example
const key = await HMAC.generateKey();

Parameters

  • algorithm: Omit<EnforcedHmacKeyGenParams, "name"> = { hash: SHA.Variant.SHA_512, }
  • extractable: boolean = true
  • Optional keyUsages: KeyUsage[]

Returns Promise<HmacProxiedCryptoKey>

Import an HMAC key from the specified format

example
const key = await HMAC.importKey("jwk", jwk, {hash: "SHA-512"});

Parameters

  • format: KeyFormat
  • key: BufferSource | JsonWebKey
  • algorithm: Omit<EnforcedHmacImportParams, "name">
  • extractable: boolean = true
  • Optional keyUsages: KeyUsage[]

Returns Promise<HmacProxiedCryptoKey>

Sign a given payload

example
const message = new TextEncoder().encode("a message");
const signature = await HMAC.sign(key.self, message);
const message = new TextEncoder().encode("a message");
const signature = await key.sign(message);

Parameters

Returns Promise<ArrayBuffer>

Verify a given signature

example
const isVerified = await HMAC.verify(key, signature, message);
example
const isVerified = await key.verify(signature, message);

Parameters

Returns Promise<boolean>

Namespaces

Interfaces