Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

squid:S1192 - String literals should not be duplicated #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions Safe/src/main/java/org/openintents/safe/CryptoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
* @author Steven Osborn - http://steven.bitsetters.com
*/
public class CryptoHelper {
private static final String GENERATE_MASTER_KEY = "generateMasterKey(): ";
private static final String SET_PASSWORD = "setPassword(): ";
private static final String ENCRYPT = "encrypt(): ";
private static final String MUST_CALL_SET_PASSWORD_BEFORE_RUNNING_DECRYPT = "Must call setPassword before running decrypt.";
private static final String DECRYPT = "decrypt(): ";
private static final String ENCRYPT_WITH_SESSION_KEY = "encryptWithSessionKey(): ";
private static final String ENCRYPT_WITH_SESSION_KEY2 = "encryptWithSessionKey2(): ";
private static final String FILE_NOT_FOUND = "File not found";
private static final String DECRYPT_INPUT_FROM = "Decrypt: Input from ";
private static final String IO_EXCEPTION = "IOException";

private static final boolean debug = false;
private static String TAG = "CryptoHelper";
Expand Down Expand Up @@ -179,7 +189,7 @@ public static String generateMasterKey() throws NoSuchAlgorithmException {
SecretKey genDesKey = keygen.generateKey();
return toHexString(genDesKey.getEncoded());
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "generateMasterKey(): " + e.toString());
Log.e(TAG, GENERATE_MASTER_KEY + e.toString());
throw e;
}
}
Expand Down Expand Up @@ -273,7 +283,7 @@ public void setPassword(String pass) {
.getInstance(algorithm, "BC");
} catch (InvalidKeySpecException | NoSuchAlgorithmException
| NoSuchPaddingException | NoSuchProviderException e) {
Log.e(TAG, "setPassword(): " + e.toString());
Log.e(TAG, SET_PASSWORD + e.toString());
}

// Every time we set a new password, also the session key changes:
Expand Down Expand Up @@ -359,7 +369,7 @@ public String encrypt(String plaintext) throws CryptoHelperException {
status = true;
} catch (IllegalBlockSizeException | BadPaddingException
| InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "encrypt(): " + e.toString());
Log.e(TAG, ENCRYPT + e.toString());
}

String stringCiphertext = toHexString(ciphertext);
Expand All @@ -376,7 +386,7 @@ public String encrypt(String plaintext) throws CryptoHelperException {
public String decrypt(String ciphertext) throws CryptoHelperException {
status = false; // assume failure
if (password == null) {
String msg = "Must call setPassword before running decrypt.";
String msg = MUST_CALL_SET_PASSWORD_BEFORE_RUNNING_DECRYPT;
throw new CryptoHelperException(msg);
}
if (salt == null) {
Expand All @@ -396,7 +406,7 @@ public String decrypt(String ciphertext) throws CryptoHelperException {
status = true;
} catch (IllegalBlockSizeException | BadPaddingException
| InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
}

return new String(plaintext);
Expand Down Expand Up @@ -444,7 +454,7 @@ public String encryptWithSessionKey(String plaintext) throws CryptoHelperExcepti
sessionKeyEncoded = sessionKey.getEncoded();
sessionKeyString = new String(sessionKeyEncoded);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "generateMasterKey(): " + e.toString());
Log.e(TAG, GENERATE_MASTER_KEY + e.toString());
}

// Convert this to a Pbe key
Expand All @@ -453,7 +463,7 @@ public String encryptWithSessionKey(String plaintext) throws CryptoHelperExcepti
try {
sessionPbeKey = keyFac.generateSecret(sessionPbeKeySpec);
} catch (InvalidKeySpecException e) {
Log.e(TAG, "setPassword(): " + e.toString());
Log.e(TAG, SET_PASSWORD + e.toString());
}

// Encrypt the session key using the master key
Expand All @@ -462,7 +472,7 @@ public String encryptWithSessionKey(String plaintext) throws CryptoHelperExcepti
cipherSessionKey = pbeCipher.doFinal(sessionKeyEncoded);
} catch (IllegalBlockSizeException | BadPaddingException
| InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "encryptWithSessionKey(): " + e.toString());
Log.e(TAG, ENCRYPT_WITH_SESSION_KEY + e.toString());
}

// Now encrypt the text using the session key
Expand All @@ -472,7 +482,7 @@ public String encryptWithSessionKey(String plaintext) throws CryptoHelperExcepti
status = true;
} catch (IllegalBlockSizeException | BadPaddingException
| InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "encryptWithSessionKey2(): " + e.toString());
Log.e(TAG, ENCRYPT_WITH_SESSION_KEY2 + e.toString());
}

String stringCipherVersion = "A";
Expand Down Expand Up @@ -505,7 +515,7 @@ public String encryptWithSessionKey(String plaintext) throws CryptoHelperExcepti
public String decryptWithSessionKey(String ciphertext) throws CryptoHelperException {
status = false; // assume failure
if (password == null) {
String msg = "Must call setPassword before running decrypt.";
String msg = MUST_CALL_SET_PASSWORD_BEFORE_RUNNING_DECRYPT;
throw new CryptoHelperException(msg);
}

Expand Down Expand Up @@ -539,7 +549,7 @@ public String decryptWithSessionKey(String ciphertext) throws CryptoHelperExcept
byteSessionKey = pbeCipher.doFinal(byteCipherSessionKey);
status = true;
} catch (IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
}

// Convert the session key into a Pbe key
Expand All @@ -549,7 +559,7 @@ public String decryptWithSessionKey(String ciphertext) throws CryptoHelperExcept
try {
sessionPbeKey = keyFac.generateSecret(sessionPbeKeySpec);
} catch (InvalidKeySpecException e) {
Log.e(TAG, "setPassword(): " + e.toString());
Log.e(TAG, SET_PASSWORD + e.toString());
}

// Use the session key to decrypt the text
Expand All @@ -561,7 +571,7 @@ public String decryptWithSessionKey(String ciphertext) throws CryptoHelperExcept
plaintext = pbeCipher.doFinal(byteCiphertext);
status = true;
} catch (IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
}

return new String(plaintext);
Expand Down Expand Up @@ -616,7 +626,7 @@ public Uri encryptFileWithSessionKey(ContentResolver contentResolver, Uri fileUr
sessionKeyEncoded = sessionKey.getEncoded();
// sessionKeyString = new String(sessionKeyEncoded);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "generateMasterKey(): " + e.toString());
Log.e(TAG, GENERATE_MASTER_KEY + e.toString());
return null;
}

Expand All @@ -626,7 +636,7 @@ public Uri encryptFileWithSessionKey(ContentResolver contentResolver, Uri fileUr
cipherSessionKey = pbeCipher.doFinal(sessionKeyEncoded);
status = true;
} catch (IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) {
Log.e(TAG, "encryptWithSessionKey(): " + e.toString());
Log.e(TAG, ENCRYPT_WITH_SESSION_KEY + e.toString());
}
if (!status) {
return null;
Expand Down Expand Up @@ -688,7 +698,7 @@ public Uri encryptFileWithSessionKey(ContentResolver contentResolver, Uri fileUr
Log.e(TAG, "Error encrypting file", e);
}
} catch (FileNotFoundException e) {
Log.e(TAG, "File not found", e);
Log.e(TAG, FILE_NOT_FOUND, e);
} catch (IOException e) {
Log.e(TAG, "IO Exception", e);
}
Expand Down Expand Up @@ -748,15 +758,15 @@ public Uri decryptFileWithSessionKey(Context ctx, Uri fileUri) throws CryptoHelp
inputPath = fileUri.getPath();
is = new java.io.FileInputStream(inputPath);
if (debug) {
Log.d(TAG, "Decrypt: Input from " + inputPath);
Log.d(TAG, DECRYPT_INPUT_FROM + inputPath);
}
if (inputPath.endsWith(OISAFE_EXTENSION)) {
outputPath = inputPath.substring(0, inputPath.length() - OISAFE_EXTENSION.length());
}
} else {
is = contentResolver.openInputStream(fileUri);
if (debug) {
Log.d(TAG, "Decrypt: Input from " + fileUri.toString());
Log.d(TAG, DECRYPT_INPUT_FROM + fileUri.toString());
}
}

Expand All @@ -777,9 +787,9 @@ public Uri decryptFileWithSessionKey(Context ctx, Uri fileUri) throws CryptoHelp
os.close();

} catch (FileNotFoundException e) {
Log.e(TAG, "File not found", e);
Log.e(TAG, FILE_NOT_FOUND, e);
} catch (IOException e) {
Log.e(TAG, "IOException", e);
Log.e(TAG, IO_EXCEPTION, e);
} catch (IllegalArgumentException e) {
Log.e(TAG, "IllegalArgumentException", e);
}
Expand Down Expand Up @@ -830,12 +840,12 @@ public Uri decryptFileWithSessionKeyThroughContentProvider(Context ctx, Uri file
if (fileUri.getScheme().equals("file")) {
is = new java.io.FileInputStream(fileUri.getPath());
if (debug) {
Log.d(TAG, "Decrypt: Input from " + fileUri.getPath());
Log.d(TAG, DECRYPT_INPUT_FROM + fileUri.getPath());
}
} else {
is = contentResolver.openInputStream(fileUri);
if (debug) {
Log.d(TAG, "Decrypt: Input from " + fileUri.toString());
Log.d(TAG, DECRYPT_INPUT_FROM + fileUri.toString());
}
}
FileOutputStream os = null;
Expand Down Expand Up @@ -873,9 +883,9 @@ public Uri decryptFileWithSessionKeyThroughContentProvider(Context ctx, Uri file
os.close();

} catch (FileNotFoundException e) {
Log.e(TAG, "File not found", e);
Log.e(TAG, FILE_NOT_FOUND, e);
} catch (IOException e) {
Log.e(TAG, "IOException", e);
Log.e(TAG, IO_EXCEPTION, e);
}

if (result == false) {
Expand Down Expand Up @@ -904,7 +914,7 @@ public boolean decryptStreamWithSessionKey(InputStream is, OutputStream os) thro
}
status = false; // assume failure
if (password == null) {
String msg = "Must call setPassword before running decrypt.";
String msg = MUST_CALL_SET_PASSWORD_BEFORE_RUNNING_DECRYPT;
throw new CryptoHelperException(msg);
}

Expand Down Expand Up @@ -966,13 +976,13 @@ public boolean decryptStreamWithSessionKey(InputStream is, OutputStream os) thro
byteSessionKey = pbeCipher.doFinal(byteCipherSessionKey);
status = true;
} catch (IllegalBlockSizeException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
} catch (BadPaddingException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
} catch (InvalidKeyException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
} catch (InvalidAlgorithmParameterException e) {
Log.e(TAG, "decrypt(): " + e.toString());
Log.e(TAG, DECRYPT + e.toString());
}

// Now decrypt the message
Expand Down Expand Up @@ -1012,7 +1022,7 @@ public boolean decryptStreamWithSessionKey(InputStream is, OutputStream os) thro
}

} catch (IOException e) {
Log.e(TAG, "IOException", e);
Log.e(TAG, IO_EXCEPTION, e);
}

return status;
Expand Down
Loading