feat: v1.1.0, add external sign/ecdh support
This commit is contained in:
58
src/lib.rs
58
src/lib.rs
@@ -133,14 +133,7 @@ pub fn private_key_sign(private_key_representation: &[u8], content: &[u8]) -> XR
|
||||
cmd.arg(STANDARD.encode(content));
|
||||
|
||||
let cmd_stdout = run_command_stdout(cmd)?;
|
||||
|
||||
if is_success(&cmd_stdout)? {
|
||||
let sign_result: SignResult = from_str(&cmd_stdout)?;
|
||||
Ok(STANDARD.decode(&sign_result.signature_base64)?)
|
||||
} else {
|
||||
let error_result: ErrorResult = from_str(&cmd_stdout)?;
|
||||
simple_error!("{}", error_result.error)
|
||||
}
|
||||
parse_sign_result(&cmd_stdout)
|
||||
}
|
||||
|
||||
// ephemera_public_key MUST be DER format public key
|
||||
@@ -156,12 +149,51 @@ pub fn private_key_ecdh(
|
||||
cmd.arg(STANDARD.encode(ephemera_public_key));
|
||||
|
||||
let cmd_stdout = run_command_stdout(cmd)?;
|
||||
parse_dh_result(&cmd_stdout)
|
||||
}
|
||||
|
||||
if is_success(&cmd_stdout)? {
|
||||
let dh_result: DhResult = from_str(&cmd_stdout)?;
|
||||
pub fn external_sign(external_command: &str, parameter: &str, alg: &str, content: &[u8]) -> XResult<Vec<u8>> {
|
||||
let mut cmd = Command::new(external_command);
|
||||
cmd.arg("external_sign");
|
||||
cmd.arg("--parameter");
|
||||
cmd.arg(parameter);
|
||||
cmd.arg("--alg");
|
||||
cmd.arg(alg);
|
||||
cmd.arg("--message-base64");
|
||||
cmd.arg(STANDARD.encode(content));
|
||||
|
||||
let cmd_stdout = run_command_stdout(cmd)?;
|
||||
parse_sign_result(&cmd_stdout)
|
||||
}
|
||||
|
||||
pub fn external_ecdh(external_command: &str, parameter: &str, ephemera_public_key: &[u8]) -> XResult<Vec<u8>> {
|
||||
let mut cmd = Command::new(external_command);
|
||||
cmd.arg("external_ecdh");
|
||||
cmd.arg("--parameter");
|
||||
cmd.arg(parameter);
|
||||
cmd.arg("--epk");
|
||||
cmd.arg(STANDARD.encode(ephemera_public_key));
|
||||
|
||||
let cmd_stdout = run_command_stdout(cmd)?;
|
||||
parse_dh_result(&cmd_stdout)
|
||||
}
|
||||
|
||||
fn parse_sign_result(stdout: &str) -> XResult<Vec<u8>> {
|
||||
if is_success(stdout)? {
|
||||
let sign_result: SignResult = from_str(stdout)?;
|
||||
Ok(STANDARD.decode(&sign_result.signature_base64)?)
|
||||
} else {
|
||||
let error_result: ErrorResult = from_str(stdout)?;
|
||||
simple_error!("{}", error_result.error)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_dh_result(stdout: &str) -> XResult<Vec<u8>> {
|
||||
if is_success(stdout)? {
|
||||
let dh_result: DhResult = from_str(stdout)?;
|
||||
Ok(hex::decode(&dh_result.shared_secret_hex)?)
|
||||
} else {
|
||||
let error_result: ErrorResult = from_str(&cmd_stdout)?;
|
||||
let error_result: ErrorResult = from_str(stdout)?;
|
||||
simple_error!("{}", error_result.error)
|
||||
}
|
||||
}
|
||||
@@ -180,7 +212,9 @@ fn run_command(mut cmd: Command) -> XResult<Output> {
|
||||
Ok(output) => {
|
||||
debugging!("Output: {:?}", output);
|
||||
if !output.status.success() {
|
||||
simple_error!("Run command not success: {:?}", output.status.code())
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
simple_error!("Run command not success: {:?}\n - stdout: {}\n - stderr: {}", output.status.code(), stdout, stderr)
|
||||
} else {
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user