xxxxxxxxxx
const getRandomTokenCrypto = () => {
return new Promise((resolve, reject) => {
crypto.randomBytes(48, function (ex, buf) {
token = buf.toString("base64").replace(/\//g, "_").replace(/\+/g, "-");
if (ex) {
reject(ex);
}
resolve(token);
});
});
};
xxxxxxxxxx
require('crypto').randomBytes(48, function(ex, buf) {
token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-');
});
xxxxxxxxxx
const crypto = require('crypto');
// Define the length of the password
const passwordLength = 8;
// Generate a random password
const generatePassword = () => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let password = '';
for (let i = 0; i < passwordLength; i++) {
const randomIndex = crypto.randomInt(0, characters.length);
password += characters[randomIndex];
}
return password;
};
// Usage
const password = generatePassword();
console.log(password);