Skip to content

Commit

Permalink
feat: print error on decode failure instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
taturosati committed Jun 15, 2024
1 parent 6d6e602 commit 9e5edb2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn sync_with_api(api: Api, key_pair: KeyPair) -> anyhow::Result<Credential
impl App {
pub async fn new(device_pass: &str) -> anyhow::Result<App> {
let sk_enc = read_sk()?;
let key_pair = KeyPair::from_sk(sk_enc.as_slice(), device_pass);
let key_pair = KeyPair::try_from_sk(sk_enc.as_slice(), device_pass)?;

let api = Api::new(key_pair.clone());

Expand Down
9 changes: 5 additions & 4 deletions common/src/crypto/asymmetric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ impl KeyPair {
})
}

pub fn from_sk(sk: &[u8], password: &str) -> KeyPair {
pub fn try_from_sk(sk: &[u8], password: &str) -> anyhow::Result<KeyPair> {
let private_key = RsaPrivateKey::from_pkcs8_encrypted_der(sk, password)
.expect("Failed to create private key");
.map_err(|_| anyhow::format_err!("Invalid device password"))?;

let public_key = private_key.to_public_key();
let signing_key = SigningKey::from(private_key.clone());
let verifying_key = VerifyingKey::from(public_key.clone());

KeyPair {
Ok(KeyPair {
private_key,
public_key,
signing_key,
verifying_key,
}
})
}

pub fn encrypt(&self, message: &str) -> String {
Expand Down

0 comments on commit 9e5edb2

Please sign in to comment.