Skip to content
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

Revised from previous PR89: Add Encryption function options #92

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ keywords = ["database"]
default = ["linkage"]
bundled = ["sqlite3-sys/bundled"]
extension = []
encryption = []
IvanUkhov marked this conversation as resolved.
Show resolved Hide resolved
linkage = ["sqlite3-sys/linkage"]

[dependencies.sqlite3-sys]
Expand Down
36 changes: 36 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,42 @@ impl Connection {
Ok(())
}

/// Set the encryption key.
#[cfg(feature = "encryption")]
#[inline]
pub fn set_encryption_key<T: AsRef<str>>(&self, key: T) -> Result<()> {
unsafe {
ok!(
self.raw.0,
ffi::sqlite3_key_v2(
self.raw.0,
std::ptr::null() as *const c_char,
str_to_cstr!(key.as_ref()).as_ptr() as *const c_void,
key.as_ref().len() as c_int,
)
);
}
Ok(())
}

/// Change the encryption key.
#[cfg(feature = "encryption")]
#[inline]
pub fn reset_encryption_key<T: AsRef<str>>(&self, new_key: T) -> Result<()> {
unsafe {
ok!(
self.raw.0,
ffi::sqlite3_rekey_v2(
self.raw.0,
std::ptr::null() as *const c_char,
str_to_cstr!(new_key.as_ref()).as_ptr() as *const c_void,
new_key.as_ref().len() as c_int,
)
);
}
Ok(())
}

/// Return the number of rows inserted, updated, or deleted by the most recent INSERT, UPDATE,
/// or DELETE statement.
#[inline]
Expand Down