我正在构建一个api,而我在客户端接口中使用CryptoJs生成哈希时遇到了麻烦
我的js函数执行此操作:
console.log(" username: '" + $rootScope.username + "'");
console.log("timestamp: '" + timestamp + "'");
console.log(" request: '" + req + "'");
console.log(" entropy: '" + "dragonsahead" + "'");
console.log(" password: '" + $rootScope.password + "'");
var message = $rootScope.username+timestamp+req+"dragonsahead";
console.log(" message: '" + message +"'");
var hash = CryptoJS.HmacSHA1(message, $rootScope.password).toString();
console.log(" HASH: '" + hash + "'");
return hash;
$rootScope.username
和$rootScope.password
是用户输入;
JS日志:
username: 'admin'
timestamp: '1394643128.478'
request: '/login'
entropy: 'dragonsahead'
password: 'e1a47a0407d876c8187b1e984a6813abde8160af'
message: 'admin1394643128.478/logindragonsahead'
HASH: '5061875265279c7378c95c9536feade1c610492d'
我可以在服务器端日志中看到哈希值不匹配。 PHP服务器日志:
2014-03-12 16:52:08 - INFO --> username: 'admin'
2014-03-12 16:52:08 - INFO --> timestamp: '1394643128.478'
2014-03-12 16:52:08 - INFO --> request: '/login'
2014-03-12 16:52:08 - INFO --> entropy: 'dragonsahead'
2014-03-12 16:52:08 - INFO --> password: 'e1a47a0407d876c8187b1e984a6813abde8160af'
2014-03-12 16:52:08 - INFO --> message: 'admin1394643128.478/logindragonsahead'
2014-03-12 16:52:08 - INFO --> HASH: '4c34a29aa05059d5016bd74796407de3d2e5428c'
2014-03-12 16:52:08 - INFO -->CLIENTOKEN: '5061875265279c7378c95c9536feade1c610492d'
在此之后我检查了i this site并且服务器构建的哈希是正确的。 所以,我决定将CryptoJS加载到一个空白页面(例如谷歌),看看会发生什么:
> var jq = document.createElement('script');
> jq.src = "https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js";
> document.getElementsByTagName('head')[0].appendChild(jq);
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script>
>var test = CryptoJS.HmacSHA1("admin1394643128.478/logindragonsahead",'e1a47a0407d876c8187b1e984a6813abde8160af')
undefined
>test.toString()
"4c34a29aa05059d5016bd74796407de3d2e5428c"
由于CryptoJS返回正确的哈希,它只能是我搞乱了我的javascript变种。我不知道什么可能弄乱我的变量......
答案 0 :(得分:1)
碰巧我将密码存储在angularjs的“$ rootScope”中。
我需要$rootScope.password.tostring()
代替$rootScope.password
;
解决。