Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul committed Sep 17, 2024
1 parent 288148d commit 64a9591
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ impl From<PickleError> for PyErr {
}
}

/// An error type describing failures which can happen during the use of `PkEncryption`
/// and `PkDecryption` objects.
#[derive(Debug, Error)]
pub enum PkEncryptionError {
#[error("The key doesn't have the correct size, got {0}, expected 32 bytes")]
Expand Down
14 changes: 7 additions & 7 deletions src/types/curve25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Curve25519PublicKey {
}
}

/// A Curve25519 secret key.
#[pyclass]
#[derive(Clone)]
pub struct Curve25519SecretKey {
Expand All @@ -71,13 +72,15 @@ impl From<vodozemac::Curve25519SecretKey> for Curve25519SecretKey {

#[pymethods]
impl Curve25519SecretKey {
/// Generate a new, random, Curve25519SecretKey.
#[new]
fn new() -> Self {
Self {
inner: vodozemac::Curve25519SecretKey::new(),
}
}

/// Create a `Curve25519SecretKey` from the given base64-encoded string.
#[classmethod]
pub fn from_base64(_cls: &Bound<'_, PyType>, key: &str) -> Result<Self, KeyError> {
Self::from_bytes(
Expand All @@ -88,6 +91,7 @@ impl Curve25519SecretKey {
)
}

/// Create a `Curve25519SecretKey` from the given byte array.
#[classmethod]
pub fn from_bytes(_cls: &Bound<'_, PyType>, bytes: &[u8]) -> Result<Self, KeyError> {
let key: &[u8; 32] = bytes.try_into().map_err(|_| {
Expand All @@ -103,24 +107,20 @@ impl Curve25519SecretKey {
})
}

/// Convert the `Curve25519SecretKey` to a base64-encoded string.
pub fn to_base64(&self) -> String {
base64_encode(self.inner.to_bytes().as_slice())
}

/// Convert the `Curve25519SecretKey` to a byte array.
pub fn to_bytes(&self) -> Py<PyBytes> {
convert_to_pybytes(self.inner.to_bytes().as_slice())
}

/// Give the `Curve25519PublicKey` associated with this `Curve25519SecretKey`.
pub fn public_key(&self) -> Curve25519PublicKey {
Curve25519PublicKey {
inner: vodozemac::Curve25519PublicKey::from(&self.inner),
}
}

#[classattr]
const __hash__: Option<PyObject> = None;

fn __eq__(&self, other: &Self) -> bool {
self.inner.to_bytes() == other.inner.to_bytes()
}
}

0 comments on commit 64a9591

Please sign in to comment.