Add API to change database passphrase

New API is:
- dc_context_is_encrypted()
- dc_context_change_passphrase()
This commit is contained in:
link2xt
2022-01-29 18:38:42 +00:00
parent e73107006e
commit 91aa8cf259
5 changed files with 136 additions and 0 deletions

View File

@@ -241,6 +241,32 @@ int dc_context_open (dc_context_t *context, const char*
int dc_context_is_open (dc_context_t *context);
/**
* Return 1 if database is encrypted. Can only be checked on open database.
* Use this method to decide whether to present an option to change passphrase
* to the user.
*
* @member dc_context_t
* @param context The context object.
* @return 1 if database is encrypted, 0 if database is not encrypted or on
* error.
*/
int dc_context_is_encrypted (dc_context_t *context);
/**
* Changes passphrase for the open database. The database must be encrypted
* already, i.e. have a non-empty password. Unencrypted databases can only be
* encrypted during import/export.
* @memberof dc_context_t
* @param context The context object.
* @param passpharse New passphrase.
* @return 1 if database was reencrypted with the new passphrase, 0 on error
* (database is closed, database is not encrypted, other SQLCipher error).
*/
int dc_context_change_passphrase (dc_context_t *context, char *passphrase);
/**
* Free a context object.
*

View File

@@ -144,6 +144,34 @@ pub unsafe extern "C" fn dc_context_is_open(context: *mut dc_context_t) -> libc:
block_on(ctx.is_open()) as libc::c_int
}
#[no_mangle]
pub unsafe extern "C" fn dc_context_is_encrypted(context: *mut dc_context_t) -> libc::c_int {
if context.is_null() {
eprintln!("ignoring careless call to dc_context_is_encrypted()");
return 0;
}
let ctx = &*context;
block_on(ctx.is_encrypted()) as libc::c_int
}
#[no_mangle]
pub unsafe extern "C" fn dc_context_change_passphrase(
context: *mut dc_context_t,
passphrase: *const libc::c_char,
) -> libc::c_int {
if context.is_null() {
eprintln!("ignoring careless call to dc_context_change_passphrase()");
return 0;
}
let ctx = &*context;
let passphrase = to_string_lossy(passphrase);
block_on(ctx.change_passphrase(passphrase))
.log_err(ctx, "change_passphrase failed")
.is_ok() as libc::c_int
}
/// Release the context structure.
///
/// This function releases the memory of the `dc_context_t` structure.