forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLServerAeadAes256CbcHmac256Factory.java
63 lines (48 loc) · 2.66 KB
/
SQLServerAeadAes256CbcHmac256Factory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Microsoft JDBC Driver for SQL Server
*
* Copyright(c) Microsoft Corporation All rights reserved.
*
* This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.bind.DatatypeConverter;
/**
* Factory for SQLServerAeadAes256CbcHmac256Algorithm
*/
class SQLServerAeadAes256CbcHmac256Factory extends SQLServerEncryptionAlgorithmFactory {
// In future we can have more
private byte algorithmVersion = 0x1;
private ConcurrentHashMap<String, SQLServerAeadAes256CbcHmac256Algorithm> encryptionAlgorithms = new ConcurrentHashMap<String, SQLServerAeadAes256CbcHmac256Algorithm>();
@Override
SQLServerEncryptionAlgorithm create(SQLServerSymmetricKey columnEncryptionKey,
SQLServerEncryptionType encryptionType,
String encryptionAlgorithm) throws SQLServerException {
assert (columnEncryptionKey != null);
if (encryptionType != SQLServerEncryptionType.Deterministic && encryptionType != SQLServerEncryptionType.Randomized) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_InvalidEncryptionType"));
Object[] msgArgs = {encryptionType, encryptionAlgorithm,
"'" + SQLServerEncryptionType.Deterministic + "," + SQLServerEncryptionType.Randomized + "'"};
throw new SQLServerException(this, form.format(msgArgs), null, 0, false);
}
String factoryKey = "";
StringBuffer factoryKeyBuilder = new StringBuffer();
factoryKeyBuilder.append(DatatypeConverter.printBase64Binary(new String(columnEncryptionKey.getRootKey(), UTF_8).getBytes()));
factoryKeyBuilder.append(":");
factoryKeyBuilder.append(encryptionType);
factoryKeyBuilder.append(":");
factoryKeyBuilder.append(algorithmVersion);
factoryKey = factoryKeyBuilder.toString();
SQLServerAeadAes256CbcHmac256Algorithm aesAlgorithm;
if (!encryptionAlgorithms.containsKey(factoryKey)) {
SQLServerAeadAes256CbcHmac256EncryptionKey encryptedKey = new SQLServerAeadAes256CbcHmac256EncryptionKey(columnEncryptionKey.getRootKey(),
SQLServerAeadAes256CbcHmac256Algorithm.algorithmName);
aesAlgorithm = new SQLServerAeadAes256CbcHmac256Algorithm(encryptedKey, encryptionType, algorithmVersion);
encryptionAlgorithms.putIfAbsent(factoryKey, aesAlgorithm);
}
return encryptionAlgorithms.get(factoryKey);
}
}