npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

otp-string-generator

v1.0.2

Published

Last update 31 July 2023. Return type changed from string to Object.

Downloads

2

Readme

otp-string-generator

Description:

The otp-string-generator module is a versatile utility for generating secure One-Time Passwords (OTPs) and random key strings in Node.js. It provides functions to create cryptographically secure random strings of specified lengths, with optional strict criteria and additional data. This module ensures high randomness and can be used for various use cases, such as generating authentication tokens, unique URLs, or any random data required in your Node.js applications.

Code:

const Generate = require('otp-string-generator');

// Generate a 6-digit OTP
const otp = Generate.getOTP(6);
console.log(otp);
// Output: "237834" or any other randomly generated 6-digit number string

// Generate a random key string with parameters

/**
 * The function `getString` generates a random string of a specified length, with optional strict
 * criteria and additional data.
 * @param {number} Length - The length of the generated string. It specifies the number of characters the string should have.
 * @param {boolean} Strict - Determines whether the generated string should follow strict rules or not. If `true`, the generated string will have a specific pattern. If `false`, the generated string will be random.
 * @param {Object} more - An object that contains additional key-value pairs that you want to include in the generated string. These key-value pairs will be appended to the string in the format `key=value&`.
 * @returns {string} A random string.
 */
const ExampleKey1 = Generate.getString(10, false, { _id: "10", email: "[email protected]" });
console.log(ExampleKey1);
// Output: {_id: "10", email: "[email protected]",_id=10&[email protected]&key=L*Rq@EUoVn}

// Generate another random key string
const ExampleKey2 = Generate.getString(10, false, { _id: "10", email: "[email protected]" });
console.log(ExampleKey2);
// Output: {_id: "10", email: "[email protected]",_id=10&[email protected]&key=L*Rq@EUoVn}

// Generate a URL key string with parameters

/**
 * The function `getUrlString` generates a URL string with specified length, strictness, and additional parameters.
 * @param {number} Length - The length of the generated key string.
 * @param {boolean} Strict - Determines whether the generated URL string should follow strict rules or not. If `Strict` is set to `true`, the generated URL string will include both lowercase and uppercase letters, as well as Special Characters `!@#$%^*`. If `Strict` is set to `false`, the generated URL string will be random.
 * @param {Object} more - An object that contains additional key-value pairs to be included in the generated URL string. These key-value pairs will be appended to the URL as query parameters.
 * @param {string} url - The base URL for the generated URL string.
 * @param {boolean} https - A boolean value indicating whether the generated URL should use the "https" protocol or not.
 * @returns {string} The generated URL string.
 */
const ExampleKey3 = Generate.getUrlString(10, false, { _id: "10", email: "[email protected]" }, "www.example.com/verify?", false);
console.log(ExampleKey3);
// Output: { _id: "10", email: "[email protected]",string:http://www.example.com/verify??_id=10&[email protected]&key=sLqTg@kGOS}

// Generate another URL key string with strictness and HTTPS

const ExampleKey4 = Generate.getUrlString(10, true, { _id: "10", email: "[email protected]" }, "www.example.com/verify?", true);
console.log(ExampleKey4);
// Output: { _id: "10", email: "[email protected]",string:http://www.example.com/verify??_id=10&[email protected]&key=sLqTg@kGOS}

Other Points:

  1. The otp-string-generator module provides a convenient way to generate secure random strings, reducing the risk of predictable or weak tokens.

  2. The getOTP function is specifically designed to generate OTPs, which are commonly used for two-factor authentication and other security mechanisms.

  3. The getString function can be used to generate random strings with optional additional data. When Strict is set to false, the generated string will be random; when true, the string follows a specific pattern.

  4. The getUrlString function is tailored for generating URL keys, adding query parameters to the base URL. When Strict is set to true, the generated URL includes both lowercase and uppercase letters and special characters.

  5. For increased security, the module relies on the crypto module in Node.js to generate cryptographically secure random numbers.

  6. The generated strings can be used for a wide range of applications, such as token-based authentication, temporary URLs, and unique identifiers.

Overall, the otp-string-generator module simplifies the process of generating secure random strings and OTPs in Node.js, making it an excellent addition to any application requiring random data.

Authors