UNPKG

@nithin-sivakumar/genotp

Version:

A lightweight and secure OTP (One-Time Password) generator for JavaScript with customizable features like length, character set, prefix, suffix, and base64 encoding.

97 lines (70 loc) 3.49 kB
# @nithin-sivakumar/genotp A lightweight and secure OTP (One-Time Password) generator for JavaScript with customizable features like length, character set, prefix, suffix, and base64 encoding. ## Features - **Secure OTP Generation**: Uses `crypto` module for secure, random OTP generation. - **Customizable Length**: Generate OTPs of any length. - **Multiple Character Sets**: - Numeric: `0123456789` - Alphanumeric: `ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789` - Custom: Use a custom character set of your choice. - **Prefix and Suffix**: Optionally add a prefix or suffix to your OTP. - **Base64 Encoding**: Optionally return the OTP as a base64-encoded string. - **No Expiry Handling**: This package only focuses on OTP generation; expiry is left to the user. ## Installation To install the package, run: ```bash npm install @nithin-sivakumar/genotp ``` ## Usage ### Generate OTP To generate an OTP, use the `generate` function with the following options: ```javascript import { generate } from "@nithin-sivakumar/genotp"; // Generate Numeric OTP of length 6 const otp = generate(6, "numeric"); console.log("Generated Numeric OTP:", otp); // Generate Alphanumeric OTP with a prefix const otpWithPrefix = generate(8, "alphanumeric", "", "OTP-", ""); console.log("Generated OTP with Prefix:", otpWithPrefix); // Generate OTP with a custom character set const customCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#"; const otpWithCustomCharSet = generate(10, "custom", customCharSet); console.log("Generated OTP with Custom Character Set:", otpWithCustomCharSet); // Generate Base64-encoded OTP const base64Otp = generate(10, "alphanumeric", "", "", "", true); console.log("Base64 Encoded OTP:", base64Otp); ``` ### Options for `generate` function: - **`length`** (number): The length of the OTP. Default is `6`. - **`mode`** (string): The mode of OTP generation: - `'numeric'`: OTP contains only numbers. - `'alphanumeric'`: OTP contains numbers and letters (both uppercase). - `'custom'`: OTP uses a custom character set. Default is `'numeric'`. - **`charSet`** (string): Custom characters to use for OTP generation (only for `custom` mode). Default is empty. - **`prefix`** (string): Optional prefix to be added to the OTP. Default is `""`. - **`suffix`** (string): Optional suffix to be added to the OTP. Default is `""`. - **`base64Encode`** (boolean): Whether to return the OTP as a base64-encoded string. Default is `false`. ## Example ```javascript import { generate } from "@nithin-sivakumar/genotp"; // Generate OTP of length 6 using numeric characters const otp = generate(6, "numeric"); console.log(otp); // Example: 123456 // Generate OTP of length 8 using alphanumeric characters const otpAlphanumeric = generate(8, "alphanumeric"); console.log(otpAlphanumeric); // Example: A1B2C3D4 // Generate OTP with custom character set const customOtp = generate( 10, "custom", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#" ); console.log(customOtp); // Example: aB1!jK8@ ``` ## Error Handling The package throws the following errors: - **Invalid Mode or Character Set**: If the mode or character set is invalid, an error will be thrown. - **Invalid Length**: If the length is less than or equal to 0, an error will be thrown. ## License MIT License. See [LICENSE](LICENSE) for more information.