Skip to content

Commit

Permalink
added error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeep1991singh committed Jun 30, 2017
1 parent 9e10c1a commit 3d09bef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-secure-key-store"
version="1.4.4"
version="1.5.2"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">

Expand Down
41 changes: 16 additions & 25 deletions src/android/SecureKeyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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() + "}");
}

}
Expand All @@ -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(
Expand All @@ -130,19 +123,19 @@ private void decrypt(String alias, CallbackContext callbackContext) {
ArrayList<Byte> values = new ArrayList<Byte>();
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();
}

String finalText = new String(bytes, 0, bytes.length, "UTF-8");
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() + "}");
}
}

Expand All @@ -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();
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 3d09bef

Please sign in to comment.