Skip to content
Snippets Groups Projects
Commit 1a44728f authored by Éloïs's avatar Éloïs
Browse files

[enh] #13 add keypair type

parent 1417b5dc
Branches
Tags
1 merge request!9Resolve "Add keyPair type"
...@@ -211,6 +211,45 @@ impl super::PrivateKey for PrivateKey { ...@@ -211,6 +211,45 @@ impl super::PrivateKey for PrivateKey {
} }
} }
/// Store a ed25519 cryptographic key pair (`PublicKey` + `PrivateKey`)
#[derive(Debug, Copy, Clone)]
pub struct KeyPair {
/// Store a Ed25519 public key.
pub pubkey:PublicKey,
/// Store a Ed25519 private key.
privkey:PrivateKey,
}
impl Display for KeyPair {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.pubkey.to_base58())
}
}
impl PartialEq<KeyPair> for KeyPair {
fn eq(&self, other: &KeyPair) -> bool {
self.pubkey.eq(&other.pubkey) && self.privkey.eq(&other.privkey)
}
}
impl Eq for KeyPair {}
impl super::KeyPair for KeyPair {
type Signature = Signature;
fn new(password: &[u8], salt: &[u8]) -> KeyPair {
let generator = self::KeyPairGenerator::with_default_parameters();
let (privkey, pubkey) = generator.generate(password, salt);
KeyPair {
pubkey,
privkey
}
}
fn sign(&self, message: &[u8]) -> Signature {
Signature(crypto::ed25519::signature(message, &self.privkey.0))
}
}
/// Keypair generator with given parameters for `scrypt` keypair function. /// Keypair generator with given parameters for `scrypt` keypair function.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct KeyPairGenerator { pub struct KeyPairGenerator {
...@@ -264,7 +303,7 @@ impl KeyPairGenerator { ...@@ -264,7 +303,7 @@ impl KeyPairGenerator {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use {PrivateKey, PublicKey, Signature}; use {PrivateKey, PublicKey, Signature, KeyPair};
#[test] #[test]
fn base58_private_key() { fn base58_private_key() {
...@@ -394,4 +433,20 @@ Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 ...@@ -394,4 +433,20 @@ Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
assert_eq!(sig, expected_signature); assert_eq!(sig, expected_signature);
assert!(pubkey.verify(message.as_bytes(), &sig)); assert!(pubkey.verify(message.as_bytes(), &sig));
} }
#[test]
fn keypair_generate_sign_and_verify () {
let keypair = super::KeyPair::new("password".as_bytes(), "salt".as_bytes());
let message = "Version: 10
Type: Identity
Currency: duniter_unit_test_currency
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
UniqueID: tic
Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
";
let sig = keypair.sign(message.as_bytes());
assert!(keypair.pubkey.verify(message.as_bytes(), &sig));
}
} }
...@@ -147,3 +147,15 @@ pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 { ...@@ -147,3 +147,15 @@ pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
/// Sign a message with this private key. /// Sign a message with this private key.
fn sign(&self, message: &[u8]) -> Self::Signature; fn sign(&self, message: &[u8]) -> Self::Signature;
} }
/// Store a cryptographic key pair (`PublicKey` + `PrivateKey`)
pub trait KeyPair: Clone + Display + Debug + PartialEq + Eq {
/// Signature type of associated cryptosystem.
type Signature: Signature;
/// Create a new KeyPair
fn new(password: &[u8], salt: &[u8]) -> Self;
/// Sign a message with privkey.
fn sign(&self, message: &[u8]) -> Self::Signature;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment