节点 CryptoJS 与 Jsrsasign SHA256withRSA 与加密 pem

Node CryptoJS vs Jsrsasign SHA256withRSA with encrypted pem

所以我 运行 遇到了使用 jsrsasign 的问题。我不太清楚我做错了什么。 我正在尝试使用 angular 模拟我们在 NodeJS 中 uinsg 的哈希过程,目前尚不清楚问题是什么。 这是我们在 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 值和签名值都产生了预期的结果 如果我们使用 angularjs 尝试类似的操作,则签名值不正确。

这里是 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 值看起来是正确的,但当我显示签名时,它是不正确的。我不太确定如何处理这个问题,但如有任何帮助,我们将不胜感激。

经过一些研究,我意识到我忘记对来自 angularjs

的答案进行 base64 编码

所以我更改了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>

添加这些更改后一切都匹配。