22
33import static org .junit .jupiter .api .Assertions .*;
44
5+ import java .io .File ;
6+ import java .io .IOException ;
57import java .math .BigInteger ;
8+ import java .sql .Connection ;
9+ import java .sql .SQLException ;
10+ import java .util .Properties ;
611import org .junit .jupiter .api .Test ;
712import 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