diff options
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/async_util/backoff.rs (renamed from core/src/async_utils/backoff.rs) | 2 | ||||
-rw-r--r-- | core/src/async_util/condvar.rs (renamed from core/src/async_utils/condvar.rs) | 4 | ||||
-rw-r--r-- | core/src/async_util/condwait.rs (renamed from core/src/async_utils/condwait.rs) | 2 | ||||
-rw-r--r-- | core/src/async_util/mod.rs (renamed from core/src/async_utils/mod.rs) | 0 | ||||
-rw-r--r-- | core/src/async_util/select.rs (renamed from core/src/async_utils/select.rs) | 2 | ||||
-rw-r--r-- | core/src/async_util/task_group.rs (renamed from core/src/async_utils/task_group.rs) | 2 | ||||
-rw-r--r-- | core/src/async_util/timeout.rs (renamed from core/src/async_utils/timeout.rs) | 2 | ||||
-rw-r--r-- | core/src/error.rs | 6 | ||||
-rw-r--r-- | core/src/event.rs | 2 | ||||
-rw-r--r-- | core/src/key_pair.rs | 189 | ||||
-rw-r--r-- | core/src/lib.rs | 9 | ||||
-rw-r--r-- | core/src/pubsub.rs | 2 | ||||
-rw-r--r-- | core/src/util/decode.rs (renamed from core/src/utils/decode.rs) | 0 | ||||
-rw-r--r-- | core/src/util/encode.rs (renamed from core/src/utils/encode.rs) | 0 | ||||
-rw-r--r-- | core/src/util/mod.rs (renamed from core/src/utils/mod.rs) | 0 | ||||
-rw-r--r-- | core/src/util/path.rs (renamed from core/src/utils/path.rs) | 0 |
16 files changed, 210 insertions, 12 deletions
diff --git a/core/src/async_utils/backoff.rs b/core/src/async_util/backoff.rs index f7e131d..a231229 100644 --- a/core/src/async_utils/backoff.rs +++ b/core/src/async_util/backoff.rs @@ -12,7 +12,7 @@ use smol::Timer; /// # Examples /// /// ``` -/// use karyons_core::async_utils::Backoff; +/// use karyons_core::async_util::Backoff; /// /// async { /// let backoff = Backoff::new(300, 3000); diff --git a/core/src/async_utils/condvar.rs b/core/src/async_util/condvar.rs index 814f78f..7396d0d 100644 --- a/core/src/async_utils/condvar.rs +++ b/core/src/async_util/condvar.rs @@ -8,7 +8,7 @@ use std::{ use smol::lock::MutexGuard; -use crate::utils::random_16; +use crate::util::random_16; /// CondVar is an async version of <https://doc.rust-lang.org/std/sync/struct.Condvar.html> /// @@ -19,7 +19,7 @@ use crate::utils::random_16; /// /// use smol::lock::Mutex; /// -/// use karyons_core::async_utils::CondVar; +/// use karyons_core::async_util::CondVar; /// /// async { /// diff --git a/core/src/async_utils/condwait.rs b/core/src/async_util/condwait.rs index e31fac3..cd4b269 100644 --- a/core/src/async_utils/condwait.rs +++ b/core/src/async_util/condwait.rs @@ -9,7 +9,7 @@ use super::CondVar; ///``` /// use std::sync::Arc; /// -/// use karyons_core::async_utils::CondWait; +/// use karyons_core::async_util::CondWait; /// /// async { /// let cond_wait = Arc::new(CondWait::new()); diff --git a/core/src/async_utils/mod.rs b/core/src/async_util/mod.rs index c871bad..c871bad 100644 --- a/core/src/async_utils/mod.rs +++ b/core/src/async_util/mod.rs diff --git a/core/src/async_utils/select.rs b/core/src/async_util/select.rs index 9fe3c77..8f2f7f6 100644 --- a/core/src/async_utils/select.rs +++ b/core/src/async_util/select.rs @@ -12,7 +12,7 @@ use smol::future::Future; /// ``` /// use std::future; /// -/// use karyons_core::async_utils::{select, Either}; +/// use karyons_core::async_util::{select, Either}; /// /// async { /// let fut1 = future::pending::<String>(); diff --git a/core/src/async_utils/task_group.rs b/core/src/async_util/task_group.rs index afc9648..3fc0cb7 100644 --- a/core/src/async_utils/task_group.rs +++ b/core/src/async_util/task_group.rs @@ -14,7 +14,7 @@ use super::{select, CondWait, Either}; /// /// use std::sync::Arc; /// -/// use karyons_core::async_utils::TaskGroup; +/// use karyons_core::async_util::TaskGroup; /// /// async { /// diff --git a/core/src/async_utils/timeout.rs b/core/src/async_util/timeout.rs index 7c55e1b..6ab35c4 100644 --- a/core/src/async_utils/timeout.rs +++ b/core/src/async_util/timeout.rs @@ -13,7 +13,7 @@ use crate::{error::Error, Result}; /// ``` /// use std::{future, time::Duration}; /// -/// use karyons_core::async_utils::timeout; +/// use karyons_core::async_util::timeout; /// /// async { /// let fut = future::pending::<()>(); diff --git a/core/src/error.rs b/core/src/error.rs index 63b45d3..7c547c4 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -7,12 +7,18 @@ pub enum Error { #[error(transparent)] IO(#[from] std::io::Error), + #[error("TryInto Error: {0}")] + TryInto(&'static str), + #[error("Timeout Error")] Timeout, #[error("Path Not Found Error: {0}")] PathNotFound(&'static str), + #[error(transparent)] + Ed25519(#[from] ed25519_dalek::ed25519::Error), + #[error("Channel Send Error: {0}")] ChannelSend(String), diff --git a/core/src/event.rs b/core/src/event.rs index 0503e88..f2c5510 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -12,7 +12,7 @@ use smol::{ lock::Mutex, }; -use crate::{utils::random_16, Result}; +use crate::{util::random_16, Result}; pub type ArcEventSys<T> = Arc<EventSys<T>>; pub type WeakEventSys<T> = Weak<EventSys<T>>; diff --git a/core/src/key_pair.rs b/core/src/key_pair.rs new file mode 100644 index 0000000..4016351 --- /dev/null +++ b/core/src/key_pair.rs @@ -0,0 +1,189 @@ +use ed25519_dalek::{Signer as _, Verifier as _}; +use rand::rngs::OsRng; + +use crate::{error::Error, Result}; + +/// key cryptography type +pub enum KeyPairType { + Ed25519, +} + +/// A Public key +pub struct PublicKey(PublicKeyInner); + +/// A Secret key +pub struct SecretKey(Vec<u8>); + +impl PublicKey { + pub fn as_bytes(&self) -> &[u8] { + self.0.as_bytes() + } + + /// Verify a signature on a message with this public key. + pub fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> { + self.0.verify(msg, signature) + } +} + +impl PublicKey { + pub fn from_bytes(kp_type: &KeyPairType, pk: &[u8]) -> Result<Self> { + Ok(Self(PublicKeyInner::from_bytes(kp_type, pk)?)) + } +} + +/// A KeyPair. +#[derive(Clone)] +pub struct KeyPair(KeyPairInner); + +impl KeyPair { + /// Generate a new random keypair. + pub fn generate(kp_type: &KeyPairType) -> Self { + Self(KeyPairInner::generate(kp_type)) + } + + /// Sign a message using the private key. + pub fn sign(&self, msg: &[u8]) -> Vec<u8> { + self.0.sign(msg) + } + + /// Get the public key of this keypair. + pub fn public(&self) -> PublicKey { + self.0.public() + } + + /// Get the secret key of this keypair. + pub fn secret(&self) -> SecretKey { + self.0.secret() + } +} + +/// An extension trait, adding essential methods to all [`KeyPair`] types. +trait KeyPairExt { + /// Sign a message using the private key. + fn sign(&self, msg: &[u8]) -> Vec<u8>; + + /// Get the public key of this keypair. + fn public(&self) -> PublicKey; + + /// Get the secret key of this keypair. + fn secret(&self) -> SecretKey; +} + +#[derive(Clone)] +enum KeyPairInner { + Ed25519(Ed25519KeyPair), +} + +impl KeyPairInner { + fn generate(kp_type: &KeyPairType) -> Self { + match kp_type { + KeyPairType::Ed25519 => Self::Ed25519(Ed25519KeyPair::generate()), + } + } +} + +impl KeyPairExt for KeyPairInner { + fn sign(&self, msg: &[u8]) -> Vec<u8> { + match self { + KeyPairInner::Ed25519(kp) => kp.sign(msg), + } + } + + fn public(&self) -> PublicKey { + match self { + KeyPairInner::Ed25519(kp) => kp.public(), + } + } + + fn secret(&self) -> SecretKey { + match self { + KeyPairInner::Ed25519(kp) => kp.secret(), + } + } +} + +#[derive(Clone)] +struct Ed25519KeyPair(ed25519_dalek::SigningKey); + +impl Ed25519KeyPair { + fn generate() -> Self { + Self(ed25519_dalek::SigningKey::generate(&mut OsRng)) + } +} + +impl KeyPairExt for Ed25519KeyPair { + fn sign(&self, msg: &[u8]) -> Vec<u8> { + self.0.sign(msg).to_bytes().to_vec() + } + + fn public(&self) -> PublicKey { + PublicKey(PublicKeyInner::Ed25519(Ed25519PublicKey( + self.0.verifying_key(), + ))) + } + + fn secret(&self) -> SecretKey { + SecretKey(self.0.to_bytes().to_vec()) + } +} + +/// An extension trait, adding essential methods to all [`PublicKey`] types. +trait PublicKeyExt { + fn as_bytes(&self) -> &[u8]; + + /// Verify a signature on a message with this public key. + fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()>; +} + +enum PublicKeyInner { + Ed25519(Ed25519PublicKey), +} + +impl PublicKeyInner { + pub fn from_bytes(kp_type: &KeyPairType, pk: &[u8]) -> Result<Self> { + match kp_type { + KeyPairType::Ed25519 => Ok(Self::Ed25519(Ed25519PublicKey::from_bytes(pk)?)), + } + } +} + +impl PublicKeyExt for PublicKeyInner { + fn as_bytes(&self) -> &[u8] { + match self { + Self::Ed25519(pk) => pk.as_bytes(), + } + } + + fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> { + match self { + Self::Ed25519(pk) => pk.verify(msg, signature), + } + } +} + +struct Ed25519PublicKey(ed25519_dalek::VerifyingKey); + +impl Ed25519PublicKey { + pub fn from_bytes(pk: &[u8]) -> Result<Self> { + let pk_bytes: [u8; 32] = pk + .try_into() + .map_err(|_| Error::TryInto("Failed to convert slice to [u8; 32]"))?; + + Ok(Self(ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes)?)) + } +} + +impl PublicKeyExt for Ed25519PublicKey { + fn as_bytes(&self) -> &[u8] { + self.0.as_bytes() + } + + fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> { + let sig_bytes: [u8; 64] = signature + .try_into() + .map_err(|_| Error::TryInto("Failed to convert slice to [u8; 64]"))?; + self.0 + .verify(msg, &ed25519_dalek::Signature::from_bytes(&sig_bytes))?; + Ok(()) + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index 67e6610..276ed89 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,19 +1,22 @@ /// A set of helper tools and functions. -pub mod utils; +pub mod util; /// A module containing async utilities that work with the /// [`smol`](https://github.com/smol-rs/smol) async runtime. -pub mod async_utils; +pub mod async_util; /// Represents karyons's Core Error. pub mod error; -/// [`event::EventSys`] Implementation +/// [`event::EventSys`] implementation. pub mod event; /// A simple publish-subscribe system [`Read More`](./pubsub/struct.Publisher.html) pub mod pubsub; +/// A cryptographic key pair +pub mod key_pair; + use smol::Executor as SmolEx; use std::sync::Arc; diff --git a/core/src/pubsub.rs b/core/src/pubsub.rs index 4cc0ab7..306d42f 100644 --- a/core/src/pubsub.rs +++ b/core/src/pubsub.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, sync::Arc}; use log::error; use smol::lock::Mutex; -use crate::{utils::random_16, Result}; +use crate::{util::random_16, Result}; pub type ArcPublisher<T> = Arc<Publisher<T>>; pub type SubscriptionID = u16; diff --git a/core/src/utils/decode.rs b/core/src/util/decode.rs index a8a6522..a8a6522 100644 --- a/core/src/utils/decode.rs +++ b/core/src/util/decode.rs diff --git a/core/src/utils/encode.rs b/core/src/util/encode.rs index 7d1061b..7d1061b 100644 --- a/core/src/utils/encode.rs +++ b/core/src/util/encode.rs diff --git a/core/src/utils/mod.rs b/core/src/util/mod.rs index a3c3f50..a3c3f50 100644 --- a/core/src/utils/mod.rs +++ b/core/src/util/mod.rs diff --git a/core/src/utils/path.rs b/core/src/util/path.rs index 2cd900a..2cd900a 100644 --- a/core/src/utils/path.rs +++ b/core/src/util/path.rs |