Skip to content

Commit

Permalink
Rename IEncrypitonKeyProvider to IKeyStoreEncryptionKeyProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Jan 23, 2024
1 parent 58ffb32 commit b2a8da9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/commcare/util/EncryptionKeyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class EncryptionKeyHelper {
public static final String CC_KEY_ALGORITHM_RSA = "RSA";
public static final String CC_IN_MEMORY_ENCRYPTION_KEY_ALIAS = "cc-in-memory-encryption-key-alias";

private static IEncryptionKeyProvider encryptionKeyProvider = EncryptionKeyServiceProvider.getInstance().serviceImpl();
private static IKeyStoreEncryptionKeyProvider keyStoreEncryptionKeyProvider = KeyStoreEncryptionKeyServiceProvider.getInstance().serviceImpl();

/**
* Converts a Base64 encoded key into a SecretKey depending on the algorithm
Expand Down Expand Up @@ -46,16 +46,16 @@ public static EncryptionKeyAndTransformation getKey(String base64encodedKey)
}

public static boolean isKeyStoreAvailable() {
return encryptionKeyProvider != null &&
Security.getProvider(encryptionKeyProvider.getKeyStoreName()) != null;
return keyStoreEncryptionKeyProvider != null &&
Security.getProvider(keyStoreEncryptionKeyProvider.getKeyStoreName()) != null;
}

private static KeyStore keystoreSingleton = null;

private static KeyStore getKeyStore() throws EncryptionKeyException {
if (keystoreSingleton == null) {
try {
keystoreSingleton = KeyStore.getInstance(encryptionKeyProvider.getKeyStoreName());
keystoreSingleton = KeyStore.getInstance(keyStoreEncryptionKeyProvider.getKeyStoreName());
keystoreSingleton.load(null);
} catch (KeyStoreException | CertificateException | IOException |
NoSuchAlgorithmException e) {
Expand Down Expand Up @@ -92,13 +92,13 @@ public static EncryptionKeyAndTransformation retrieveKeyFromKeyStore(String keyA
key = ((KeyStore.SecretKeyEntry)keyEntry).getSecretKey();
}
} else {
key = encryptionKeyProvider.generateCryptographicKeyInKeyStore(keyAlias, cryptographicOperation);
key = keyStoreEncryptionKeyProvider.generateCryptographicKeyInKeyStore(keyAlias, cryptographicOperation);
}
} catch (KeyStoreException| NoSuchAlgorithmException | UnrecoverableEntryException e) {
throw new EncryptionKeyException("Error retrieving key from KeyStore: ", e);
}
if (key != null) {
return new EncryptionKeyAndTransformation(key, encryptionKeyProvider.getTransformationString());
return new EncryptionKeyAndTransformation(key, keyStoreEncryptionKeyProvider.getTransformationString());
} else {
throw new EncryptionKeyException("Key couldn't be found in the keyStore");
}
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/commcare/util/EncryptionKeyServiceProvider.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author avazirna
*/

public interface IEncryptionKeyProvider {
public interface IKeyStoreEncryptionKeyProvider {

Key generateCryptographicKeyInKeyStore(String keyAlias,
EncryptionHelper.CryptographicOperation cryptographicOperation)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.commcare.util;

import java.util.ServiceLoader;

/**
* Utility class responsible for finding implementations of IEncryptionKeyProvider during runtime
* and loading them in memory
*
* @author avazirna
*/

public class KeyStoreEncryptionKeyServiceProvider {
private static KeyStoreEncryptionKeyServiceProvider serviceProvider;
private ServiceLoader<IKeyStoreEncryptionKeyProvider> loader;

private KeyStoreEncryptionKeyServiceProvider() {
loader = ServiceLoader.load(IKeyStoreEncryptionKeyProvider.class);
}

public static KeyStoreEncryptionKeyServiceProvider getInstance() {
if (serviceProvider == null) {
serviceProvider = new KeyStoreEncryptionKeyServiceProvider();
}
return serviceProvider;
}

public IKeyStoreEncryptionKeyProvider serviceImpl() {
IKeyStoreEncryptionKeyProvider service = null;
if (loader.iterator().hasNext()) {
service = loader.iterator().next();
}

return service;
}
}

0 comments on commit b2a8da9

Please sign in to comment.