2015-05-13 89 views
0

所以我遇到了使用jsrsasign的問題。我不完全是我做錯了。 我試圖模仿我們使用角度在NodeJS中使用的散列過程,但不清楚問題是什麼。 下面是我們在那的NodeJS完美運行節點CryptoJS vs Jsrsasign SHA256withRSA加密pem

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值顯示正確的,但,當我告訴簽名,這是不正確。我不太清楚如何處理這個,但任何援助將不勝感激。

回答

0

經過一番研究,我意識到,我忘了Base64編碼從angularjs答案

所以我從這個

var signature = sig.sign(); 

改變controller.js代碼更改爲:

var signature = hextob64(sig.sign()); 

我還必須更新index.html文件以包含此庫

<script language="JavaScript" type="text/javascript" src="bower_components/jsrsasign/base64x-1.1.js"></script> 

一切匹配一次我添加這些更改。