JavaScript中的Symfony2密码编码器功能

JavaScript中的Symfony2密码编码器功能

本文介绍了JavaScript中的Symfony2密码编码器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FOSUserBundle和FOSRestBundle创建了一个Symfony2应用程序。我想使用rest api将其他应用程序连接到我的Symfony应用程序。
我需要用Javascript编写Symfony密码编码器功能。
实际上在PHP中是这样的:

I created a Symfony2 application using FOSUserBundle and FOSRestBundle. I'd like to connect other application with my Symfony application using rest api.I need to write the Symfony password encoder function in Javascript.Actually in PHP, it goes like:

$salt = "secret";
$password = "azerty";
$salted = $password.'{'.$salt.'}';
$digest = hash('sha512', $salted, true);

for ($i = 1; $i < 5000; $i++) {
    $digest = hash('sha512', $digest.$salted, true);
}
$digest = base64_encode($digest);

return $digest;

在Javascript中,我尝试使用CryptoJS库。我的代码是:

In Javascript, I tried to use CryptoJS library. My code is:

var salt = 'secret',
    password = 'azerty',
    salted = password + '{' + salt + '}'
    digest = CryptoJS.SHA512(salted);

for (var i=1; i<5000; i++) {
    digest = CryptoJS.SHA512(digest+salted);
}

digest = digest.toString(CryptoJS.enc.Base64);

return digest;

但是你猜怎么着?它不起作用,我也不知道为什么。
有人可以帮忙吗? :)

But guess what ? It does not work and i don't know why.Can anyone help please ? :)

致谢,
Colzak。

Regards,Colzak.

推荐答案

好吧@timothymctim的回复帮助了我。实际上,我认为这是有关字符编码的问题。
这是一个(奇怪的)解决方案:

Ok @timothymctim 's response helped me. Actually, I think it's an issue about character encoding.Here's a (strange) solution :

PHP:

$salt = "secret";
$password = "azerty";
$salted = $password.'{'.$salt.'}';
$digest = hash('sha512', $salted, true);

for ($i = 1; $i < 5000; $i++) {
    $digest = hash('sha512', utf8_encode($digest).$salted, true);
}
$digest = base64_encode($digest);

return $digest;

和Javascript:

And the Javascript :

var salt = 'secret',
password = 'azerty',
salted = password + '{' + salt + '}'
digest = CryptoJS.SHA512(salted);

for (var i=1; i<5000; i++) {
    digest = CryptoJS.SHA512(digest.toString(CryptoJS.enc.Latin1)+salted);
}

digest = digest.toString(CryptoJS.enc.Base64);

return digest;

我不知道该怎么想。
还是谢谢大家!

I don't know what to think.Thanks anyway everybody who helped !

这篇关于JavaScript中的Symfony2密码编码器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:21