Skip to content

otplib API Documentation / @otplib/core / normalizeSecret

Function: normalizeSecret()

normalizeSecret(secret, base32?): Uint8Array

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

Normalize secret input to Uint8Array

Accepts either a Base32-encoded string or Uint8Array and returns Uint8Array. If a Base32Plugin is provided, string secrets will be automatically decoded.

Parameters

secret

The secret to normalize (string or Uint8Array)

string | Uint8Array<ArrayBufferLike>

base32?

Optional Base32Plugin to decode string secrets

decode

(str) => Uint8Array

Returns

Uint8Array

The secret as Uint8Array

Throws

If secret is a string but no Base32Plugin is provided

Example

ts
import { normalizeSecret } from '@otplib/core'
import { ScureBase32Plugin } from '@otplib/plugin-base32-scure'

const base32 = new ScureBase32Plugin()

// Uint8Array - returned as-is
const secret1 = normalizeSecret(new Uint8Array([1, 2, 3]))

// Base32 string - automatically decoded
const secret2 = normalizeSecret('JBSWY3DPEHPK3PXP', base32)

Released under the MIT License.