Skip to content

Commit

Permalink
py-bindings: expose SecretKeyFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Mar 21, 2021
1 parent 320afa3 commit 3a9ac8b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions umbral-pre-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use pyo3::PyObjectProtocol;
use umbral_pre::SerializableToArray;

#[pyclass(module = "umbral")]
#[derive(PartialEq)]
pub struct SecretKey {
backend: umbral_pre::SecretKey,
}
Expand All @@ -33,6 +34,50 @@ impl SecretKey {
}
}

#[pyproto]
impl PyObjectProtocol for SecretKey {
fn __richcmp__(&self, other: PyRef<SecretKey>, op: CompareOp) -> PyResult<bool> {
match op {
CompareOp::Eq => Ok(self == &*other),
CompareOp::Ne => Ok(self != &*other),
_ => Err(PyTypeError::new_err("SecretKey objects are not ordered")),
}
}
}

#[pyclass(module = "umbral")]
pub struct SecretKeyFactory {
backend: umbral_pre::SecretKeyFactory,
}

#[pymethods]
impl SecretKeyFactory {
#[staticmethod]
pub fn random() -> Self {
Self {
backend: umbral_pre::SecretKeyFactory::random(),
}
}

pub fn secret_key_by_label(&self, label: &[u8]) -> Option<SecretKey> {
let backend_sk = self.backend.secret_key_by_label(label)?;
Some(SecretKey {
backend: backend_sk,
})
}

pub fn to_encrypted_bytes(&self, py: Python, password: &[u8]) -> Option<PyObject> {
let ciphertext = self.backend.to_encrypted_bytes(password)?;
Some(PyBytes::new(py, ciphertext.as_slice()).into())
}

#[staticmethod]
pub fn from_encrypted_bytes(password: &[u8], bytes: &[u8]) -> Option<Self> {
let sk = umbral_pre::SecretKeyFactory::from_encrypted_bytes(password, bytes)?;
Some(Self { backend: sk })
}
}

#[pyclass(module = "umbral")]
#[derive(PartialEq)]
pub struct PublicKey {
Expand Down Expand Up @@ -252,6 +297,7 @@ pub fn decrypt_reencrypted(
#[pymodule]
fn _umbral(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<SecretKey>()?;
m.add_class::<SecretKeyFactory>()?;
m.add_class::<PublicKey>()?;
m.add_class::<Capsule>()?;
m.add_class::<KeyFrag>()?;
Expand Down
1 change: 1 addition & 0 deletions umbral-pre-python/umbral_pre/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._umbral import (
SecretKey,
SecretKeyFactory,
PublicKey,
Capsule,
KeyFrag,
Expand Down

0 comments on commit 3a9ac8b

Please sign in to comment.