Skip to content

otplib API Documentation / @otplib/core / utils / normalizeHashAlgorithm

Function: normalizeHashAlgorithm()

normalizeHashAlgorithm(value, options?): HashAlgorithm

Defined in: packages/core/src/utils.ts:425

Normalize and validate a hash algorithm

Matching ignores case and accepts an optional - or _ before the digest size, so 'SHA1', 'Sha1', 'SHA-1' and 'sha_1' all resolve to 'sha1'. This is safe to be forgiving about: every accepted alias names the same algorithm. No alias of any other digest appears in the table, so 'sha3-256' and 'sha-384' are rejected outright.

Anything that is not an alias of a supported algorithm is rejected rather than guessed at, because substituting a different algorithm produces self-consistent tokens that do not match conforming implementations using the requested algorithm - a failure that can remain hidden until interoperability is tested.

Parameters

value

unknown

The algorithm to normalize

options?

NormalizeHashAlgorithmOptions = {}

Narrowed algorithm set and calling plugin name

Returns

HashAlgorithm

The canonical lowercase algorithm

Throws

If the value is not a supported algorithm

Example

typescript
normalizeHashAlgorithm('SHA1');   // 'sha1'
normalizeHashAlgorithm('SHA-1');  // 'sha1'
normalizeHashAlgorithm('sha384'); // throws AlgorithmUnsupportedError

// A plugin backed by a restricted implementation
normalizeHashAlgorithm('sha256', { supported: ['sha1'], plugin: 'custom' });

Released under the MIT License.