@@ -6,9 +6,10 @@ import type { KeyPairKey } from '../../../../../react-native-quick-crypto/src/Ci
66import type { EncodingOptions } from '../../../../../react-native-quick-crypto/src/keys' ;
77// import { PrivateKey } from 'sscrypto/node';
88
9+ const message = 'Hello Node.js world!' ;
10+
911// Tests that a key pair can be used for encryption / decryption.
1012function testEncryptDecrypt ( publicKey : KeyPairKey , privateKey : KeyPairKey ) {
11- const message = 'Hello Node.js world!' ;
1213 const plaintext = Buffer . from ( message , 'utf8' ) ;
1314 for ( const key of [ publicKey , privateKey ] ) {
1415 // the EncodingOptions type is weird as shit, but it works.
@@ -62,25 +63,94 @@ describe('publicCipher', () => {
6263 // }
6364 // });
6465
65- it ( 'publicEncrypt/privateDecrypt' , ( ) => {
66- const { privateKey, publicKey } = crypto . generateKeyPairSync ( 'rsa' , {
67- modulusLength : 512 ,
68- publicKeyEncoding : {
69- type : 'pkcs1' ,
70- format : 'pem' ,
71- } ,
72- privateKeyEncoding : {
73- type : 'pkcs8' ,
74- format : 'pem' ,
75- } ,
76- } ) ;
66+ // it('publicEncrypt/privateDecrypt', () => {
67+ // const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
68+ // modulusLength: 512,
69+ // publicKeyEncoding: {
70+ // type: 'pkcs1',
71+ // format: 'pem',
72+ // },
73+ // privateKeyEncoding: {
74+ // type: 'pkcs8',
75+ // format: 'pem',
76+ // },
77+ // });
7778
78- testEncryptDecrypt ( publicKey , privateKey ) ;
79- } ) ;
79+ // testEncryptDecrypt(publicKey, privateKey);
80+ // });
81+
82+ // it('publicEncrypt/privateDecrypt with non-common exponent', () => {
83+ // const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
84+ // publicExponent: 3,
85+ // modulusLength: 512,
86+ // publicKeyEncoding: {
87+ // type: 'pkcs1',
88+ // format: 'pem',
89+ // },
90+ // privateKeyEncoding: {
91+ // type: 'pkcs8',
92+ // format: 'pem',
93+ // },
94+ // });
95+
96+ // testEncryptDecrypt(publicKey, privateKey);
97+ // });
98+
99+ // it('publicEncrypt/privateDecrypt with passphrase', () => {
100+ // const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
101+ // modulusLength: 4096,
102+ // publicKeyEncoding: {
103+ // type: 'spki',
104+ // format: 'pem',
105+ // },
106+ // privateKeyEncoding: {
107+ // type: 'pkcs8',
108+ // format: 'pem',
109+ // cipher: 'aes-256-cbc',
110+ // passphrase: 'top secret',
111+ // },
112+ // });
113+
114+ // const message = 'Hello RN world!';
115+ // const plaintext = Buffer.from(message, 'utf8');
116+ // const ciphertext = crypto.publicEncrypt(
117+ // publicKey as EncodingOptions,
118+ // plaintext,
119+ // );
120+ // const decrypted = crypto.privateDecrypt(
121+ // { key: privateKey, passphrase: 'top secret' },
122+ // ciphertext,
123+ // );
124+
125+ // expect(decrypted.toString('utf-8')).to.equal(message);
126+ // });
127+
128+ // it('passphrased private key without passphrase should throw', () => {
129+ // const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
130+ // modulusLength: 4096,
131+ // publicKeyEncoding: {
132+ // type: 'spki',
133+ // format: 'pem',
134+ // },
135+ // privateKeyEncoding: {
136+ // type: 'pkcs8',
137+ // format: 'pem',
138+ // cipher: 'aes-256-cbc',
139+ // passphrase: 'top secret',
140+ // },
141+ // });
142+
143+ // try {
144+ // testEncryptDecrypt(publicKey, privateKey);
145+ // assert.fail();
146+ // // eslint-disable-next-line @typescript-eslint/no-unused-vars
147+ // } catch (_e) {
148+ // // intentionally left blank
149+ // }
150+ // });
80151
81- it ( 'publicEncrypt/privateDecrypt with non-common exponent ' , ( ) => {
152+ it ( '#494 decryption error ' , ( ) => {
82153 const { privateKey, publicKey } = crypto . generateKeyPairSync ( 'rsa' , {
83- publicExponent : 3 ,
84154 modulusLength : 512 ,
85155 publicKeyEncoding : {
86156 type : 'pkcs1' ,
@@ -92,59 +162,23 @@ describe('publicCipher', () => {
92162 } ,
93163 } ) ;
94164
95- testEncryptDecrypt ( publicKey , privateKey ) ;
96- } ) ;
97-
98- it ( 'publicEncrypt/privateDecrypt with passphrase' , ( ) => {
99- const { privateKey, publicKey } = crypto . generateKeyPairSync ( 'rsa' , {
100- modulusLength : 4096 ,
101- publicKeyEncoding : {
102- type : 'spki' ,
103- format : 'pem' ,
165+ const encryptedMessage = crypto . publicEncrypt (
166+ {
167+ key : publicKey ,
168+ padding : crypto . constants . RSA_PKCS1_PADDING ,
104169 } ,
105- privateKeyEncoding : {
106- type : 'pkcs8' ,
107- format : 'pem' ,
108- cipher : 'aes-256-cbc' ,
109- passphrase : 'top secret' ,
110- } ,
111- } ) ;
112-
113- const message = 'Hello RN world!' ;
114- const plaintext = Buffer . from ( message , 'utf8' ) ;
115- const ciphertext = crypto . publicEncrypt (
116- publicKey as EncodingOptions ,
117- plaintext ,
170+ Buffer . from ( message , 'utf8' )
118171 ) ;
119- const decrypted = crypto . privateDecrypt (
120- { key : privateKey , passphrase : 'top secret' } ,
121- ciphertext ,
122- ) ;
123-
124- expect ( decrypted . toString ( 'utf-8' ) ) . to . equal ( message ) ;
125- } ) ;
126172
127- it ( 'passphrased private key without passphrase should throw' , ( ) => {
128- const { privateKey, publicKey } = crypto . generateKeyPairSync ( 'rsa' , {
129- modulusLength : 4096 ,
130- publicKeyEncoding : {
131- type : 'spki' ,
132- format : 'pem' ,
173+ const decryptedMessage = crypto . privateDecrypt (
174+ {
175+ key : privateKey ,
176+ padding : crypto . constants . RSA_PKCS1_PADDING ,
133177 } ,
134- privateKeyEncoding : {
135- type : 'pkcs8' ,
136- format : 'pem' ,
137- cipher : 'aes-256-cbc' ,
138- passphrase : 'top secret' ,
139- } ,
140- } ) ;
178+ encryptedMessage
179+ ) ;
141180
142- try {
143- testEncryptDecrypt ( publicKey , privateKey ) ;
144- assert . fail ( ) ;
145- // eslint-disable-next-line @typescript-eslint/no-unused-vars
146- } catch ( _e ) {
147- // intentionally left blank
148- }
149- } ) ;
181+ console . log ( encryptedMessage . toString ( 'hex' ) ) ;
182+ expect ( decryptedMessage . toString ( 'utf8' ) ) . to . equal ( message ) ;
183+ } )
150184} ) ;
0 commit comments