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

password-toolkit

v2.0.0

Published

Library for secure password, for evaluating and generating secure passwords.

Downloads

37

Readme

PasswordToolKit

Github Actions npm version License

The PasswordToolKit is a simple and easy-to-use class that allows generating and evaluating password strength.

Table of Contents

Installation

You can install PasswordToolKit via npm:

npm install password-toolkit

Importing

To use PasswordToolKit in your JavaScript application, first import it:

// Using ES6 imports
import PasswordToolKit from 'password-toolkit';

// Using Node.js `require()`
const PasswordToolKit = require('password-toolkit');

Usage

The PasswordToolKit module is a class that needs to be instantiated with a settings a object with configuration settings, before it can be used. Once instantiated, you can use the create() and evaluate() methods to create and evaluate passwords.

// Import the PasswordToolKit module
const PasswordToolKit = require('password-toolkit');

// Create an instance of PasswordToolKit
const passwordToolKit = new PasswordToolKit();

// It is equivalent
const passwordToolKit = new PasswordToolKit({
  maximum: 30,
  suggestions: [
    'The password must have at least 8 characters.',
    'Add uppercase and lowercase letters to make the password more secure.',
    'Add numbers to make the password more secure.',
    'Add symbols to make the password more secure.',
    'Avoid using repeated characters in the password.',
    'Avoid using common password patterns.',
    'Excellent! The password is secure.',
  ],
  qualities: ['insecure', 'low', 'medium', 'high', 'perfect'],
});

// Or You can translate the messages for the evaluation of the password
const passwordToolKit = new PasswordToolKit({
  maximum: 30,
  suggestions: [
    'La contraseña debe tener al menos 8 caracteres.',
    'Agregue letras mayúsculas y minúsculas para que la contraseña sea más segura.',
    'Agregue números para que la contraseña sea más segura.',
    'Agregue símbolos para que la contraseña sea más segura.',
    'Evitar el uso de caracteres repetidos en la contraseña.',
    'Evitar el uso de patrones de contraseña comunes.',
    '¡Excelente! La contraseña es segura.',
  ],
  qualities: ['Inseguro', 'Bajo', 'Medio', 'Alto', 'Perfecto'],
});

// Generate a password
const options = {
  size: 12,
  numbers: true,
  symbols: true,
  uppercases: true,
  lowercases: true
};

const validation = passwordToolKit.checkOptions(options);

if (validation.ok) {
  const password = passwordToolKit.create(options);
  // Evaluate the security of a password
  const evaluation = passwordToolKit.evaluate(password);

  // Output the results
  console.log('Generated Password:', password);
  console.log('Password Evaluation:', evaluation);
} else {
  throw new Error(validation.reason);
}

Type Definitions

PasswordToolKitSettings

The options for the PasswordToolKit instance.

type: Object

| Property | Type | Description | |-------------|------------------|----------------------------------------------------------| | maximum | number | The maximum length allowed for a password. | | suggestions | Array.<string> | Texts offering suggestions to improve password security. | | qualities | Array.<string> | Quality levels for password strength. |

OptionsValidation

The result of validating password options.

type: Object

| Property | Type | Description | |----------|-----------|-------------------------------------------------------------------| | ok | boolean | Indicates whether the validation passed true or failed false. | | reason | string | Reason for the validation result. |

GenerateOptions

Configuration options to generate passwords.

type: Object

| Property | Type | Description | |------------|-----------|------------------------------------------------------------------| | size | number | The desired size of the password. | | numbers | boolean | Indicates whether numbers are allowed in the password. | | symbols | boolean | Indicates whether symbols are allowed in the password. | | uppercases | boolean | Indicates whether uppercase letters are allowed in the password. | | lowercases | boolean | Indicates whether lowercase letters are allowed in the password. |

PasswordEvaluation

The result of validating a password's security.

type: Object

| Property | Type | Description | |------------|----------|---------------------------------------------------------------| | level | number | A number indicating the security level of the password (0-5). | | suggestion | string | Text offering suggestions to improve password security. | | quality | string | The quality level of the password. |

API

PasswordToolKit(settings)

Creates an instance of the PasswordToolKit class.

Arguments

| Name | Type | Description | |------------------------|------------------|---------------------------------------------------------| | settings | object | The options for the PasswordToolKit instance. | | settings.maximum | number | The maximum length allowed for a password. | | settings.suggestions | Array.<string> | Text offering suggestions to improve password security. | | settings.qualities | Array.<string> | Array of quality levels for password strength. |

Throws

| Type | Description | |--------------|----------------------------------------------| | TypeError | if the options parameter is not an object. | | TypeError | if the suggestions value is not an array. | | TypeError | if the qualities value is not an array. | | TypeError | if the maximum value is not a number. | | TypeError | if any suggestions value is not a string. | | TypeError | if any qualities value is not a string. | | RangeError | if the suggestions array length is not 7. | | RangeError | if the qualities array length is not 5. |

Example

const PasswordToolKit = require('password-toolkit');

const passwordToolKit = new PasswordToolKit();

// or

PasswordToolKit#checkOptions(options)

Checks if the provided options for generating a password are valid.

Arguments

| Name | Type | Description | |----------------------|-----------|------------------------------------------------------------------| | options | object | The options for the password to be generated. | | options.size | number | The desired size of the password. | | options.numbers | boolean | Indicates whether numbers are allowed in the password. | | options.symbols | boolean | Indicates whether symbols are allowed in the password. | | options.uppercases | boolean | Indicates whether uppercase letters are allowed in the password. | | options.lowercases | boolean | Indicates whether lowercase letters are allowed in the password. |

Returns

An object with the following properties:

| Property | Type | Description | |----------|-----------|-------------------------------------------------------------------| | ok | boolean | Indicates whether the validation passed true or failed false. | | reason | string | Reason for the validation result. |

Example

const PasswordToolKit = require('password-toolkit');

const passwordToolKit = new PasswordToolKit();
const options = {
  size: 12,
  numbers: true,
  symbols: true,
  uppercases: true,
  lowercases: true
};
const validation = passwordToolKit.checkOptions(options);

PasswordToolKit#generate(options)

The generate() method generates a new password with the provided options. It accepts an options object with the following properties:

Arguments

| Name | Type | Description | |----------------------|-----------|------------------------------------------------------------------| | options | object | The options for the password to be generated. | | options.size | number | The desired size of the password. | | options.numbers | boolean | Indicates whether numbers are allowed in the password. | | options.symbols | boolean | Indicates whether symbols are allowed in the password. | | options.uppercases | boolean | Indicates whether uppercase letters are allowed in the password. | | options.lowercases | boolean | Indicates whether lowercase letters are allowed in the password. |

Returns

The method returns the generated password as a string, or null if the provided options are invalid.

Example

const options = {
  size: 12,
  numbers: true,
  symbols: true,
  uppercases: true,
  lowercases: true
};

const password = passwordToolKit.create(options);

PasswordToolKit#evaluate(password)

The evaluate() method evaluates the security level of a password. It accepts a password string as input.

Arguments

| Name | Type | Description | |------------|----------|-------------------------------| | password | string | The password to be evaluated. |

Returns

An object with the following properties:

| Property | Type | Description | |--------------|----------|---------------------------------------------------------------| | level | number | A number indicating the security level of the password (0-5). | | suggestion | string | Text offering suggestions to improve password security. | | quality | string | The quality level of the password. |

Throws

| Type | Description | |------------------|--------------------------------------------| | TypeError | If the password value is not a string. |

Example

const evaluation = passwordToolKit.evaluate('MySecurePassword123!');

Contributing

If you encounter any bugs or issues with PasswordToolKit, issues and feature requests are welcome. Feel free to check issues page if you want to contribute.

License

Distributed under the MIT License. See LICENSE for more information.