I find myself particularly frustrated with the various checks and requirements imposed by password fields. Let's try to develop a straightforward password generator in Node.JS
Here we’re using Node’s built-in crypto module; crypto.randomBytes creates a buffer of random bytes with the length specified. toString(‘base64’) converts that into a base64 string. Base64 encoding expands the size of the data, so we use slice to cut the string to our desired length.
const crypto = require('crypto');
function generatePassword(length = 16) {
return crypto.randomBytes(length).toString('base64').slice(0, length);
}
console.log(generatePassword());
I’ve tried this on a couple of sign-up forms but the problem with this approach is that we’re not guaranteed to get a special character. So here we define what our special characters are, then slice our base64 in such a way that it is one less than the desired length, and introduce a special character at the end randomly.
const crypto = require('crypto');
function generatePassword(length = 16) {
const specialChars = '!@#$%^&*()_+=-';
const password = crypto.randomBytes(length).toString('base64').slice(0, length-1);
const specialChar = specialChars[Math.floor(Math.random() * specialChars.length)];
return password + specialChar;
}
console.log(generatePassword());
Strings are array-like objects, where individual characters correspond to specific indices. This allows you to access the individual characters of a string using array-like bracket notation.
specialChars[Math.floor(Math.random() * specialChars.length)]
This line is generating a random index number between 0 and the length of the special character string and then accesses the character at that index in the special character string. This effectively selects a random character from the special character string.
In JavaScript, both the Array and String objects have a slice
method. This method returns a shallow copy of a portion of the array or string into a new array or string object. It selects the elements starting at the provided start argument, and ends at, but does not include the provided end argument.
The syntax is as follows:
arr.slice([start[, end]])
Start(optional): The zero-based index at which to start extraction. If it is a negative number, it means the index is counted from the end of the array. For example, -1 means the last element of the array.
End(optional): The zero-based index before which to end extraction. The character in this index will not be included. If this parameter is not provided, slice will extract till the end of the array. If it is a negative number, it means the index is counted from the end of the array.
For example, in the statement
crypto.randomBytes(length).toString('base64').slice(0, length)
The slice function is used to cut the base64 string down to the specified length. The first argument is 0, which means start at the beginning of the string, and the second argument is length, which means stop at the character at index length. The result is a string that is length characters long.
An immediate enhancement to consider for the future is the ability to link the generated password with its corresponding website. While I intend to revisit this feature at a later stage, for the present moment, let's maintain the simplicity and compact nature of this project.