-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add SQLiteMC encryption for JVM #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 26 additions & 80 deletions
106
core/data/db-sqldelight/src/jvmMain/kotlin/com/softartdev/notedelight/db/JvmCipherUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,96 +1,42 @@ | ||
| package com.softartdev.notedelight.db | ||
|
|
||
| import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver | ||
| import com.softartdev.notedelight.model.PlatformSQLiteState | ||
| import io.toxicity.sqlite.mc.driver.SQLiteMCDriver | ||
| import io.toxicity.sqlite.mc.driver.config.DatabasesDir | ||
| import io.toxicity.sqlite.mc.driver.config.encryption.Key | ||
| import java.io.File | ||
| import java.io.FileNotFoundException | ||
| import java.sql.Connection | ||
| import java.sql.DriverManager | ||
| import java.sql.ResultSet | ||
| import java.sql.Statement | ||
|
|
||
| object JvmCipherUtils { | ||
|
|
||
| fun getDatabaseState(dbName: String): PlatformSQLiteState { | ||
| var result = PlatformSQLiteState.DOES_NOT_EXIST | ||
| val dbPath = File(dbName) | ||
| if (dbPath.exists()) { | ||
| result = PlatformSQLiteState.UNENCRYPTED | ||
| var connection: Connection? = null | ||
| try { | ||
| val url = JdbcSqliteDriver.IN_MEMORY + dbName // jdbc:sqlite:$dbName | ||
| connection = DriverManager.getConnection(url) | ||
| val statement: Statement = connection.createStatement() | ||
| val resultSet: ResultSet = statement.executeQuery("PRAGMA user_version;") | ||
| val version: Long = resultSet.getLong(1) | ||
| println("db version = $version") | ||
| } catch (throwable: Throwable) { | ||
| throwable.printStackTrace() | ||
| result = PlatformSQLiteState.ENCRYPTED | ||
| } finally { | ||
| connection?.close() | ||
| private val dbFile = File(FilePathResolver().invoke()) | ||
|
|
||
| private val factory = SQLiteMCDriver.Factory( | ||
| dbName = dbFile.name, | ||
| schema = NoteDb.Schema | ||
| ) { | ||
| filesystem(DatabasesDir(dbFile.parentFile!!)) { | ||
| encryption { | ||
| sqlCipher { v4() } | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| fun decrypt(password: String, dbName: String) { | ||
| val originalFile = File(dbName) | ||
| if (originalFile.exists()) { | ||
| val newFile = File.createTempFile("sqlcipherutils", "tmp", null) | ||
|
|
||
| var url = JdbcSqliteDriver.IN_MEMORY + dbName // jdbc:sqlite:$dbName | ||
| var connection = DriverManager.getConnection(url, null, password) | ||
| val statement = connection.prepareStatement("ATTACH DATABASE ? AS plaintext KEY ''") | ||
| statement.setString(1, newFile.absolutePath) | ||
| statement.execute() | ||
| connection.createStatement().executeQuery("SELECT sqlcipher_export('plaintext')") | ||
| connection.createStatement().executeQuery("DETACH DATABASE plaintext") | ||
| val resultSet: ResultSet = statement.executeQuery("PRAGMA user_version;") | ||
| val version: Long = resultSet.getLong(1) | ||
| statement.close() | ||
| connection.close() | ||
|
|
||
| url = JdbcSqliteDriver.IN_MEMORY + newFile.absolutePath | ||
| connection = DriverManager.getConnection(url) | ||
| connection.createStatement().executeQuery("PRAGMA user_version = $version") | ||
| connection.close() | ||
|
|
||
| originalFile.delete() | ||
| newFile.renameTo(originalFile) | ||
| } else { | ||
| throw FileNotFoundException(originalFile.absolutePath + " not found") | ||
| fun getDatabaseState(): PlatformSQLiteState { | ||
| if (!dbFile.exists()) return PlatformSQLiteState.DOES_NOT_EXIST | ||
| return try { | ||
| factory.createBlocking(Key.Empty).close() | ||
| PlatformSQLiteState.UNENCRYPTED | ||
| } catch (_: Throwable) { | ||
| PlatformSQLiteState.ENCRYPTED | ||
| } | ||
| } | ||
|
|
||
| fun encrypt(password: String?, dbName: String) { | ||
| val originalFile = File(dbName) | ||
| if (originalFile.exists()) { | ||
| val newFile = File.createTempFile("sqlcipherutils", "tmp", null) | ||
|
|
||
| var url = JdbcSqliteDriver.IN_MEMORY + originalFile.absolutePath // jdbc:sqlite:${originalFile.absolutePath} | ||
| var connection = DriverManager.getConnection(url) | ||
| var statement: Statement = connection.createStatement() | ||
| val resultSet: ResultSet = statement.executeQuery("PRAGMA user_version;") | ||
| val version: Long = resultSet.getLong(1) | ||
| statement.close() | ||
| connection.close() | ||
|
|
||
| url = JdbcSqliteDriver.IN_MEMORY + newFile.absolutePath | ||
| connection = DriverManager.getConnection(url, null, password) | ||
| statement = connection.prepareStatement("ATTACH DATABASE ? AS plaintext KEY ''") | ||
| statement.setString(1, originalFile.absolutePath) | ||
| statement.execute() | ||
| connection.createStatement().executeQuery("SELECT sqlcipher_export('plaintext')") | ||
| connection.createStatement().executeQuery("DETACH DATABASE plaintext") | ||
| connection.createStatement().executeQuery("PRAGMA user_version = $version") | ||
| statement.close() | ||
| connection.close() | ||
|
|
||
| originalFile.delete() | ||
| newFile.renameTo(originalFile) | ||
| fun createDriver(passphrase: CharSequence = "", rekey: CharSequence? = null): SQLiteMCDriver { | ||
| val key = if (passphrase.isEmpty()) Key.Empty else Key.passphrase(passphrase.toString()) | ||
| return if (rekey != null) { | ||
| val rekeyKey = if (rekey.isEmpty()) Key.Empty else Key.passphrase(rekey.toString()) | ||
| factory.createBlocking(key, rekeyKey) | ||
| } else { | ||
| throw FileNotFoundException(originalFile.absolutePath + " not found") | ||
| factory.createBlocking(key) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Declare SQLiteMC driver dependency for JVM sources
The Kotlin classes now import
io.toxicity.sqlite.mc.driver.SQLiteMCDriverand related configuration, but thejvmMainsource set no longer pulls in any driver dependency—implementation(libs.sqlDelight.jvm)was removed and nothing replaced it. Without a runtime such asio.toxicity.sqlite-mc:sqlite-driverthe JVM module will not compile (unresolved reference) or will crash at runtime withClassNotFoundException. Add the SQLiteMC driver dependency to thejvmMain(and corresponding test) dependencies to match the new imports.Useful? React with 👍 / 👎.