11/* eslint-disable @typescript-eslint/no-unused-expressions */
22import { Ed , randomBytes , ab2str } from 'react-native-quick-crypto' ;
33import { Buffer } from '@craftzdog/react-native-buffer' ;
4- // import type {
5- // // KeyObject,
6- // // CFRGKeyPairType,
7- // // GenerateKeyPairCallback,
8- // GenerateKeyPairOptions,
9- // // KeyPairKey,
10- // } from 'react-native-quick-crypto';
114import { expect } from 'chai' ;
125import { test } from '../util' ;
136
@@ -90,20 +83,39 @@ test(SUITE, 'sign/verify - switched args does not verify', async () => {
9083} ) ;
9184
9285test ( SUITE , 'sign/verify - non-internally generated private key' , async ( ) => {
86+ const pub = Buffer . from (
87+ 'e106bf015ad54a64022295c7af2c35f9511eb37264a7722a9642eaac6c59a494' ,
88+ 'hex' ,
89+ ) ;
90+ const priv = Buffer . from (
91+ '5f27e170afc5091c4933d980c5fe86af997b91375115c6ee2c0fe4ea12400ed0' ,
92+ 'hex' ,
93+ ) ;
94+
95+ const ed2 = new Ed ( 'ed25519' , { } ) ;
96+ const signature = await ed2 . sign ( data1 . buffer , priv ) ;
97+ const verified = await ed2 . verify ( signature , data1 . buffer , pub ) ;
98+ expect ( verified ) . to . be . true ;
99+ } ) ;
100+
101+ test ( SUITE , 'sign/verify - bad signature' , async ( ) => {
93102 let ed1 : Ed | null = new Ed ( 'ed25519' , { } ) ;
94103 await ed1 . generateKeyPair ( ) ;
104+ const pub = ed1 . getPublicKey ( ) ;
95105 const priv = ed1 . getPrivateKey ( ) ;
96106 ed1 = null ;
97107
98108 const ed2 = new Ed ( 'ed25519' , { } ) ;
99109 const signature = await ed2 . sign ( data1 . buffer , priv ) ;
100- const verified = await ed2 . verify ( signature , data1 . buffer , priv ) ;
101- expect ( verified ) . to . be . true ;
110+ const signature2 = randomBytes ( 64 ) . buffer ;
111+ expect ( ab2str ( signature2 ) ) . not . to . equal ( ab2str ( signature ) ) ;
112+ const verified = await ed2 . verify ( signature2 , data1 . buffer , pub ) ;
113+ expect ( verified ) . to . be . false ;
102114} ) ;
103115
104116test (
105117 SUITE ,
106- 'sign/verify - bad non-internally generated private key' ,
118+ 'sign/verify - bad verify with private key, not public ' ,
107119 async ( ) => {
108120 let ed1 : Ed | null = new Ed ( 'ed25519' , { } ) ;
109121 await ed1 . generateKeyPair ( ) ;
@@ -112,9 +124,7 @@ test(
112124
113125 const ed2 = new Ed ( 'ed25519' , { } ) ;
114126 const signature = await ed2 . sign ( data1 . buffer , priv ) ;
115- const signature2 = randomBytes ( 64 ) . buffer ;
116- expect ( ab2str ( signature2 ) ) . not . to . equal ( ab2str ( signature ) ) ;
117- const verified = await ed2 . verify ( signature2 , data1 . buffer , priv ) ;
127+ const verified = await ed2 . verify ( signature , data1 . buffer , priv ) ;
118128 expect ( verified ) . to . be . false ;
119129 } ,
120130) ;
@@ -124,10 +134,11 @@ test(SUITE, 'sign/verify - Uint8Arrays', () => {
124134
125135 const ed1 = new Ed ( 'ed25519' , { } ) ;
126136 ed1 . generateKeyPairSync ( ) ;
137+ const pub = new Uint8Array ( ed1 . getPublicKey ( ) ) ;
127138 const priv = new Uint8Array ( ed1 . getPrivateKey ( ) ) ;
128139
129140 const ed2 = new Ed ( 'ed25519' , { } ) ;
130141 const signature = new Uint8Array ( ed2 . signSync ( encode ( data ) , priv ) ) ;
131- const verified = ed2 . verifySync ( signature , encode ( data ) , priv ) ;
142+ const verified = ed2 . verifySync ( signature , encode ( data ) , pub ) ;
132143 expect ( verified ) . to . be . true ;
133144} ) ;
0 commit comments