@@ -12,7 +12,9 @@ const {
1212 generateKeyPair,
1313 generateKeyPairSync,
1414 publicEncrypt,
15- privateDecrypt
15+ privateDecrypt,
16+ sign,
17+ verify
1618} = require ( 'crypto' ) ;
1719const { promisify } = require ( 'util' ) ;
1820
@@ -41,13 +43,24 @@ function testEncryptDecrypt(publicKey, privateKey) {
4143
4244// Tests that a key pair can be used for signing / verification.
4345function testSignVerify ( publicKey , privateKey ) {
44- const message = 'Hello Node.js world!' ;
45- const signature = createSign ( 'SHA256' ) . update ( message )
46- . sign ( privateKey , 'hex' ) ;
47- for ( const key of [ publicKey , privateKey ] ) {
48- const okay = createVerify ( 'SHA256' ) . update ( message )
49- . verify ( key , signature , 'hex' ) ;
50- assert ( okay ) ;
46+ const message = Buffer . from ( 'Hello Node.js world!' ) ;
47+
48+ function oldSign ( algo , data , key ) {
49+ return createSign ( algo ) . update ( data ) . sign ( key ) ;
50+ }
51+
52+ function oldVerify ( algo , data , key , signature ) {
53+ return createVerify ( algo ) . update ( data ) . verify ( key , signature ) ;
54+ }
55+
56+ for ( const signFn of [ sign , oldSign ] ) {
57+ const signature = signFn ( 'SHA256' , message , privateKey ) ;
58+ for ( const verifyFn of [ verify , oldVerify ] ) {
59+ for ( const key of [ publicKey , privateKey ] ) {
60+ const okay = verifyFn ( 'SHA256' , message , key , signature ) ;
61+ assert ( okay ) ;
62+ }
63+ }
5164 }
5265}
5366
0 commit comments