1
1
/* eslint-disable @typescript-eslint/no-unused-expressions */
2
2
import { Ed , randomBytes , ab2str } from 'react-native-quick-crypto' ;
3
+ import { Buffer } from '@craftzdog/react-native-buffer' ;
3
4
// import type {
4
5
// // KeyObject,
5
6
// // CFRGKeyPairType,
@@ -12,6 +13,10 @@ import { test } from '../util';
12
13
13
14
const SUITE = 'ed25519' ;
14
15
16
+ const encoder = new TextEncoder ( ) ;
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ const encode = ( data : any ) : Uint8Array => encoder . encode ( JSON . stringify ( data ) ) ;
19
+
15
20
/*
16
21
const jwkOptions: GenerateKeyPairOptions = {
17
22
publicKeyEncoding: {
@@ -75,25 +80,30 @@ test(SUITE, 'sign/verify - bad signature does not verify', async () => {
75
80
expect ( verified ) . to . be . false ;
76
81
} ) ;
77
82
78
- test (
79
- SUITE ,
80
- 'sign/verify with non-internally generated private key' ,
81
- async ( ) => {
82
- let ed1 : Ed | null = new Ed ( 'ed25519' , { } ) ;
83
- await ed1 . generateKeyPair ( ) ;
84
- const priv = ed1 . getPrivateKey ( ) ;
85
- ed1 = null ;
83
+ test ( SUITE , 'sign/verify - switched args does not verify' , async ( ) => {
84
+ const ed = new Ed ( 'ed25519' , { } ) ;
85
+ await ed . generateKeyPair ( ) ;
86
+ const signature = await ed . sign ( data1 . buffer ) ;
87
+ // verify(message, signature) is switched
88
+ const verified = await ed . verify ( data1 . buffer , signature ) ;
89
+ expect ( verified ) . to . be . false ;
90
+ } ) ;
86
91
87
- const ed2 = new Ed ( 'ed25519' , { } ) ;
88
- const signature = await ed2 . sign ( data1 . buffer , priv ) ;
89
- const verified = await ed2 . verify ( signature , data1 . buffer , priv ) ;
90
- expect ( verified ) . to . be . true ;
91
- } ,
92
- ) ;
92
+ test ( SUITE , 'sign/verify - non-internally generated private key' , async ( ) => {
93
+ let ed1 : Ed | null = new Ed ( 'ed25519' , { } ) ;
94
+ await ed1 . generateKeyPair ( ) ;
95
+ const priv = ed1 . getPrivateKey ( ) ;
96
+ ed1 = null ;
97
+
98
+ const ed2 = new Ed ( 'ed25519' , { } ) ;
99
+ const signature = await ed2 . sign ( data1 . buffer , priv ) ;
100
+ const verified = await ed2 . verify ( signature , data1 . buffer , priv ) ;
101
+ expect ( verified ) . to . be . true ;
102
+ } ) ;
93
103
94
104
test (
95
105
SUITE ,
96
- 'sign/verify with bad non-internally generated private key' ,
106
+ 'sign/verify - bad non-internally generated private key' ,
97
107
async ( ) => {
98
108
let ed1 : Ed | null = new Ed ( 'ed25519' , { } ) ;
99
109
await ed1 . generateKeyPair ( ) ;
@@ -108,3 +118,16 @@ test(
108
118
expect ( verified ) . to . be . false ;
109
119
} ,
110
120
) ;
121
+
122
+ test ( SUITE , 'sign/verify - Uint8Arrays' , ( ) => {
123
+ const data = { b : 'world' , a : 'hello' } ;
124
+
125
+ const ed1 = new Ed ( 'ed25519' , { } ) ;
126
+ ed1 . generateKeyPairSync ( ) ;
127
+ const priv = new Uint8Array ( ed1 . getPrivateKey ( ) ) ;
128
+
129
+ const ed2 = new Ed ( 'ed25519' , { } ) ;
130
+ const signature = new Uint8Array ( ed2 . signSync ( encode ( data ) , priv ) ) ;
131
+ const verified = ed2 . verifySync ( signature , encode ( data ) , priv ) ;
132
+ expect ( verified ) . to . be . true ;
133
+ } ) ;
0 commit comments