Skip to content

Commit e6bef4e

Browse files
Change HashKey in the driver to 256 Hash
1 parent 21945e5 commit e6bef4e

3 files changed

Lines changed: 33 additions & 33 deletions

File tree

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,37 +122,37 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial
122122

123123
private Boolean isAzureDW = null;
124124

125-
static class Sha1HashKey implements java.io.Serializable {
125+
static class SQLServerHashKey implements java.io.Serializable {
126126

127127
/**
128128
* Always refresh SerialVersionUID when prompted
129129
*/
130130
private static final long serialVersionUID = 166788428640603097L;
131131
private byte[] bytes;
132132

133-
Sha1HashKey(String sql,
133+
SQLServerHashKey(String sql,
134134
String parametersDefinition) {
135-
this(String.format("%s%s", sql, parametersDefinition));
135+
this(sql + parametersDefinition);
136136
}
137137

138-
Sha1HashKey(String s) {
139-
bytes = getSha1Digest().digest(s.getBytes());
138+
SQLServerHashKey(String s) {
139+
bytes = getSha256Digest().digest(s.getBytes());
140140
}
141141

142142
public boolean equals(Object obj) {
143-
if (!(obj instanceof Sha1HashKey))
143+
if (!(obj instanceof SQLServerHashKey))
144144
return false;
145145

146-
return java.util.Arrays.equals(bytes, ((Sha1HashKey) obj).bytes);
146+
return java.util.Arrays.equals(bytes, ((SQLServerHashKey) obj).bytes);
147147
}
148148

149149
public int hashCode() {
150150
return java.util.Arrays.hashCode(bytes);
151151
}
152152

153-
private java.security.MessageDigest getSha1Digest() {
153+
private java.security.MessageDigest getSha256Digest() {
154154
try {
155-
return java.security.MessageDigest.getInstance("SHA-1");
155+
return java.security.MessageDigest.getInstance("SHA-256");
156156
}
157157
catch (final java.security.NoSuchAlgorithmException e) {
158158
// This is not theoretically possible, but we're forced to catch it anyway
@@ -170,9 +170,9 @@ class PreparedStatementHandle {
170170
private boolean isDirectSql;
171171
private volatile boolean evictedFromCache;
172172
private volatile boolean explicitlyDiscarded;
173-
private Sha1HashKey key;
173+
private SQLServerHashKey key;
174174

175-
PreparedStatementHandle(Sha1HashKey key,
175+
PreparedStatementHandle(SQLServerHashKey key,
176176
int handle,
177177
boolean isDirectSql,
178178
boolean isEvictedFromCache) {
@@ -211,7 +211,7 @@ int getHandle() {
211211
}
212212

213213
/** Get the cache key. */
214-
Sha1HashKey getKey() {
214+
SQLServerHashKey getKey() {
215215
return key;
216216
}
217217

@@ -258,19 +258,19 @@ void removeReference() {
258258
static final private int PARSED_SQL_CACHE_SIZE = 100;
259259

260260
/** Cache of parsed SQL meta data */
261-
static private ConcurrentLinkedHashMap<Sha1HashKey, ParsedSQLCacheItem> parsedSQLCache;
261+
static private ConcurrentLinkedHashMap<SQLServerHashKey, ParsedSQLCacheItem> parsedSQLCache;
262262

263263
static {
264-
parsedSQLCache = new Builder<Sha1HashKey, ParsedSQLCacheItem>().maximumWeightedCapacity(PARSED_SQL_CACHE_SIZE).build();
264+
parsedSQLCache = new Builder<SQLServerHashKey, ParsedSQLCacheItem>().maximumWeightedCapacity(PARSED_SQL_CACHE_SIZE).build();
265265
}
266266

267267
/** Get prepared statement cache entry if exists, if not parse and create a new one */
268-
static ParsedSQLCacheItem getCachedParsedSQL(Sha1HashKey key) {
268+
static ParsedSQLCacheItem getCachedParsedSQL(SQLServerHashKey key) {
269269
return parsedSQLCache.get(key);
270270
}
271271

272272
/** Parse and create a information about parsed SQL text */
273-
static ParsedSQLCacheItem parseAndCacheSQL(Sha1HashKey key,
273+
static ParsedSQLCacheItem parseAndCacheSQL(SQLServerHashKey key,
274274
String sql) throws SQLServerException {
275275
JDBCSyntaxTranslator translator = new JDBCSyntaxTranslator();
276276

@@ -291,9 +291,9 @@ static ParsedSQLCacheItem parseAndCacheSQL(Sha1HashKey key,
291291
private int statementPoolingCacheSize = DEFAULT_STATEMENT_POOLING_CACHE_SIZE;
292292

293293
/** Cache of prepared statement handles */
294-
private ConcurrentLinkedHashMap<Sha1HashKey, PreparedStatementHandle> preparedStatementHandleCache;
294+
private ConcurrentLinkedHashMap<SQLServerHashKey, PreparedStatementHandle> preparedStatementHandleCache;
295295
/** Cache of prepared statement parameter metadata */
296-
private ConcurrentLinkedHashMap<Sha1HashKey, SQLServerParameterMetaData> parameterMetadataCache;
296+
private ConcurrentLinkedHashMap<SQLServerHashKey, SQLServerParameterMetaData> parameterMetadataCache;
297297
/**
298298
* Checks whether statement pooling is enabled or disabled. The default is set to true;
299299
*/
@@ -5797,23 +5797,23 @@ public void setStatementPoolingCacheSize(int value) {
57975797
* @param value
57985798
*/
57995799
private void prepareCache() {
5800-
preparedStatementHandleCache = new Builder<Sha1HashKey, PreparedStatementHandle>().maximumWeightedCapacity(getStatementPoolingCacheSize())
5800+
preparedStatementHandleCache = new Builder<SQLServerHashKey, PreparedStatementHandle>().maximumWeightedCapacity(getStatementPoolingCacheSize())
58015801
.listener(new PreparedStatementCacheEvictionListener()).build();
58025802

5803-
parameterMetadataCache = new Builder<Sha1HashKey, SQLServerParameterMetaData>().maximumWeightedCapacity(getStatementPoolingCacheSize())
5803+
parameterMetadataCache = new Builder<SQLServerHashKey, SQLServerParameterMetaData>().maximumWeightedCapacity(getStatementPoolingCacheSize())
58045804
.build();
58055805
}
58065806

58075807
/** Get a parameter metadata cache entry if statement pooling is enabled */
5808-
final SQLServerParameterMetaData getCachedParameterMetadata(Sha1HashKey key) {
5808+
final SQLServerParameterMetaData getCachedParameterMetadata(SQLServerHashKey key) {
58095809
if (!isStatementPoolingEnabled())
58105810
return null;
58115811

58125812
return parameterMetadataCache.get(key);
58135813
}
58145814

58155815
/** Register a parameter metadata cache entry if statement pooling is enabled */
5816-
final void registerCachedParameterMetadata(Sha1HashKey key,
5816+
final void registerCachedParameterMetadata(SQLServerHashKey key,
58175817
SQLServerParameterMetaData pmd) {
58185818
if (!isStatementPoolingEnabled() || null == pmd)
58195819
return;
@@ -5822,15 +5822,15 @@ final void registerCachedParameterMetadata(Sha1HashKey key,
58225822
}
58235823

58245824
/** Get or create prepared statement handle cache entry if statement pooling is enabled */
5825-
final PreparedStatementHandle getCachedPreparedStatementHandle(Sha1HashKey key) {
5825+
final PreparedStatementHandle getCachedPreparedStatementHandle(SQLServerHashKey key) {
58265826
if (!isStatementPoolingEnabled())
58275827
return null;
58285828

58295829
return preparedStatementHandleCache.get(key);
58305830
}
58315831

58325832
/** Get or create prepared statement handle cache entry if statement pooling is enabled */
5833-
final PreparedStatementHandle registerCachedPreparedStatementHandle(Sha1HashKey key,
5833+
final PreparedStatementHandle registerCachedPreparedStatementHandle(SQLServerHashKey key,
58345834
int handle,
58355835
boolean isDirectSql) {
58365836
if (!isStatementPoolingEnabled() || null == key)
@@ -5858,8 +5858,8 @@ final void evictCachedPreparedStatementHandle(PreparedStatementHandle handle) {
58585858
}
58595859

58605860
// Handle closing handles when removed from cache.
5861-
final class PreparedStatementCacheEvictionListener implements EvictionListener<Sha1HashKey, PreparedStatementHandle> {
5862-
public void onEviction(Sha1HashKey key,
5861+
final class PreparedStatementCacheEvictionListener implements EvictionListener<SQLServerHashKey, PreparedStatementHandle> {
5862+
public void onEviction(SQLServerHashKey key,
58635863
PreparedStatementHandle handle) {
58645864
if (null != handle) {
58655865
handle.setIsEvictedFromCache(true); // Mark as evicted from cache.

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.logging.Level;
3434

3535
import com.microsoft.sqlserver.jdbc.SQLServerConnection.PreparedStatementHandle;
36-
import com.microsoft.sqlserver.jdbc.SQLServerConnection.Sha1HashKey;
36+
import com.microsoft.sqlserver.jdbc.SQLServerConnection.SQLServerHashKey;
3737

3838
/**
3939
* SQLServerPreparedStatement provides JDBC prepared statement functionality. SQLServerPreparedStatement provides methods for the user to supply
@@ -75,7 +75,7 @@ public class SQLServerPreparedStatement extends SQLServerStatement implements IS
7575
private PreparedStatementHandle cachedPreparedStatementHandle;
7676

7777
/** Hash of user supplied SQL statement used for various cache lookups */
78-
private Sha1HashKey sqlTextCacheKey;
78+
private SQLServerHashKey sqlTextCacheKey;
7979

8080
/**
8181
* Array with parameter names generated in buildParamTypeDefinitions For mapping encryption information to parameters, as the second result set
@@ -214,7 +214,7 @@ String getClassNameInternal() {
214214
stmtPoolable = true;
215215

216216
// Create a cache key for this statement.
217-
sqlTextCacheKey = new Sha1HashKey(sql);
217+
sqlTextCacheKey = new SQLServerHashKey(sql);
218218

219219
// Parse or fetch SQL metadata from cache.
220220
ParsedSQLCacheItem parsedSQL = getCachedParsedSQL(sqlTextCacheKey);
@@ -631,7 +631,7 @@ boolean onRetValue(TDSReader tdsReader) throws SQLServerException {
631631
// Cache the reference to the newly created handle, NOT for cursorable handles.
632632
if (null == cachedPreparedStatementHandle && !isCursorable(executeMethod)) {
633633
cachedPreparedStatementHandle = connection.registerCachedPreparedStatementHandle(
634-
new Sha1HashKey(preparedSQL, preparedTypeDefinitions), prepStmtHandle, executedSqlDirectly);
634+
new SQLServerHashKey(preparedSQL, preparedTypeDefinitions), prepStmtHandle, executedSqlDirectly);
635635
}
636636

637637
param.skipValue(tdsReader, true);
@@ -978,7 +978,7 @@ private boolean reuseCachedHandle(boolean hasNewTypeDefinitions,
978978

979979
// Check for new cache reference.
980980
if (null == cachedPreparedStatementHandle) {
981-
PreparedStatementHandle cachedHandle = connection.getCachedPreparedStatementHandle(new Sha1HashKey(preparedSQL, preparedTypeDefinitions));
981+
PreparedStatementHandle cachedHandle = connection.getCachedPreparedStatementHandle(new SQLServerHashKey(preparedSQL, preparedTypeDefinitions));
982982
// If handle was found then re-use, only if AE is not on and is not a batch query with new type definitions (We shouldn't reuse handle
983983
// if it is batch query and has new type definition, or if it is on, make sure encryptionMetadataIsRetrieved is retrieved.
984984
if (null != cachedHandle) {

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.regex.Matcher;
2929
import java.util.regex.Pattern;
3030

31-
import com.microsoft.sqlserver.jdbc.SQLServerConnection.Sha1HashKey;
31+
import com.microsoft.sqlserver.jdbc.SQLServerConnection.SQLServerHashKey;
3232

3333
/**
3434
* SQLServerStatment provides the basic implementation of JDBC statement functionality. It also provides a number of base class implementation methods
@@ -752,7 +752,7 @@ final void processResponse(TDSReader tdsReader) throws SQLServerException {
752752
private String ensureSQLSyntax(String sql) throws SQLServerException {
753753
if (sql.indexOf(LEFT_CURLY_BRACKET) >= 0) {
754754

755-
Sha1HashKey cacheKey = new Sha1HashKey(sql);
755+
SQLServerHashKey cacheKey = new SQLServerHashKey(sql);
756756

757757
// Check for cached SQL metadata.
758758
ParsedSQLCacheItem cacheItem = getCachedParsedSQL(cacheKey);

0 commit comments

Comments
 (0)