所以我使用jsrsasign遇到了一个问题。我不是我做错了什么。 我试图使用angular来模拟我们在NodeJS中使用的哈希过程,并且不清楚问题是什么。 以下是我们在NodeJS中完成的完美工作
var restler = require('restler');
var path = require('path');
var fs = require('fs');
var crypto = require('crypto');
var dateFormat = require('dateformat');
var sha_hash = crypto.createHash('SHA1');
var pem = fs.readFileSync('private-key.pem');
var pkey = pem.toString('ascii');
var private_key = {
key: pkey,
passphrase:'password'
}
//var current_date = new Date();
//console.log(dateFormat(current_date, "yyyy/mm/dd HH:MM:ss l"));
var current_date = '2015/05/06 20:07:12 926';
//SToken
sha_hash.update(current_date);
var stoken = sha_hash.digest('hex');
console.log(stoken);
//Signature
var signature = crypto.createSign('RSA-SHA256')
signature.update(stoken);
var sig = signature.sign(private_key, 'base64');
console.log(sig);
SToken值和Signature Values都会产生预期的结果 如果我们使用angularjs尝试类似的东西,则Signature值不正确。
这是index.html:
<!DOCTYPE html>
<html>
<body ng-app="main">
<div ng-view>
</div>
</body>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/dateformat/dist/dateformat.min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"> </script>
<script src="bower_components/crypto-js/core.js"></script>
<script src="bower_components/crypto-js/x64-core.js"></script>
<script src="bower_components/crypto-js/pbkdf2.js"></script>
<script src="bower_components/crypto-js/crypto-js.js"></script>
<script src="bower_components/crypto-js/hmac.js"></script>
<script src="bower_components/crypto-js/hmac-sha256.js"></script>
<script src="bower_components/crypto-js/sha1.js"></script>
<script src="bower_components/crypto-js/sha256.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/ext/base64.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/ext/jsbn.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/ext/jsbn2.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/ext/rsa.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/ext/rsa2.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/crypto-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/rsasign-1.2.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/rsapem-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/asn1-1.0.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/asn1hex-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/keyutil-1.0.js"></script>
<script src="modules/authentication/authentication.module.js"></script>
<script src="modules/authentication/authentication.controller.js"></script>
<script src="app.js" ></script>
</html>
这是authentication.controller.js文件:
(function (authenticationModule) {
var authenticationController = function($scope, $http, $q, $filter) {
var deferred = $q.defer()
var private_key = $http({
method: 'GET',
url: 'private-key.pem'
}).success(function(privateKey) {
deferred.resolve();
//var current_date = new Date();
//var date_string = $filter('date')(current_date, "yyyy/mm/dd HH:MM:ss SSS");
var date_string = '2015/05/06 20:07:12 926';
//SToken
var stoken = CryptoJS.SHA1(date_string).toString();
console.log(stoken);
$scope.stoken = stoken;
//Signature
var sig = new KJUR.crypto.Signature({"alg": "SHA256withRSA", "prov": "cryptojs/jsrsa"});
sig.init(privateKey, 'password');
sig.updateString(stoken);
var signature = sig.sign();
console.log(signature);
$scope.signature = signature;
}).error(function(error) {
console.error("there was an error reading file");
deferred.reject(error);
});
};
authenticationModule.controller('authenticationController', ['$scope', '$http', '$q', '$filter', authenticationController]);
}(angular.module('authentication')));
这是authentication.module.js文件
(function (angular) {
'use strict';
//Initialize the Module
angular.module('authentication',[]);
}(window.angular));
这是app.js文件:
(function(angular){
//Defines my angular main portion of the application
var module = angular.module('main',['ngRoute', 'authentication']);
//Defines the routes
module
.config(function($routeProvider){
$routeProvider
.when('/auth',{ templateUrl:'modules/authentication/authentication.view.html', controller:'authenticationController'})
.otherwise({redirectTo:'/auth'});
});
})(window.angular);
最后这里是观点:
<div>
SToken: {{stoken}}
</div>
<div>
Signature: {{signature}}
</div>
同样,重复一遍,SToken值显示正确,但是当我显示签名时,它是不正确的。我不太清楚如何处理这个,但任何协助将不胜感激。
答案 0 :(得分:0)
经过一些研究,我意识到我忘记了base64编码来自angularjs的答案
所以我改变了这个
的controller.js代码var signature = sig.sign();
对此:
var signature = hextob64(sig.sign());
我还必须更新index.html文件以包含此库
<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/base64x-1.1.js"></script>
一旦我添加了这些更改,所有内容都会匹配。