Skip to content

Instantly share code, notes, and snippets.

@process
Created May 26, 2014 20:57
Show Gist options
  • Save process/318b7d38a54368595d1e to your computer and use it in GitHub Desktop.
Save process/318b7d38a54368595d1e to your computer and use it in GitHub Desktop.
Creates an SSHA512 hash from the given cleartext and salt. If no salt is given, 4 random bytes are used. Derived from https://gist.github.com/itorres/2947088. The hashes generated by this function work with Dovecot, which is what is was written for.
function ssha512(cleartext, salt) {
( typeof(salt) == 'undefined') ? salt = CryptoJS.lib.WordArray.random(4) : salt = CryptoJS.enc.Utf8.parse(salt);
var plain = CryptoJS.enc.Utf8.parse(cleartext);
var digest = CryptoJS.SHA512(plain.concat(salt));
var ssha = '{SSHA512}' + digest.concat(salt).toString(CryptoJS.enc.Base64);
return ssha;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment