Skip to content

Commit

Permalink
NIP-04: Allow decrypting to raw bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Dec 6, 2023
1 parent 29b113e commit 8767d7b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions crates/nostr/src/nips/nip04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ where
))
}

/// Decrypt
pub fn decrypt<S>(
/// Decrypts content to bytes
pub fn decrypt_to_bytes<S>(
sk: &SecretKey,
pk: &XOnlyPublicKey,
encrypted_content: S,
) -> Result<String, Error>
) -> Result<Vec<u8>, Error>
where
S: Into<String>,
{
Expand All @@ -134,6 +134,19 @@ where
.decrypt_padded_vec_mut::<Pkcs7>(&encrypted_content)
.map_err(|_| Error::WrongBlockMode)?;

Ok(result)
}

/// Decrypts content to a UTF-8 string
pub fn decrypt<S>(
sk: &SecretKey,
pk: &XOnlyPublicKey,
encrypted_content: S,
) -> Result<String, Error>
where
S: Into<String>,
{
let result = decrypt_to_bytes(sk, pk, encrypted_content)?;
String::from_utf8(result).map_err(|_| Error::Utf8Encode)
}

Expand Down

0 comments on commit 8767d7b

Please sign in to comment.