Skip to content

Instantly share code, notes, and snippets.

View Gu7z's full-sized avatar
🏠
Working from home

Gustavo Ferri Gu7z

🏠
Working from home
View GitHub Profile
@Gu7z
Gu7z / hash_password.js
Created March 7, 2023 19:09
Generate random password in JS with webcrypto
async function hashPassword(password) {
const passwordBuffer = new TextEncoder().encode(password);
const hashBuffer = await crypto.subtle.digest('SHA-256', passwordBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
function generateRandomPassword(length) {
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';