@@ -31,7 +31,10 @@ module.exports.makeScheme = function (key, options) {
3131 }
3232
3333 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 ) ;
3538 var res = this . key . $doPrivate ( new BigInteger ( encoded ) ) . toBuffer ( this . key . encryptedDataLength ) ;
3639 return res ;
3740 } ;
@@ -45,17 +48,20 @@ module.exports.makeScheme = function (key, options) {
4548 var emLen = Math . ceil ( ( this . key . keySize - 1 ) / 8 ) ;
4649 var m = this . key . $doPublic ( signature ) . toBuffer ( emLen ) ;
4750
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 ) ;
4955 } ;
5056
5157 /*
5258 * https://tools.ietf.org/html/rfc3447#section-9.1.1
5359 *
54- * M [Buffer] Message to encode
60+ * mHash [Buffer] Hashed message to encode
5561 * 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)
5662 * @returns {Buffer } The encoded message
5763 */
58- Scheme . prototype . emsa_pss_encode = function ( M , emBits ) {
64+ Scheme . prototype . emsa_pss_encode = function ( mHash , emBits ) {
5965 var hash = this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ;
6066 var mgf = this . options . signingSchemeOptions . mgf || OAEP . eme_oaep_mgf1 ;
6167 var sLen = this . options . signingSchemeOptions . saltLength || DEFAULT_SALT_LENGTH ;
@@ -70,10 +76,6 @@ module.exports.makeScheme = function (key, options) {
7076 ) ;
7177 }
7278
73- var mHash = crypt . createHash ( hash ) ;
74- mHash . update ( M ) ;
75- mHash = mHash . digest ( ) ;
76-
7779 var salt = crypt . randomBytes ( sLen ) ;
7880
7981 var Mapostrophe = new Buffer ( 8 + hLen + sLen ) ;
@@ -116,12 +118,12 @@ module.exports.makeScheme = function (key, options) {
116118 /*
117119 * https://tools.ietf.org/html/rfc3447#section-9.1.2
118120 *
119- * M [Buffer] Message
121+ * mHash [Buffer] Hashed message
120122 * EM [Buffer] Signature
121123 * 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)
122124 * @returns {Boolean } True if signature(EM) matches message(M)
123125 */
124- Scheme . prototype . emsa_pss_verify = function ( M , EM , emBits ) {
126+ Scheme . prototype . emsa_pss_verify = function ( mHash , EM , emBits ) {
125127 var hash = this . options . signingSchemeOptions . hash || DEFAULT_HASH_FUNCTION ;
126128 var mgf = this . options . signingSchemeOptions . mgf || OAEP . eme_oaep_mgf1 ;
127129 var sLen = this . options . signingSchemeOptions . saltLength || DEFAULT_SALT_LENGTH ;
@@ -172,10 +174,6 @@ module.exports.makeScheme = function (key, options) {
172174
173175 var salt = DB . slice ( DB . length - sLen ) ;
174176
175- var mHash = crypt . createHash ( hash ) ;
176- mHash . update ( M ) ;
177- mHash = mHash . digest ( ) ;
178-
179177 var Mapostrophe = new Buffer ( 8 + hLen + sLen ) ;
180178 Mapostrophe . fill ( 0 , 0 , 8 ) ;
181179 mHash . copy ( Mapostrophe , 8 ) ;
@@ -189,4 +187,4 @@ module.exports.makeScheme = function (key, options) {
189187 } ;
190188
191189 return new Scheme ( key , options ) ;
192- } ;
190+ } ;
0 commit comments