Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save openarun/317cbc91a4572bd5878664bf209c6659 to your computer and use it in GitHub Desktop.
Save openarun/317cbc91a4572bd5878664bf209c6659 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment