Skip to content

Instantly share code, notes, and snippets.

@itorres
Created June 18, 2012 06:10
Show Gist options
  • Save itorres/2947088 to your computer and use it in GitHub Desktop.
Save itorres/2947088 to your computer and use it in GitHub Desktop.
node.js ssha hash generation and check functions intended for use with LDAP servers.
var crypto = require('crypto');
/*
* node.js ssha hash generation and check functions intended for use with LDAP servers.
*/
function ssha(cleartext, salt) {
var sum = crypto.createHash('sha1');
( typeof(salt) == 'undefined') ? salt = new Buffer(crypto.randomBytes(20)).toString('base64') : salt = salt;
sum.update(cleartext);
sum.update(salt);
var digest = sum.digest();
var ssha = '{SSHA}' + new Buffer(digest+salt,'binary').toString('base64');
return ssha;
}
function checkssha(cleartext, hash) {
var sum = crypto.createHash('sha1');
if (hash.substr(0,6) != '{SSHA}') {
console.error("Not a SSHA hash");
return false;
}
var bhash = new Buffer(hash.substr(6),'base64');
var salt = bhash.toString('binary',20);
var newssha = ssha(cleartext,salt);
return (hash == newssha);
}
var cleartext = "Aliquando bonus dormitat Homerus"
var hash=ssha(cleartext);
console.log(checkssha(cleartext, hash));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment