Skip to content

Commit 8cb90d0

Browse files
committed
Fixed bug with hexadecimal keys
1 parent 73efe29 commit 8cb90d0

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version=3.39.1
2-
artifactVersion=3.39.1
2+
artifactVersion=3.39.1.1-SNAPSHOT
33
sqliteMCVersion=1.4.6

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.github.willena</groupId>
66
<artifactId>sqlite-jdbc</artifactId>
7-
<version>3.39.1</version>
7+
<version>3.39.1.1-SNAPSHOT</version>
88
<name>SQLite JDBC</name>
99
<description>SQLite JDBC library with encryption and authentication support</description>
1010
<url>https://github.com/Willena/sqlite-jdbc-crypt</url>

src/main/java/org/sqlite/mc/SQLiteMCConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ public Builder withHexKey(byte[] key) {
223223
ByteBuffer byteBuffer = ByteBuffer.wrap(key);
224224
StringBuilder hexString = new StringBuilder();
225225
while (byteBuffer.hasRemaining()) {
226-
hexString.append(String.format("%02X ", byteBuffer.get()));
226+
hexString.append(String.format("%02X", byteBuffer.get()));
227227
}
228-
return withKey(hexString.toString());
228+
return withHexKey(hexString.toString());
229229
}
230230

231231
public Builder withHexKey(String hexkey) {

src/test/java/org/sqlite/mc/SQLiteMCSqlCipherConfigTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import java.io.File;
6+
import java.io.IOException;
57
import java.math.BigInteger;
8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
import java.util.Properties;
611
import org.junit.jupiter.api.Test;
712
import org.sqlite.SQLiteConfig;
813

@@ -19,6 +24,9 @@ class SQLiteMCSqlCipherConfigTest {
1924
"54686973206973206D79207070617373776F72642E2E73616C7479206B65792073616C742E"
2025
.toLowerCase();
2126

27+
private static final String hexKey = "54686973206973206D792070";
28+
private static final String hexKey2 = "AAFF54686973206973206D792070";
29+
2230
// https://www.baeldung.com/java-byte-arrays-hex-strings
2331
private static byte[] toBytes(String hexString) {
2432
byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
@@ -55,4 +63,44 @@ void withRawSaltedKey() {
5563
config.build().toProperties().getProperty(SQLiteConfig.Pragma.KEY.pragmaName),
5664
("x'" + saltedHexKeyValid + "'"));
5765
}
66+
67+
@Test
68+
void withHexKey() {
69+
SQLiteMCSqlCipherConfig config = new SQLiteMCSqlCipherConfig();
70+
config.withHexKey(toBytes(hexKey));
71+
72+
Properties buildedConfig = config.build().toProperties();
73+
74+
assertEquals(buildedConfig.getProperty(SQLiteConfig.Pragma.KEY.pragmaName), hexKey);
75+
assertEquals(
76+
buildedConfig.getProperty(SQLiteConfig.Pragma.HEXKEY_MODE.pragmaName),
77+
SQLiteConfig.HexKeyMode.SSE.getValue());
78+
}
79+
80+
@Test
81+
void hexKeyRekey() throws IOException, SQLException {
82+
File tmpFile = File.createTempFile("tmp-sqlite", ".db");
83+
tmpFile.deleteOnExit();
84+
85+
SQLiteMCSqlCipherConfig config = new SQLiteMCSqlCipherConfig();
86+
Connection con =
87+
config.withHexKey(hexKey)
88+
.build()
89+
.createConnection("jdbc:sqlite:file:" + tmpFile.getAbsolutePath());
90+
con.createStatement().execute(String.format("PRAGMA hexrekey='%s'", hexKey2));
91+
con.close();
92+
93+
assertThrows(
94+
SQLException.class,
95+
() ->
96+
config.withHexKey(hexKey)
97+
.build()
98+
.createConnection("jdbc:sqlite:file:" + tmpFile.getAbsolutePath()));
99+
100+
assertDoesNotThrow(
101+
() ->
102+
config.withHexKey(hexKey2)
103+
.build()
104+
.createConnection("jdbc:sqlite:file:" + tmpFile.getAbsolutePath()));
105+
}
58106
}

0 commit comments

Comments
 (0)