Skip to content

Instantly share code, notes, and snippets.

@billyxs
Last active October 11, 2020 16:50
Show Gist options
  • Save billyxs/c9a338b5443d346ff9eb to your computer and use it in GitHub Desktop.
Save billyxs/c9a338b5443d346ff9eb to your computer and use it in GitHub Desktop.
CryptoJS PBKDF2 hashing with javascript. This could be used for a simple, not high security, password auth.
// bower install crypto-js
// Add to scripts to html file
// <script src="bower_components/crypto-js/crypto-js.js"></script>
// <script src="bower_components/crypto-js/pbkdf2.js"></script>
function auth(password) {
// Make a salt with CryptoJS.lib.WordArray.random(128/8);
var salt = 'your salt'; // CryptoJS.lib.WordArray.random(128/8);
// 1000 iterations takes a couple seconds in the browser. Wouldn't want to go much higher if this is a browser implementation
var iterations = 1000;
// make your own hash if you don't know this one
var matchHash = '98a24959e05c77656f6308a541d2dd922c4dd9e2b6b75d49509e31c42fb4a4ba681ad5408ede32a523139bb38f3db5ea9f867f2cb4512137018296eeea000d36';
var userHash = CryptoJS.PBKDF2(password, salt, { keySize: 512/32, iterations: iterations });
return userHash.toString() === matchHash;
}
@Rahal-Kanishka
Copy link

@billyxs Can I know why the length of 'matchHash' variable is 128 while you mentioned the keySize is 512. Is it like an upper limit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment