Skip to content

Commit

Permalink
Merge pull request #570 from v-afrafi/Java9
Browse files Browse the repository at this point in the history
Update samples in JDBC 4.3 branch
  • Loading branch information
AfsanehR-zz authored Nov 30, 2017
2 parents 8c7f465 + 27b9414 commit b1dbacc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.platform.version>1.1.0-M1</junit.platform.version>
<junit.platform.version>1.0.0-M3</junit.platform.version>
<junit.jupiter.version>5.0.0-M3</junit.jupiter.version>
</properties>

Expand Down
24 changes: 22 additions & 2 deletions src/samples/alwaysencrypted/src/main/java/AlwaysEncrypted.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.sql.SQLException;
import java.sql.Statement;

import javax.xml.bind.DatatypeConverter;

import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider;
import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionKeyStoreProvider;
Expand Down Expand Up @@ -116,7 +115,7 @@ public static void main(String[] args) {
*/
String createCEKSQL = "CREATE COLUMN ENCRYPTION KEY " + columnEncryptionKey + " WITH VALUES ( " + " COLUMN_MASTER_KEY = "
+ columnMasterKeyName + " , ALGORITHM = '" + algorithm + "' , ENCRYPTED_VALUE = 0x"
+ DatatypeConverter.printHexBinary(encryptedCEK) + " ) ";
+ bytesToHexString(encryptedCEK, encryptedCEK.length) + " ) ";

try (Statement cekStatement = sourceConnection.createStatement()) {
cekStatement.executeUpdate(createCEKSQL);
Expand All @@ -129,6 +128,27 @@ public static void main(String[] args) {
e.printStackTrace();
}
}

/**
*
* @param b
* byte value
* @param length
* length of the array
* @return
*/
final static char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

private static String bytesToHexString(byte[] b,
int length) {
StringBuilder sb = new StringBuilder(length * 2);
for (int i = 0; i < length; i++) {
int hexVal = b[i] & 0xFF;
sb.append(hexChars[(hexVal & 0xF0) >> 4]);
sb.append(hexChars[(hexVal & 0x0F)]);
}
return sb.toString();
}

// To avoid storing the sourceConnection String in your code,
// you can retrieve it from a configuration file.
Expand Down

0 comments on commit b1dbacc

Please sign in to comment.