@@ -31,7 +31,10 @@ module.exports.makeScheme = function (key, options) {
31
31
}
32
32
33
33
Scheme . prototype . sign = function ( buffer ) {
34
- var encoded = this . emsa_pss_encode ( buffer , this . key . keySize - 1 ) ;
34
+ var mHash = crypt . createHash ( this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ) ;
35
+ mHash . update ( buffer ) ;
36
+
37
+ var encoded = this . emsa_pss_encode ( mHash . digest ( ) , this . key . keySize - 1 ) ;
35
38
var res = this . key . $doPrivate ( new BigInteger ( encoded ) ) . toBuffer ( this . key . encryptedDataLength ) ;
36
39
return res ;
37
40
} ;
@@ -45,17 +48,20 @@ module.exports.makeScheme = function (key, options) {
45
48
var emLen = Math . ceil ( ( this . key . keySize - 1 ) / 8 ) ;
46
49
var m = this . key . $doPublic ( signature ) . toBuffer ( emLen ) ;
47
50
48
- return this . emsa_pss_verify ( buffer , m , this . key . keySize - 1 ) ;
51
+ var mHash = crypt . createHash ( this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ) ;
52
+ mHash . update ( buffer ) ;
53
+
54
+ return this . emsa_pss_verify ( mHash . digest ( ) , m , this . key . keySize - 1 ) ;
49
55
} ;
50
56
51
57
/*
52
58
* https://tools.ietf.org/html/rfc3447#section-9.1.1
53
59
*
54
- * M [Buffer] Message to encode
60
+ * mHash [Buffer] Hashed message to encode
55
61
* emBits [uint] Maximum length of output in bits. Must be at least 8hLen + 8sLen + 9 (hLen = Hash digest length in bytes | sLen = length of salt in bytes)
56
62
* @returns {Buffer } The encoded message
57
63
*/
58
- Scheme . prototype . emsa_pss_encode = function ( M , emBits ) {
64
+ Scheme . prototype . emsa_pss_encode = function ( mHash , emBits ) {
59
65
var hash = this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ;
60
66
var mgf = this . options . signingSchemeOptions . mgf || OAEP . eme_oaep_mgf1 ;
61
67
var sLen = this . options . signingSchemeOptions . saltLength || DEFAULT_SALT_LENGTH ;
@@ -70,10 +76,6 @@ module.exports.makeScheme = function (key, options) {
70
76
) ;
71
77
}
72
78
73
- var mHash = crypt . createHash ( hash ) ;
74
- mHash . update ( M ) ;
75
- mHash = mHash . digest ( ) ;
76
-
77
79
var salt = crypt . randomBytes ( sLen ) ;
78
80
79
81
var Mapostrophe = new Buffer ( 8 + hLen + sLen ) ;
@@ -116,12 +118,12 @@ module.exports.makeScheme = function (key, options) {
116
118
/*
117
119
* https://tools.ietf.org/html/rfc3447#section-9.1.2
118
120
*
119
- * M [Buffer] Message
121
+ * mHash [Buffer] Hashed message
120
122
* EM [Buffer] Signature
121
123
* emBits [uint] Length of EM in bits. Must be at least 8hLen + 8sLen + 9 to be a valid signature. (hLen = Hash digest length in bytes | sLen = length of salt in bytes)
122
124
* @returns {Boolean } True if signature(EM) matches message(M)
123
125
*/
124
- Scheme . prototype . emsa_pss_verify = function ( M , EM , emBits ) {
126
+ Scheme . prototype . emsa_pss_verify = function ( mHash , EM , emBits ) {
125
127
var hash = this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ;
126
128
var mgf = this . options . signingSchemeOptions . mgf || OAEP . eme_oaep_mgf1 ;
127
129
var sLen = this . options . signingSchemeOptions . saltLength || DEFAULT_SALT_LENGTH ;
@@ -172,10 +174,6 @@ module.exports.makeScheme = function (key, options) {
172
174
173
175
var salt = DB . slice ( DB . length - sLen ) ;
174
176
175
- var mHash = crypt . createHash ( hash ) ;
176
- mHash . update ( M ) ;
177
- mHash = mHash . digest ( ) ;
178
-
179
177
var Mapostrophe = new Buffer ( 8 + hLen + sLen ) ;
180
178
Mapostrophe . fill ( 0 , 0 , 8 ) ;
181
179
mHash . copy ( Mapostrophe , 8 ) ;
@@ -189,4 +187,4 @@ module.exports.makeScheme = function (key, options) {
189
187
} ;
190
188
191
189
return new Scheme ( key , options ) ;
192
- } ;
190
+ } ;
0 commit comments