18
18
import java .io .FileReader ;
19
19
import java .io .IOException ;
20
20
21
+ import openpgp .Entity ;
22
+ import openpgp .FileHints ;
21
23
import openpgp .KeyOptions ;
22
24
import openpgp .KeyPair ;
23
25
import openpgp .FastOpenPGP ;
26
28
27
29
public class RNFastOpenPGPModule extends ReactContextBaseJavaModule {
28
30
29
- private final ReactApplicationContext reactContext ;
30
31
private final FastOpenPGP instance ;
31
32
32
33
public RNFastOpenPGPModule (ReactApplicationContext reactContext ) {
33
34
super (reactContext );
34
- this .reactContext = reactContext ;
35
35
36
36
instance = Openpgp .newFastOpenPGP ();
37
37
}
@@ -61,11 +61,12 @@ private void writeFile(byte[] data, String inputFile) throws IOException {
61
61
}
62
62
63
63
@ ReactMethod
64
- public void decrypt (final String message , final String privateKey , final String passphrase , final Promise promise ) {
64
+ public void decrypt (final String message , final String privateKey , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
65
65
new Thread (new Runnable () {
66
66
public void run () {
67
67
try {
68
- String result = instance .decrypt (message , privateKey , passphrase );
68
+ KeyOptions options = getKeyOptions (mapOptions );
69
+ String result = instance .decrypt (message , privateKey , passphrase , options );
69
70
promise .resolve (result );
70
71
} catch (Exception e ) {
71
72
promise .reject (e );
@@ -75,11 +76,12 @@ public void run() {
75
76
}
76
77
77
78
@ ReactMethod
78
- public void decryptFile (final String inputFile , final String outputFile , final String privateKey , final String passphrase , final Promise promise ) {
79
+ public void decryptFile (final String inputFile , final String outputFile , final String privateKey , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
79
80
new Thread (new Runnable () {
80
81
public void run () {
81
82
try {
82
- byte [] result = instance .decryptBytes (readFile (inputFile ), privateKey , passphrase );
83
+ KeyOptions options = getKeyOptions (mapOptions );
84
+ byte [] result = instance .decryptBytes (readFile (inputFile ), privateKey , passphrase , options );
83
85
writeFile (result , outputFile );
84
86
promise .resolve (outputFile );
85
87
} catch (Exception e ) {
@@ -90,11 +92,14 @@ public void run() {
90
92
}
91
93
92
94
@ ReactMethod
93
- public void encrypt (final String message , final String publicKey , final Promise promise ) {
95
+ public void encrypt (final String message , final String publicKey , final ReadableMap mapEntity , final ReadableMap mapFileHints , final ReadableMap mapOptions , final Promise promise ) {
94
96
new Thread (new Runnable () {
95
97
public void run () {
96
98
try {
97
- String result = instance .encrypt (message , publicKey );
99
+ KeyOptions options = getKeyOptions (mapOptions );
100
+ FileHints fileHints = getFileHints (mapFileHints );
101
+ Entity signedEntity = getEntity (mapEntity );
102
+ String result = instance .encrypt (message , publicKey , signedEntity , fileHints , options );
98
103
promise .resolve (result );
99
104
} catch (Exception e ) {
100
105
promise .reject (e );
@@ -104,11 +109,14 @@ public void run() {
104
109
}
105
110
106
111
@ ReactMethod
107
- public void encryptFile (final String inputFile , final String outputFile , final String publicKey , final Promise promise ) {
112
+ public void encryptFile (final String inputFile , final String outputFile , final String publicKey , final ReadableMap mapEntity , final ReadableMap mapFileHints , final ReadableMap mapOptions , final Promise promise ) {
108
113
new Thread (new Runnable () {
109
114
public void run () {
110
115
try {
111
- byte [] result = instance .encryptBytes (readFile (inputFile ), publicKey );
116
+ KeyOptions options = getKeyOptions (mapOptions );
117
+ FileHints fileHints = getFileHints (mapFileHints );
118
+ Entity signedEntity = getEntity (mapEntity );
119
+ byte [] result = instance .encryptBytes (readFile (inputFile ), publicKey , signedEntity , fileHints , options );
112
120
writeFile (result , outputFile );
113
121
promise .resolve (outputFile );
114
122
} catch (Exception e ) {
@@ -119,11 +127,12 @@ public void run() {
119
127
}
120
128
121
129
@ ReactMethod
122
- public void sign (final String message , final String publicKey , final String privateKey , final String passphrase , final Promise promise ) {
130
+ public void sign (final String message , final String publicKey , final String privateKey , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
123
131
new Thread (new Runnable () {
124
132
public void run () {
125
133
try {
126
- String result = instance .sign (message , publicKey , privateKey , passphrase );
134
+ KeyOptions options = getKeyOptions (mapOptions );
135
+ String result = instance .sign (message , publicKey , privateKey , passphrase , options );
127
136
promise .resolve (result );
128
137
} catch (Exception e ) {
129
138
promise .reject (e );
@@ -133,11 +142,12 @@ public void run() {
133
142
}
134
143
135
144
@ ReactMethod
136
- public void signFile (final String inputFile , final String publicKey , final String privateKey , final String passphrase , final Promise promise ) {
145
+ public void signFile (final String inputFile , final String publicKey , final String privateKey , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
137
146
new Thread (new Runnable () {
138
147
public void run () {
139
148
try {
140
- String result = instance .signBytesToString (readFile (inputFile ), publicKey , privateKey , passphrase );
149
+ KeyOptions options = getKeyOptions (mapOptions );
150
+ String result = instance .signBytesToString (readFile (inputFile ), publicKey , privateKey , passphrase , options );
141
151
promise .resolve (result );
142
152
} catch (Exception e ) {
143
153
promise .reject (e );
@@ -174,58 +184,6 @@ public void run() {
174
184
}).start ();
175
185
}
176
186
177
- private KeyOptions getKeyOptions (ReadableMap map ) {
178
- KeyOptions options = new KeyOptions ();
179
-
180
- if (map == null ) {
181
- return options ;
182
- }
183
- if (map .hasKey ("cipher" )) {
184
- options .setCipher (map .getString ("cipher" ));
185
- }
186
- if (map .hasKey ("compression" )) {
187
- options .setCompression (map .getString ("compression" ));
188
- }
189
- if (map .hasKey ("hash" )) {
190
- options .setHash (map .getString ("hash" ));
191
- }
192
- if (map .hasKey ("RSABits" )) {
193
- options .setRSABits (map .getInt ("RSABits" ));
194
- }
195
- if (map .hasKey ("compressionLevel" )) {
196
- options .setCompressionLevel (map .getInt ("compressionLevel" ));
197
- }
198
- return options ;
199
- }
200
-
201
- private Options getOptions (ReadableMap map ) {
202
- Options options = new Options ();
203
-
204
- if (map == null ) {
205
- return options ;
206
- }
207
- if (map .hasKey ("comment" )) {
208
- options .setComment (map .getString ("comment" ));
209
- }
210
- if (map .hasKey ("email" )) {
211
- options .setEmail (map .getString ("email" ));
212
- }
213
- if (map .hasKey ("name" )) {
214
- options .setName (map .getString ("name" ));
215
- }
216
- if (map .hasKey ("passphrase" )) {
217
- options .setPassphrase (map .getString ("passphrase" ));
218
- }
219
- if (map .hasKey ("keyOptions" )) {
220
- ReadableMap keyOptions = map .getMap ("keyOptions" );
221
- if (keyOptions != null ) {
222
- options .setKeyOptions (this .getKeyOptions (keyOptions ));
223
- }
224
- }
225
-
226
- return options ;
227
- }
228
-
229
187
@ ReactMethod
230
188
public void decryptSymmetric (final String message , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
231
189
new Thread (new Runnable () {
@@ -260,12 +218,13 @@ public void run() {
260
218
}
261
219
262
220
@ ReactMethod
263
- public void encryptSymmetric (final String message , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
221
+ public void encryptSymmetric (final String message , final String passphrase , final ReadableMap mapFileHints , final ReadableMap mapOptions , final Promise promise ) {
264
222
new Thread (new Runnable () {
265
223
public void run () {
266
224
try {
225
+ FileHints fileHints = getFileHints (mapFileHints );
267
226
KeyOptions options = getKeyOptions (mapOptions );
268
- String result = instance .encryptSymmetric (message , passphrase , options );
227
+ String result = instance .encryptSymmetric (message , passphrase , fileHints , options );
269
228
promise .resolve (result );
270
229
} catch (Exception e ) {
271
230
promise .reject (e );
@@ -275,12 +234,13 @@ public void run() {
275
234
}
276
235
277
236
@ ReactMethod
278
- public void encryptSymmetricFile (final String inputFile , final String outputFile , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
237
+ public void encryptSymmetricFile (final String inputFile , final String outputFile , final String passphrase , final ReadableMap mapFileHints , final ReadableMap mapOptions , final Promise promise ) {
279
238
new Thread (new Runnable () {
280
239
public void run () {
281
240
try {
241
+ FileHints fileHints = getFileHints (mapFileHints );
282
242
KeyOptions options = getKeyOptions (mapOptions );
283
- byte [] result = instance .encryptSymmetricBytes (readFile (inputFile ), passphrase , options );
243
+ byte [] result = instance .encryptSymmetricBytes (readFile (inputFile ), passphrase , fileHints , options );
284
244
writeFile (result , outputFile );
285
245
promise .resolve (outputFile );
286
246
} catch (Exception e ) {
@@ -308,4 +268,96 @@ public void run() {
308
268
}
309
269
}).start ();
310
270
}
271
+
272
+ private FileHints getFileHints (ReadableMap map ) {
273
+ FileHints options = new FileHints ();
274
+
275
+ if (map == null ) {
276
+ return options ;
277
+ }
278
+ if (map .hasKey ("fileName" )) {
279
+ options .setFileName (map .getString ("fileName" ));
280
+ }
281
+ if (map .hasKey ("isBinary" )) {
282
+ options .setIsBinary (map .getBoolean ("isBinary" ));
283
+ }
284
+ if (map .hasKey ("modTime" )) {
285
+ options .setModTime (map .getString ("modTime" ));
286
+ }
287
+ return options ;
288
+ }
289
+
290
+ private Entity getEntity (ReadableMap map ) {
291
+ Entity options = new Entity ();
292
+
293
+ if (map == null ) {
294
+ return null ;
295
+ }
296
+ if (map .hasKey ("publicKey" )) {
297
+ options .setPublicKey (map .getString ("publicKey" ));
298
+ }
299
+ if (map .hasKey ("privateKey" )) {
300
+ options .setPrivateKey (map .getString ("privateKey" ));
301
+ }
302
+ if (map .hasKey ("passphrase" )) {
303
+ options .setPassphrase (map .getString ("passphrase" ));
304
+ }
305
+ return options ;
306
+ }
307
+
308
+ private KeyOptions getKeyOptions (ReadableMap map ) {
309
+ KeyOptions options = new KeyOptions ();
310
+
311
+ if (map == null ) {
312
+ return options ;
313
+ }
314
+ if (map .hasKey ("cipher" )) {
315
+ options .setCipher (map .getString ("cipher" ));
316
+ }
317
+ if (map .hasKey ("compression" )) {
318
+ options .setCompression (map .getString ("compression" ));
319
+ }
320
+ if (map .hasKey ("hash" )) {
321
+ options .setHash (map .getString ("hash" ));
322
+ }
323
+ if (map .hasKey ("RSABits" )) {
324
+ options .setRSABits (map .getInt ("RSABits" ));
325
+ }
326
+ // this is just in case
327
+ if (map .hasKey ("rsaBits" )) {
328
+ options .setRSABits (map .getInt ("rsaBits" ));
329
+ }
330
+ if (map .hasKey ("compressionLevel" )) {
331
+ options .setCompressionLevel (map .getInt ("compressionLevel" ));
332
+ }
333
+ return options ;
334
+ }
335
+
336
+ private Options getOptions (ReadableMap map ) {
337
+ Options options = new Options ();
338
+
339
+ if (map == null ) {
340
+ return options ;
341
+ }
342
+ if (map .hasKey ("comment" )) {
343
+ options .setComment (map .getString ("comment" ));
344
+ }
345
+ if (map .hasKey ("email" )) {
346
+ options .setEmail (map .getString ("email" ));
347
+ }
348
+ if (map .hasKey ("name" )) {
349
+ options .setName (map .getString ("name" ));
350
+ }
351
+ if (map .hasKey ("passphrase" )) {
352
+ options .setPassphrase (map .getString ("passphrase" ));
353
+ }
354
+ if (map .hasKey ("keyOptions" )) {
355
+ ReadableMap keyOptions = map .getMap ("keyOptions" );
356
+ if (keyOptions != null ) {
357
+ options .setKeyOptions (this .getKeyOptions (keyOptions ));
358
+ }
359
+ }
360
+
361
+ return options ;
362
+ }
311
363
}
0 commit comments