UNPKG

react-native-nitro-totp

Version:

React Native module for TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) authentication.

48 lines (44 loc) 1.54 kB
"use strict"; import { defaultOptions } from "./constants.js"; /** * Checks if the given OTP (One-Time Password) has a valid format. * * @param {string} otp - The OTP to check. * @param {GenerateOptions} [options=defaultOptions] - The options for OTP generation (optional). * @returns {boolean} True if the OTP format is valid, false otherwise. */ export const isValidOTP = (otp, options) => { const cleanedOtp = otp.replace(/\D+/g, ''); const digits = options.digits || defaultOptions.digits; return cleanedOtp.length === digits; }; /** * Formats the secret key for display by inserting hyphens every 4 characters. * * @param {string} secretKey - The secret key to format. * @returns {string} The formatted secret key. */ export const formatSecretKey = secretKey => { const cleanedKey = secretKey.replace(/[-=\s]/g, '').toUpperCase(); return cleanedKey.match(/.{1,4}/g)?.join('-') ?? secretKey; }; /** * Formats an OTP (One-Time Password) by grouping digits for readability. * * @param {string} otp - The OTP to format. * @returns {string} The formatted OTP. */ export const formatOTP = otp => { const cleanedOtp = otp.replace(/\D+/g, ''); return cleanedOtp.replace(/(\d{3})(?=\d)/g, '$1 '); }; /** * Parses the secret key by removing any hyphens from it. * * @param {string} secretKey - The secret key to parse. * @returns {string} The parsed secret key. */ export const parseSecretKey = secretKey => { return secretKey.replace(/[-=\s]/g, '').toUpperCase(); }; //# sourceMappingURL=utils.js.map