Greenheart / pagecrypt

Password Protected Single Page Applications and HTML files
GNU Affero General Public License v3.0
233 stars 25 forks source link

Consider adding support for automated password generation #4

Closed Greenheart closed 3 years ago

Greenheart commented 3 years ago

Since automated password generation is required in all 4 current pagecrypt use cases, it might be a good idea to add it to the core library, to reduce the need for external dependencies.

This basic password generator implements the main features of generate-password except the strict mode. However, with sufficient password length, this shouldn't be any problem.

This implementation also seems to be faster compared to generate-password, since it uses fewer operations.

const crypto = require('crypto')

function generatePassword(
    length = 40,
    characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
) {
    return Array.from(crypto.randomFillSync(new Uint32Array(length)))
        .map((x) => characters[x % characters.length])
        .join('')
}

console.log(generatePassword(80))

How to implement

Greenheart commented 3 years ago
// IDEA: Maybe add password generation as an option
// If the option is present, potential `[password]` argument will be ignored.
// If the option is not defined, `[password]` should be treated as an required argument.
pagecrypt
    .option('-g, --generate-password', 'Generate a random password with a given length', 80)
Greenheart commented 3 years ago

Will be released with 3.2.0