diff --git a/package.json b/package.json index 715800f..1405451 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-secure-key-store", - "version": "1.5.1", + "version": "1.5.2", "description": "Cordova plugin for securely saving keys, passwords or strings on devices.", "cordova": { "id": "cordova-plugin-secure-key-store", diff --git a/plugin.xml b/plugin.xml index be4d5b5..0f7eb4e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ diff --git a/src/android/SecureKeyStore.java b/src/android/SecureKeyStore.java index 03fb0ab..cac365a 100644 --- a/src/android/SecureKeyStore.java +++ b/src/android/SecureKeyStore.java @@ -52,7 +52,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo String alias = args.getString(0); this.removeKeyFile(alias, callbackContext); return true; - } + } return false; } @@ -68,14 +68,9 @@ private void encrypt(String alias, String input, CallbackContext callbackContext Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); end.add(Calendar.YEAR, 1); - KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder( - getContext()) - .setAlias(alias) - .setSubject(new X500Principal("CN=" + alias)) - .setSerialNumber(BigInteger.ONE) - .setStartDate(start.getTime()) - .setEndDate(end.getTime()) - .build(); + KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext()).setAlias(alias) + .setSubject(new X500Principal("CN=" + alias)).setSerialNumber(BigInteger.ONE) + .setStartDate(start.getTime()).setEndDate(end.getTime()).build(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", getKeyStore()); generator.initialize(spec); @@ -95,8 +90,7 @@ private void encrypt(String alias, String input, CallbackContext callbackContext Cipher cipher = Cipher.getInstance(Constants.RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - CipherOutputStream cipherOutputStream = new CipherOutputStream( - outputStream, cipher); + CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher); cipherOutputStream.write(input.getBytes("UTF-8")); cipherOutputStream.close(); byte[] vals = outputStream.toByteArray(); @@ -107,8 +101,8 @@ private void encrypt(String alias, String input, CallbackContext callbackContext callbackContext.success("key created and stored successfully"); } catch (Exception e) { - Log.e(Constants.TAG, "Exception: " + e.getMessage()); - callbackContext.error("Api-level:" + Build.VERSION.SDK_INT + "\nException: " + e.getMessage()); + Log.e(Constants.TAG, "Exception: " + e.getMessage()); + callbackContext.error("{\"code\":9,\"api-level\":" + Build.VERSION.SDK_INT + ",\"class\":" + e.getMessage() + "}"); } } @@ -121,7 +115,6 @@ private void decrypt(String alias, CallbackContext callbackContext) { keyStore.load(null); PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, null); - Cipher output = Cipher.getInstance(Constants.RSA_ALGORITHM); output.init(Cipher.DECRYPT_MODE, privateKey); CipherInputStream cipherInputStream = new CipherInputStream( @@ -130,10 +123,10 @@ private void decrypt(String alias, CallbackContext callbackContext) { ArrayList values = new ArrayList(); int nextByte; while ((nextByte = cipherInputStream.read()) != -1) { - values.add((byte)nextByte); + values.add((byte) nextByte); } byte[] bytes = new byte[values.size()]; - for(int i = 0; i < bytes.length; i++) { + for (int i = 0; i < bytes.length; i++) { bytes[i] = values.get(i).byteValue(); } @@ -141,8 +134,8 @@ private void decrypt(String alias, CallbackContext callbackContext) { callbackContext.success(finalText); } catch (Exception e) { - Log.e(Constants.TAG, "Exception: " + e.getMessage()); - callbackContext.error("Api-level:" + Build.VERSION.SDK_INT + "\nException: " + e.getMessage()); + Log.e(Constants.TAG, "Exception: " + e.getMessage()); + callbackContext.error("{\"code\":1,\"api-level\":" + Build.VERSION.SDK_INT + ",\"class\":" + e.getMessage() + "}"); } } @@ -153,10 +146,10 @@ private void removeKeyFile(String alias, CallbackContext callbackContext) { callbackContext.success("keys removed successfully"); } catch (Exception e) { - Log.e(Constants.TAG, "Exception: " + e.getMessage()); - callbackContext.error("Api-level:" + Build.VERSION.SDK_INT + "\nException: " + e.getMessage()); + Log.e(Constants.TAG, "Exception: " + e.getMessage()); + callbackContext.error("{\"code\":6,\"api-level\":" + Build.VERSION.SDK_INT + ",\"class\":" + e.getMessage() + "}"); } - } + } private Context getContext() { return cordova.getActivity().getApplicationContext(); @@ -166,13 +159,11 @@ private String getKeyStore() { try { KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_1); return Constants.KEYSTORE_PROVIDER_1; - } - catch (Exception err) { + } catch (Exception err) { try { KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_2); return Constants.KEYSTORE_PROVIDER_2; - } - catch (Exception e) { + } catch (Exception e) { return Constants.KEYSTORE_PROVIDER_3; } }