Skip to content
Snippets Groups Projects
Commit 665d5052 authored by nanocryk's avatar nanocryk
Browse files

Merge branch...

Merge branch '8-publickey-privatekey-signature-associated-type-must-implement-signature-trait' into 'dev'

Resolve "{PublicKey/PrivateKey}::Signature associated type must implement Signature trait"

Closes #8

See merge request !5
parents 3d5bb988 3643d5f3
No related branches found
No related tags found
1 merge request!5Resolve "{PublicKey/PrivateKey}::Signature associated type must implement Signature trait"
......@@ -33,7 +33,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "duniter-keys"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
......
[package]
name = "duniter-keys"
version = "0.1.0"
version = "0.2.0"
authors = ["nanocryk <nanocryk@duniter.org>"]
description = "Manage cryptographic keys for the Duniter project."
repository = "https://git.duniter.org/nodes/rust/duniter-rs"
......
......@@ -24,19 +24,13 @@ use std::fmt::Error;
use std::fmt::Formatter;
use std::fmt::Debug;
use base58::{ToBase58, FromBase58, FromBase58Error};
use base58::{FromBase58, FromBase58Error, ToBase58};
use base64;
use base64::DecodeError;
use crypto;
use super::BaseConvertionError;
// ---------------------------- //
// ----- struct Signature ----- //
// ---------------------------- //
/// Store a ed25519 signature.
#[derive(Clone, Copy)]
pub struct Signature(pub [u8; 64]);
......@@ -58,9 +52,9 @@ impl super::Signature for Signature {
Err(DecodeError::InvalidByte(pos, byte)) => {
Err(BaseConvertionError::InvalidCharacter(byte as char, pos))
}
Err(DecodeError::InvalidLength) => Err(
BaseConvertionError::InvalidBaseConverterLength(),
),
Err(DecodeError::InvalidLength) => {
Err(BaseConvertionError::InvalidBaseConverterLength())
}
}
}
......@@ -93,12 +87,6 @@ impl PartialEq<Signature> for Signature {
impl Eq for Signature {}
// ---------------------------- //
// ----- struct PublicKey ----- //
// ---------------------------- //
/// Store a Ed25519 public key.
///
/// Can be generated with [`KeyPairGenerator`].
......@@ -142,12 +130,12 @@ impl super::PublicKey for PublicKey {
Err(BaseConvertionError::InvalidKeyLendth(result.len(), 32))
}
}
Err(FromBase58Error::InvalidBase58Character(character, pos)) => Err(
BaseConvertionError::InvalidCharacter(character, pos),
),
Err(FromBase58Error::InvalidBase58Length) => Err(
BaseConvertionError::InvalidBaseConverterLength(),
),
Err(FromBase58Error::InvalidBase58Character(character, pos)) => {
Err(BaseConvertionError::InvalidCharacter(character, pos))
}
Err(FromBase58Error::InvalidBase58Length) => {
Err(BaseConvertionError::InvalidBaseConverterLength())
}
}
}
......@@ -156,12 +144,6 @@ impl super::PublicKey for PublicKey {
}
}
// ----------------------------- //
// ----- struct PrivateKey ----- //
// ----------------------------- //
/// Store a Ed25519 private key.
///
/// Can be generated with [`KeyPairGenerator`].
......@@ -214,12 +196,12 @@ impl super::PrivateKey for PrivateKey {
Err(BaseConvertionError::InvalidKeyLendth(result.len(), 64))
}
}
Err(FromBase58Error::InvalidBase58Character(character, pos)) => Err(
BaseConvertionError::InvalidCharacter(character, pos),
),
Err(FromBase58Error::InvalidBase58Length) => Err(
BaseConvertionError::InvalidBaseConverterLength(),
),
Err(FromBase58Error::InvalidBase58Character(character, pos)) => {
Err(BaseConvertionError::InvalidCharacter(character, pos))
}
Err(FromBase58Error::InvalidBase58Length) => {
Err(BaseConvertionError::InvalidBaseConverterLength())
}
}
}
......@@ -229,13 +211,6 @@ impl super::PrivateKey for PrivateKey {
}
}
// ----------------------------------- //
// ----- struct KeyPairGenerator ----- //
// ----------------------------------- //
/// Keypair generator with given parameters for `scrypt` keypair function.
#[derive(Debug, Copy, Clone)]
pub struct KeyPairGenerator {
......@@ -286,20 +261,15 @@ impl KeyPairGenerator {
}
}
// ---------------------- //
// ----- UNIT TESTS ----- //
// ---------------------- //
#[cfg(test)]
mod tests {
use super::*;
use {Signature, PublicKey, PrivateKey};
use {PrivateKey, PublicKey, Signature};
#[test]
fn base58_private_key() {
let private58 = "468Q1XtTq7h84NorZdWBZFJrGkB18CbmbHr9tkp9snt5GiERP7ySs3wM8myLccbAAGejgMRC9r\
let private58 =
"468Q1XtTq7h84NorZdWBZFJrGkB18CbmbHr9tkp9snt5GiERP7ySs3wM8myLccbAAGejgMRC9r\
qnXuW3iAfZACm7";
let private_key = super::PrivateKey::from_base58(private58).unwrap();
let private_raw = private58.from_base58().unwrap();
......@@ -354,7 +324,8 @@ mod tests {
BaseConvertionError::InvalidKeyLendth(35, 32)
);
assert_eq!(
super::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQd").unwrap_err(),
super::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQd")
.unwrap_err(),
BaseConvertionError::InvalidKeyLendth(31, 32)
);
assert_eq!(
......@@ -397,8 +368,8 @@ mod tests {
#[test]
fn message_sign_verify() {
let pubkey = super::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV")
.unwrap();
let pubkey =
super::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV").unwrap();
let prikey = super::PrivateKey::from_base58(
"468Q1XtTq7h84NorZdWBZFJrGkB18CbmbHr9tkp9snt\
......
// Copyright (C) 2017 The Duniter Project Developers.
// Copyright (C) 2018 The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
......@@ -46,12 +46,9 @@
//! `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`
//! with `=` as padding character.
#![deny(missing_docs,
missing_debug_implementations, missing_copy_implementations,
trivial_casts, trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces, unused_qualifications)]
#![deny(missing_docs, missing_debug_implementations, missing_copy_implementations, trivial_casts,
trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces,
unused_qualifications)]
extern crate base58;
extern crate base64;
......@@ -64,10 +61,6 @@ use base58::ToBase58;
pub mod ed25519;
// ------------------------------------ //
// ----- enum BaseConvertionError ----- //
// ------------------------------------ //
/// Errors enumeration for Base58/64 strings convertion.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BaseConvertionError {
......@@ -79,11 +72,6 @@ pub enum BaseConvertionError {
InvalidBaseConverterLength(),
}
// --------------------------- //
// ----- trait Signature ----- //
// --------------------------- //
/// Store a cryptographic signature.
///
/// A signature can be converted from/to Base64 format.
......@@ -108,11 +96,6 @@ pub trait Signature: Clone + Display + Debug + PartialEq + Eq {
fn to_base64(&self) -> String;
}
// ---------------------------- //
// ----- struct PublicKey ----- //
// ---------------------------- //
/// Store a cryptographic public key.
///
/// A `PublicKey` can be converted from/to Base64 format.
......@@ -123,10 +106,8 @@ pub trait Signature: Clone + Display + Debug + PartialEq + Eq {
///
/// [`PrivateKey`]: trait.PrivateKey.html
pub trait PublicKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
/// [`Signature`] type of associated cryptosystem.
///
/// [`Signature`]: trait.Signature.html
type Signature;
/// Signature type of associated cryptosystem.
type Signature: Signature;
/// Create a PublicKey from a Base58 string.
///
......@@ -141,11 +122,6 @@ pub trait PublicKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool;
}
// ----------------------------- //
// ----- struct PrivateKey ----- //
// ----------------------------- //
/// Store a cryptographic private key.
///
/// A `PrivateKey` can be converted from/to Base58 format.
......@@ -156,10 +132,8 @@ pub trait PublicKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
///
/// [`PublicKey`]: trait.PublicKey.html
pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
/// [`Signature`] type of associated cryptosystem.
///
/// [`Signature`]: trait.Signature.html
type Signature;
/// Signature type of associated cryptosystem.
type Signature: Signature;
/// Create a PrivateKey from a Base58 string.
///
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment