dreamworkAPI
Ctrl K
GitHub
libraryDocs / crypto / hmac

crypto.hmac

librarysharedmenusource

Hash-based Message Authentication Code library.

See RFC 2104 for the algorithm.

About HMAC

Fields

NameTypeDescription
md5Computes a hmac using the MD5 hashing.
sha1Computes a hmac using the SHA-1 hashing.
sha256Computes a hmac using the SHA-256 hashing.
sha384Computes a hmac using the SHA-384 hashing.
sha512Computes a hmac using the SHA-512 hashing.

Functions

crypto.hmac.digestComputes hmac of the message using the given hash function.
crypto.hmac.digestWithPadsComputes hmac of the message using precomputed pads from hmac.pads.
crypto.hmac.normalizeKeyNormalizes the key to the block size of the hash function.
crypto.hmac.padsComputes the outer and inner pads of the key, key ⊕ opad and key ⊕ ipad.
crypto.hmac.presetReturns a function that computes hmac using the given hash class.

#crypto.hmac.digest

sharedmenunodiscardsource
function crypto.hmac.digest(message: string, key: string, digest_fn: fun( data: string, as_hex: boolean? ): string, block_size: integer, as_hex?: boolean): string

Computes hmac of the message using the given hash function.

Parameters
NameTypeDescription
messagestringThe message to compute hmac for.
keystringThe key to use.
digest_fnfun( data: string, as_hex: boolean? ): stringThe hash function to use.
block_sizeintegerThe block size of the hash function.
as_hexoptionalbooleantrue if the result should be a hex string, false otherwise.
Returns
TypeNameDescription
stringhmac_strThe hmac string of the message.

#crypto.hmac.digestWithPads

sharedmenunodiscardsource
function crypto.hmac.digestWithPads(message: string, outer: string, inner: string, digest_fn: fun( data: string, as_hex: boolean? ): string, as_hex?: boolean): string

Computes hmac of the message using precomputed pads from hmac.pads.

Parameters
NameTypeDescription
messagestringThe message to compute hmac for.
outerstringThe outer pad.
innerstringThe inner pad.
digest_fnfun( data: string, as_hex: boolean? ): stringThe hash function to use.
as_hexoptionalbooleantrue if the result should be a hex string, false otherwise.
Returns
TypeNameDescription
stringhmac_strThe hmac string of the message.

#crypto.hmac.normalizeKey

sharedmenunodiscardsource
function crypto.hmac.normalizeKey(key: string, digest_fn: fun( data: string ): string, block_size: integer): string

Normalizes the key to the block size of the hash function.

Keys longer than block_size are hashed first, shorter keys are padded with zero bytes.

Parameters
NameTypeDescription
keystringThe key to normalize.
digest_fnfun( data: string ): stringThe hash function to use.
block_sizeintegerThe block size of the hash function.
Returns
TypeNameDescription
stringnormalized_keyThe key of exactly block_size bytes.

#crypto.hmac.pads

sharedmenunodiscardsource
function crypto.hmac.pads(key: string, block_size: integer): string, string

Computes the outer and inner pads of the key, key ⊕ opad and key ⊕ ipad.

The pads can be computed once and reused with hmac.digestWithPads for many messages under the same key.

Parameters
NameTypeDescription
keystringThe key normalized by hmac.normalizeKey, must be exactly block_size bytes.
block_sizeintegerThe block size of the hash function.
Returns
TypeNameDescription
stringouterThe outer pad.
stringinnerThe inner pad.

#crypto.hmac.preset

sharedmenunodiscardsource
function crypto.hmac.preset(hash: crypto.HashClass): fun( message: string, key: string, as_hex: boolean? ): string

Returns a function that computes hmac using the given hash class.

Parameters
NameTypeDescription
hashcrypto.HashClassThe hash class to use, e.g. crypto.SHA256.
Returns
TypeNameDescription
fun( message: string, key: string, as_hex: boolean? ): string
Generated from the dreamwork meta branch.