package com.microsoft.sqlserver.jdbc;
import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap;
final class SQLServerEncryptionAlgorithmFactoryList {
private ConcurrentHashMap<String, SQLServerEncryptionAlgorithmFactory> encryptionAlgoFactoryMap;
private static final SQLServerEncryptionAlgorithmFactoryList instance = new SQLServerEncryptionAlgorithmFactoryList();
private SQLServerEncryptionAlgorithmFactoryList() {
encryptionAlgoFactoryMap = new ConcurrentHashMap<>();
encryptionAlgoFactoryMap.putIfAbsent(SQLServerAeadAes256CbcHmac256Algorithm.algorithmName,
new SQLServerAeadAes256CbcHmac256Factory());
}
static SQLServerEncryptionAlgorithmFactoryList getInstance() {
return instance;
}
String getRegisteredCipherAlgorithmNames() {
StringBuffer stringBuff = new StringBuffer();
boolean first = true;
for (String key : encryptionAlgoFactoryMap.keySet()) {
if (first) {
stringBuff.append("'");
first = false;
} else {
stringBuff.append(", '");
}
stringBuff.append(key);
stringBuff.append("'");
}
return stringBuff.toString();
}
SQLServerEncryptionAlgorithm getAlgorithm(SQLServerSymmetricKey key, SQLServerEncryptionType encryptionType,
String algorithmName) throws SQLServerException {
SQLServerEncryptionAlgorithm encryptionAlgorithm = null;
SQLServerEncryptionAlgorithmFactory factory = null;
if (!encryptionAlgoFactoryMap.containsKey(algorithmName)) {
MessageFormat form = new MessageFormat(
SQLServerException.getErrString("R_UnknownColumnEncryptionAlgorithm"));
Object[] msgArgs = {algorithmName,
SQLServerEncryptionAlgorithmFactoryList.getInstance().getRegisteredCipherAlgorithmNames()};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
factory = encryptionAlgoFactoryMap.get(algorithmName);
assert null != factory : "Null Algorithm Factory class detected";
encryptionAlgorithm = factory.create(key, encryptionType, algorithmName);
return encryptionAlgorithm;
}
}