Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcousens/903168364ed97eea4c39b72bb9e87e23 to your computer and use it in GitHub Desktop.
Save rcousens/903168364ed97eea4c39b72bb9e87e23 to your computer and use it in GitHub Desktop.
Function for node.js that will encrypt password exactly like Symfony2 PHP framework. So all passwords from Symfony2 will work in your node.js application
'use strict';
var crypto = require('crypto');
var _ = require('lodash');
/**
* Will encrypt password in Symfony2 way using a given salt.
*
* @param {string} password
* @param {string} salt
* @returns {string}
*/
function encryptPassword(password, salt){
var salted = password + '{' + salt + '}';
if (!salt){
salted = password;
}
var digest = crypto.createHash('sha512').update(salted).digest('binary');
for (var i = 1; i < 5000; i++){
digest = crypto.createHash('sha512').update(digest + salted).digest('binary');
}
return (new Buffer(digest, 'binary')).toString('base64');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment