Skip to content

hmac ​

The hmac namespace provides HMAC functions for authenticated messaging.

tyrus
import { CryptoError, hmac } from "tyrus:crypto";
import { utf8 } from "tyrus:encoding";

const data: u8[] = utf8.encode("message");
const key: u8[] = utf8.encode("secret-key");
const mac: Result<u8[], CryptoError> = hmac.sha256(data, key);
match (mac) { Ok(bytes) => { print(bytes.length); } Err(error) => { printError(error.code); } }

Functions ​

FunctionSignature
hmac.sha256(string | u8[], string | u8[]) => Result<u8[], CryptoError>
hmac.sha384(string | u8[], string | u8[]) => Result<u8[], CryptoError>
hmac.sha512(string | u8[], string | u8[]) => Result<u8[], CryptoError>

Both data and key accept string | u8[]. Temporary key copies controlled by the runtime are zeroized after use.

Use timingSafeEqual to verify HMACs in constant time.