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

#13 use KeyPair in doc and ed25519 generator

parent a5da3b2a
No related branches found
No related tags found
No related merge requests found
...@@ -217,7 +217,7 @@ pub struct KeyPair { ...@@ -217,7 +217,7 @@ pub struct KeyPair {
/// Store a Ed25519 public key. /// Store a Ed25519 public key.
pub pubkey:PublicKey, pub pubkey:PublicKey,
/// Store a Ed25519 private key. /// Store a Ed25519 private key.
privkey:PrivateKey, pub privkey:PrivateKey,
} }
impl Display for KeyPair { impl Display for KeyPair {
...@@ -236,14 +236,9 @@ impl Eq for KeyPair {} ...@@ -236,14 +236,9 @@ impl Eq for KeyPair {}
impl super::KeyPair for KeyPair { impl super::KeyPair for KeyPair {
type Signature = Signature; type Signature = Signature;
fn from_passwd_and_salt(password: &[u8], salt: &[u8]) -> KeyPair {
fn new(password: &[u8], salt: &[u8]) -> KeyPair {
let generator = self::KeyPairGenerator::with_default_parameters(); let generator = self::KeyPairGenerator::with_default_parameters();
let (privkey, pubkey) = generator.generate(password, salt); generator.generate(password, salt)
KeyPair {
pubkey,
privkey
}
} }
fn sign(&self, message: &[u8]) -> Signature { fn sign(&self, message: &[u8]) -> Signature {
Signature(crypto::ed25519::signature(message, &self.privkey.0)) Signature(crypto::ed25519::signature(message, &self.privkey.0))
...@@ -286,7 +281,7 @@ impl KeyPairGenerator { ...@@ -286,7 +281,7 @@ impl KeyPairGenerator {
/// ///
/// The [`PublicKey`](struct.PublicKey.html) will be able to verify messaged signed with /// The [`PublicKey`](struct.PublicKey.html) will be able to verify messaged signed with
/// the [`PrivateKey`](struct.PrivateKey.html). /// the [`PrivateKey`](struct.PrivateKey.html).
pub fn generate(&self, password: &[u8], salt: &[u8]) -> (PrivateKey, PublicKey) { pub fn generate(&self, password: &[u8], salt: &[u8]) -> KeyPair {
let mut seed = [0u8; 32]; let mut seed = [0u8; 32];
crypto::scrypt::scrypt( crypto::scrypt::scrypt(
password, password,
...@@ -296,7 +291,10 @@ impl KeyPairGenerator { ...@@ -296,7 +291,10 @@ impl KeyPairGenerator {
); );
let (private, public) = crypto::ed25519::keypair(&seed); let (private, public) = crypto::ed25519::keypair(&seed);
(PrivateKey(private), PublicKey(public)) KeyPair {
pubkey: PublicKey(public),
privkey: PrivateKey(private),
}
} }
} }
...@@ -436,7 +434,7 @@ Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 ...@@ -436,7 +434,7 @@ Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
#[test] #[test]
fn keypair_generate_sign_and_verify () { fn keypair_generate_sign_and_verify () {
let keypair = super::KeyPair::new("password".as_bytes(), "salt".as_bytes()); let keypair = super::KeyPair::from_passwd_and_salt("password".as_bytes(), "salt".as_bytes());
let message = "Version: 10 let message = "Version: 10
Type: Identity Type: Identity
......
...@@ -21,21 +21,21 @@ ...@@ -21,21 +21,21 @@
//! # Usage //! # Usage
//! //!
//! ``` //! ```
//! use duniter_keys::{Signature, PublicKey, PrivateKey}; //! use duniter_keys::{Signature, PublicKey, PrivateKey, KeyPair};
//! use duniter_keys::ed25519::KeyPairGenerator; //! use duniter_keys::ed25519::KeyPairGenerator;
//! //!
//! let generator = KeyPairGenerator::with_default_parameters(); //! let generator = KeyPairGenerator::with_default_parameters();
//! //!
//! let (private_key, public_key) = generator.generate( //! let keypair = generator.generate(
//! b"password", //! b"password",
//! b"salt" //! b"salt"
//! ); //! );
//! //!
//! let message = "Hello, world!"; //! let message = "Hello, world!";
//! //!
//! let signature = private_key.sign(&message.as_bytes()); //! let signature = keypair.sign(&message.as_bytes());
//! //!
//! assert!(public_key.verify(&message.as_bytes(), &signature)); //! assert!(keypair.pubkey.verify(&message.as_bytes(), &signature));
//! ``` //! ```
//! //!
//! # Format //! # Format
...@@ -153,8 +153,8 @@ pub trait KeyPair: Clone + Display + Debug + PartialEq + Eq { ...@@ -153,8 +153,8 @@ pub trait KeyPair: Clone + Display + Debug + PartialEq + Eq {
/// Signature type of associated cryptosystem. /// Signature type of associated cryptosystem.
type Signature: Signature; type Signature: Signature;
/// Create a new KeyPair /// Generate a new KeyPair from passwd and salt
fn new(password: &[u8], salt: &[u8]) -> Self; fn from_passwd_and_salt(password: &[u8], salt: &[u8]) -> Self;
/// Sign a message with privkey. /// Sign a message with privkey.
fn sign(&self, message: &[u8]) -> Self::Signature; 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