How to validate Symfony2 sha512 passwords using nodejs

≡放荡痞女 提交于 2019-12-04 20:20:33

Symfony does 5000 iterations with sha512.

  1. They concatenate raw password with user's salt to generate a new salt
  2. They generate a starting hash with above data.
  3. On each iteration previous hash its updated with the current hash + new salt generated at step 1.

In node, on each iteration you should digest previous hash as binary and in the end digest as base64, to mimic what Symfony does.

And an example:

var crypto = require('crypto');

var encodePassword = function (raw, salt) {
    var salted = raw + '{'+salt+'}',
        hash = crypto.createHash('sha512').update(salted, 'utf-8');

    for (var i = 1; i < 5000 ; i++) {
        hash = crypto.createHash('sha512').update(hash.digest('binary')+salted);
    }

    return hash.digest('base64');
};

console.log("Password: "+ encodePassword("secret", "h2zaays1cx2og00c6ow2gc0k4skg41g"));

Hello might be it will be useful for someone. This is part of code from my live website that works.

https://gist.github.com/konstantinzolotarev/deec71876739f8bf1058

Check Pbkdf2PasswordEncoder source code to see defaults values. Also keep in mind that Symfony will do a base64_encode by default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!