Skip to content

Commit

Permalink
Change HashKey in the driver to 256 Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Jun 28, 2018
1 parent 21945e5 commit e6bef4e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
52 changes: 26 additions & 26 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,37 +122,37 @@ public class SQLServerConnection implements ISQLServerConnection, java.io.Serial

private Boolean isAzureDW = null;

static class Sha1HashKey implements java.io.Serializable {
static class SQLServerHashKey implements java.io.Serializable {

/**
* Always refresh SerialVersionUID when prompted
*/
private static final long serialVersionUID = 166788428640603097L;
private byte[] bytes;

Sha1HashKey(String sql,
SQLServerHashKey(String sql,
String parametersDefinition) {
this(String.format("%s%s", sql, parametersDefinition));
this(sql + parametersDefinition);
}

Sha1HashKey(String s) {
bytes = getSha1Digest().digest(s.getBytes());
SQLServerHashKey(String s) {
bytes = getSha256Digest().digest(s.getBytes());
}

public boolean equals(Object obj) {
if (!(obj instanceof Sha1HashKey))
if (!(obj instanceof SQLServerHashKey))
return false;

return java.util.Arrays.equals(bytes, ((Sha1HashKey) obj).bytes);
return java.util.Arrays.equals(bytes, ((SQLServerHashKey) obj).bytes);
}

public int hashCode() {
return java.util.Arrays.hashCode(bytes);
}

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

PreparedStatementHandle(Sha1HashKey key,
PreparedStatementHandle(SQLServerHashKey key,
int handle,
boolean isDirectSql,
boolean isEvictedFromCache) {
Expand Down Expand Up @@ -211,7 +211,7 @@ int getHandle() {
}

/** Get the cache key. */
Sha1HashKey getKey() {
SQLServerHashKey getKey() {
return key;
}

Expand Down Expand Up @@ -258,19 +258,19 @@ void removeReference() {
static final private int PARSED_SQL_CACHE_SIZE = 100;

/** Cache of parsed SQL meta data */
static private ConcurrentLinkedHashMap<Sha1HashKey, ParsedSQLCacheItem> parsedSQLCache;
static private ConcurrentLinkedHashMap<SQLServerHashKey, ParsedSQLCacheItem> parsedSQLCache;

static {
parsedSQLCache = new Builder<Sha1HashKey, ParsedSQLCacheItem>().maximumWeightedCapacity(PARSED_SQL_CACHE_SIZE).build();
parsedSQLCache = new Builder<SQLServerHashKey, ParsedSQLCacheItem>().maximumWeightedCapacity(PARSED_SQL_CACHE_SIZE).build();
}

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

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

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

/** Cache of prepared statement handles */
private ConcurrentLinkedHashMap<Sha1HashKey, PreparedStatementHandle> preparedStatementHandleCache;
private ConcurrentLinkedHashMap<SQLServerHashKey, PreparedStatementHandle> preparedStatementHandleCache;
/** Cache of prepared statement parameter metadata */
private ConcurrentLinkedHashMap<Sha1HashKey, SQLServerParameterMetaData> parameterMetadataCache;
private ConcurrentLinkedHashMap<SQLServerHashKey, SQLServerParameterMetaData> parameterMetadataCache;
/**
* Checks whether statement pooling is enabled or disabled. The default is set to true;
*/
Expand Down Expand Up @@ -5797,23 +5797,23 @@ public void setStatementPoolingCacheSize(int value) {
* @param value
*/
private void prepareCache() {
preparedStatementHandleCache = new Builder<Sha1HashKey, PreparedStatementHandle>().maximumWeightedCapacity(getStatementPoolingCacheSize())
preparedStatementHandleCache = new Builder<SQLServerHashKey, PreparedStatementHandle>().maximumWeightedCapacity(getStatementPoolingCacheSize())
.listener(new PreparedStatementCacheEvictionListener()).build();

parameterMetadataCache = new Builder<Sha1HashKey, SQLServerParameterMetaData>().maximumWeightedCapacity(getStatementPoolingCacheSize())
parameterMetadataCache = new Builder<SQLServerHashKey, SQLServerParameterMetaData>().maximumWeightedCapacity(getStatementPoolingCacheSize())
.build();
}

/** Get a parameter metadata cache entry if statement pooling is enabled */
final SQLServerParameterMetaData getCachedParameterMetadata(Sha1HashKey key) {
final SQLServerParameterMetaData getCachedParameterMetadata(SQLServerHashKey key) {
if (!isStatementPoolingEnabled())
return null;

return parameterMetadataCache.get(key);
}

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

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

return preparedStatementHandleCache.get(key);
}

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

// Handle closing handles when removed from cache.
final class PreparedStatementCacheEvictionListener implements EvictionListener<Sha1HashKey, PreparedStatementHandle> {
public void onEviction(Sha1HashKey key,
final class PreparedStatementCacheEvictionListener implements EvictionListener<SQLServerHashKey, PreparedStatementHandle> {
public void onEviction(SQLServerHashKey key,
PreparedStatementHandle handle) {
if (null != handle) {
handle.setIsEvictedFromCache(true); // Mark as evicted from cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.logging.Level;

import com.microsoft.sqlserver.jdbc.SQLServerConnection.PreparedStatementHandle;
import com.microsoft.sqlserver.jdbc.SQLServerConnection.Sha1HashKey;
import com.microsoft.sqlserver.jdbc.SQLServerConnection.SQLServerHashKey;

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

/** Hash of user supplied SQL statement used for various cache lookups */
private Sha1HashKey sqlTextCacheKey;
private SQLServerHashKey sqlTextCacheKey;

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

// Create a cache key for this statement.
sqlTextCacheKey = new Sha1HashKey(sql);
sqlTextCacheKey = new SQLServerHashKey(sql);

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

param.skipValue(tdsReader, true);
Expand Down Expand Up @@ -978,7 +978,7 @@ private boolean reuseCachedHandle(boolean hasNewTypeDefinitions,

// Check for new cache reference.
if (null == cachedPreparedStatementHandle) {
PreparedStatementHandle cachedHandle = connection.getCachedPreparedStatementHandle(new Sha1HashKey(preparedSQL, preparedTypeDefinitions));
PreparedStatementHandle cachedHandle = connection.getCachedPreparedStatementHandle(new SQLServerHashKey(preparedSQL, preparedTypeDefinitions));
// 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
// if it is batch query and has new type definition, or if it is on, make sure encryptionMetadataIsRetrieved is retrieved.
if (null != cachedHandle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.microsoft.sqlserver.jdbc.SQLServerConnection.Sha1HashKey;
import com.microsoft.sqlserver.jdbc.SQLServerConnection.SQLServerHashKey;

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

Sha1HashKey cacheKey = new Sha1HashKey(sql);
SQLServerHashKey cacheKey = new SQLServerHashKey(sql);

// Check for cached SQL metadata.
ParsedSQLCacheItem cacheItem = getCachedParsedSQL(cacheKey);
Expand Down

0 comments on commit e6bef4e

Please sign in to comment.