我正在构建一个 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'
在此之后,我检查了这个站点,服务器构建的哈希值是正确的。所以,我决定将 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 变量。我不知道什么可能会弄乱我的变量...