Friday, April 14, 2023

Quick Bytes: Javascript Live Password Hashing

I was recently working on a UI and wanted to hash a password while it was being typed. Here’s the quick code:

You need this in your HTML file to load the library

<script src=”https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

This javascript code

function hashPassword() {
const password = document.getElementById(‘password’).value;
const hashedPassword = CryptoJS.SHA256(password).toString();
console.log(‘Hashed password:’, hashedPassword);
document.getElementById(‘hashedPassword’).value = hashedPassword;
}

And then here’s the form code

<label for=”password”>Password Phrase:</label>
<input type=”text” id=”password” oninput=”hashPassword()”>
<input type=”text” id=”hashedPassword” name=”hashedPassword”>

Happy Coding!