1818import java .io .FileReader ;
1919import java .io .IOException ;
2020
21+ import openpgp .Entity ;
22+ import openpgp .FileHints ;
2123import openpgp .KeyOptions ;
2224import openpgp .KeyPair ;
2325import openpgp .FastOpenPGP ;
2628
2729public class RNFastOpenPGPModule extends ReactContextBaseJavaModule {
2830
29- private final ReactApplicationContext reactContext ;
3031 private final FastOpenPGP instance ;
3132
3233 public RNFastOpenPGPModule (ReactApplicationContext reactContext ) {
3334 super (reactContext );
34- this .reactContext = reactContext ;
3535
3636 instance = Openpgp .newFastOpenPGP ();
3737 }
@@ -61,11 +61,12 @@ private void writeFile(byte[] data, String inputFile) throws IOException {
6161 }
6262
6363 @ 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 ) {
6565 new Thread (new Runnable () {
6666 public void run () {
6767 try {
68- String result = instance .decrypt (message , privateKey , passphrase );
68+ KeyOptions options = getKeyOptions (mapOptions );
69+ String result = instance .decrypt (message , privateKey , passphrase , options );
6970 promise .resolve (result );
7071 } catch (Exception e ) {
7172 promise .reject (e );
@@ -75,11 +76,12 @@ public void run() {
7576 }
7677
7778 @ 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 ) {
7980 new Thread (new Runnable () {
8081 public void run () {
8182 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 );
8385 writeFile (result , outputFile );
8486 promise .resolve (outputFile );
8587 } catch (Exception e ) {
@@ -90,11 +92,14 @@ public void run() {
9092 }
9193
9294 @ 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 ) {
9496 new Thread (new Runnable () {
9597 public void run () {
9698 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 );
98103 promise .resolve (result );
99104 } catch (Exception e ) {
100105 promise .reject (e );
@@ -104,11 +109,14 @@ public void run() {
104109 }
105110
106111 @ 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 ) {
108113 new Thread (new Runnable () {
109114 public void run () {
110115 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 );
112120 writeFile (result , outputFile );
113121 promise .resolve (outputFile );
114122 } catch (Exception e ) {
@@ -119,11 +127,12 @@ public void run() {
119127 }
120128
121129 @ 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 ) {
123131 new Thread (new Runnable () {
124132 public void run () {
125133 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 );
127136 promise .resolve (result );
128137 } catch (Exception e ) {
129138 promise .reject (e );
@@ -133,11 +142,12 @@ public void run() {
133142 }
134143
135144 @ 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 ) {
137146 new Thread (new Runnable () {
138147 public void run () {
139148 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 );
141151 promise .resolve (result );
142152 } catch (Exception e ) {
143153 promise .reject (e );
@@ -174,58 +184,6 @@ public void run() {
174184 }).start ();
175185 }
176186
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-
229187 @ ReactMethod
230188 public void decryptSymmetric (final String message , final String passphrase , final ReadableMap mapOptions , final Promise promise ) {
231189 new Thread (new Runnable () {
@@ -260,12 +218,13 @@ public void run() {
260218 }
261219
262220 @ 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 ) {
264222 new Thread (new Runnable () {
265223 public void run () {
266224 try {
225+ FileHints fileHints = getFileHints (mapFileHints );
267226 KeyOptions options = getKeyOptions (mapOptions );
268- String result = instance .encryptSymmetric (message , passphrase , options );
227+ String result = instance .encryptSymmetric (message , passphrase , fileHints , options );
269228 promise .resolve (result );
270229 } catch (Exception e ) {
271230 promise .reject (e );
@@ -275,12 +234,13 @@ public void run() {
275234 }
276235
277236 @ 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 ) {
279238 new Thread (new Runnable () {
280239 public void run () {
281240 try {
241+ FileHints fileHints = getFileHints (mapFileHints );
282242 KeyOptions options = getKeyOptions (mapOptions );
283- byte [] result = instance .encryptSymmetricBytes (readFile (inputFile ), passphrase , options );
243+ byte [] result = instance .encryptSymmetricBytes (readFile (inputFile ), passphrase , fileHints , options );
284244 writeFile (result , outputFile );
285245 promise .resolve (outputFile );
286246 } catch (Exception e ) {
@@ -308,4 +268,96 @@ public void run() {
308268 }
309269 }).start ();
310270 }
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+ }
311363}
0 commit comments