Last active
January 22, 2024 10:36
-
-
Save konstantinzolotarev/deec71876739f8bf1058 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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(this.algorithm).update(salted).digest('binary'); | |
for (var i = 1; i < this.iterations; i++){ | |
digest = crypto.createHash(this.algorithm).update( Buffer.concat([Buffer.from(digest, 'binary'), Buffer.from(salted, 'utf8')]) ).digest('binary'); | |
} | |
return (Buffer.from(digest, 'binary')).toString('base64'); | |
}; |
@anthonymassard Sorry. Seems that Github didn't create notification about comments here.
Do you still has this issue ?
This doesn't currently work with Node 10.13.0. The concatenation of digest and salted causes issue and doesn't match Symfony. I got around this via the Buffer::concat method. See below;
var salted = `${password}{${salt}}`;
if (!salt){
salted = password;
}
var digest = crypto.createHash(this.algorithm).update(salted).digest('binary');
for (var i = 1; i < this.iterations; i++){
digest = crypto.createHash(this.algorithm).update( Buffer.concat([Buffer.from(digest, 'binary'), Buffer.from(salted, 'utf8')]) ).digest('binary');
}
return (Buffer.from(digest, 'binary')).toString('base64');
Thanks for your code also!
Thanks!! it works perfectly!
Thank you @stevebaldwin21
Many thanks @stevebaldwin21 !
Thanks @stevebaldwin21 saved us hours
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I want to implement this code in a pure JS script (without node).
I attempted to Browserify this script but I have different result then. Have you an idea about how implement this in a browser ?
Thank you in advance