Permalink
Browse files

Default hash string methods convert to utf8 which is no good for bina…

…ry data

Make hex versions available
  • Loading branch information...
1 parent ef0e9d4 commit b591c21cfe3e5aca7c3b05945bdda8c87c248a96 @davedoesdev davedoesdev committed Jul 23, 2013
Showing with 19 additions and 11 deletions.
  1. +0 −4 base64x-1.1.js
  2. +10 −0 crypto-1.1.js
  3. +9 −7 rsasign-1.2.js
View
@@ -293,10 +293,6 @@ function rstrtohex(s) {
return result;
}
-function rstrtoutf8(s) {
- return unescape(encodeURIComponent(s));
-}
-
// ==== URIComponent / hex ================================
/**
* convert a URLComponent string such like "%67%68" to a hexadecimal string.<br/>
View
@@ -133,6 +133,11 @@ KJUR.crypto.Util = new function() {
return md.digestString(s);
};
+ this.sha256Hex = function(s) {
+ var md = new KJUR.crypto.MessageDigest({'alg':'sha256', 'prov':'cryptojs'});
+ return md.digestHex(s);
+ };
+
/**
* get hexadecimal SHA512 hash of string
* @name sha512
@@ -147,6 +152,11 @@ KJUR.crypto.Util = new function() {
return md.digestString(s);
};
+ this.sha512Hex = function(s) {
+ var md = new KJUR.crypto.MessageDigest({'alg':'sha512', 'prov':'cryptojs'});
+ return md.digestHex(s);
+ };
+
/**
* get hexadecimal MD5 hash of string
* @name md5
View
@@ -63,6 +63,8 @@ _RSASIGN_HASHHEXFUNC['sha256'] = function(s){return KJUR.crypto.Util.sha256(s
_RSASIGN_HASHHEXFUNC['sha512'] = function(s){return KJUR.crypto.Util.sha512(s);}
_RSASIGN_HASHHEXFUNC['md5'] = function(s){return KJUR.crypto.Util.md5(s);};
_RSASIGN_HASHHEXFUNC['ripemd160'] = function(s){return KJUR.crypto.Util.ripemd160(s);};
+_RSASIGN_HASHHEXFUNC['sha256Hex'] = function(s){return KJUR.crypto.Util.sha256Hex(s);}
+_RSASIGN_HASHHEXFUNC['sha512Hex'] = function(s){return KJUR.crypto.Util.sha512Hex(s);}
//_RSASIGN_HASHHEXFUNC['sha1'] = function(s){return sha1.hex(s);} // http://user1.matsumoto.ne.jp/~goma/js/hash.html
//_RSASIGN_HASHHEXFUNC['sha256'] = function(s){return sha256.hex;} // http://user1.matsumoto.ne.jp/~goma/js/hash.html
@@ -130,7 +132,7 @@ function pss_mgf1_str(seed, len, hash) {
var mask = '', i = 0;
while (mask.length < len) {
- mask += hextorstr(hash(rstrtoutf8(seed + String.fromCharCode.apply(String, [
+ mask += hextorstr(hash(rstrtohex(seed + String.fromCharCode.apply(String, [
(i & 0xff000000) >> 24,
(i & 0x00ff0000) >> 16,
(i & 0x0000ff00) >> 8,
@@ -151,8 +153,8 @@ function pss_mgf1_str(seed, len, hash) {
* @return returns hexadecimal string of signature value.
*/
function _rsasign_signStringPSS(s, hashAlg, sLen) {
- var hashFunc = _RSASIGN_HASHHEXFUNC[hashAlg];
- var hHash = hashFunc(s);
+ var hashFunc = _RSASIGN_HASHHEXFUNC[hashAlg + 'Hex'];
+ var hHash = hashFunc(rstrtohex(s));
var mHash = hextorstr(hHash);
var hLen = mHash.length;
var emBits = this.n.bitLength() - 1;
@@ -179,7 +181,7 @@ function _rsasign_signStringPSS(s, hashAlg, sLen) {
salt = String.fromCharCode.apply(String, salt);
}
- var H = hextorstr(hashFunc(rstrtoutf8('\x00\x00\x00\x00\x00\x00\x00\x00' + mHash + salt)));
+ var H = hextorstr(hashFunc(rstrtohex('\x00\x00\x00\x00\x00\x00\x00\x00' + mHash + salt)));
var PS = [];
for (i = 0; i < emLen - sLen - hLen - 2; i += 1) {
@@ -297,8 +299,8 @@ function _rsasign_verifyStringPSS(sMsg, biSig, hashAlg, sLen) {
return false;
}
- var hashFunc = _RSASIGN_HASHHEXFUNC[hashAlg];
- var hHash = hashFunc(sMsg);
+ var hashFunc = _RSASIGN_HASHHEXFUNC[hashAlg + 'Hex'];
+ var hHash = hashFunc(rstrtohex(sMsg));
var mHash = hextorstr(hHash);
var hLen = mHash.length;
var emBits = this.n.bitLength() - 1;
@@ -363,7 +365,7 @@ function _rsasign_verifyStringPSS(sMsg, biSig, hashAlg, sLen) {
throw "0x01 marker not found";
}
- return H === hextorstr(hashFunc(rstrtoutf8('\x00\x00\x00\x00\x00\x00\x00\x00' + mHash +
+ return H === hextorstr(hashFunc(rstrtohex('\x00\x00\x00\x00\x00\x00\x00\x00' + mHash +
String.fromCharCode.apply(String, DB.slice(-sLen)))));
}

0 comments on commit b591c21

Please sign in to comment.