Removed Hash struct, cleaned the code. Renamed Wyrd to wyrd_ns.
This commit is contained in:
+66
-113
@@ -5,14 +5,16 @@ use rand::{thread_rng, Rng};
|
||||
use std::fs;
|
||||
use std::fmt;
|
||||
use std::path::Path;
|
||||
use std::io::Error as IoError;
|
||||
use serde::export::fmt::Error;
|
||||
use serde::{Serialize, Deserialize, Serializer, Deserializer};
|
||||
// For deserialization
|
||||
use serde::de::{Error as DeError, Visitor};
|
||||
use serde::export::Formatter;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Signature {
|
||||
private_key: KeyPrivate,
|
||||
public_key: KeyPublic,
|
||||
private_key: Key,
|
||||
public_key: Key,
|
||||
}
|
||||
|
||||
impl Signature {
|
||||
@@ -21,12 +23,12 @@ impl Signature {
|
||||
let mut rng = thread_rng();
|
||||
rng.fill(&mut buf);
|
||||
let (private, public) = keypair(&buf);
|
||||
Signature {private_key: KeyPrivate::new(&private), public_key: KeyPublic::new(&public)}
|
||||
Signature {private_key: Key::from_bytes(&private), public_key: Key::from_bytes(&public)}
|
||||
}
|
||||
|
||||
pub fn from_bytes(seed: &[u8]) -> Self {
|
||||
let (private, public) = keypair(&seed);
|
||||
Signature {private_key: KeyPrivate::new(&private), public_key: KeyPublic::new(&public)}
|
||||
Signature {private_key: Key::from_bytes(&private), public_key: Key::from_bytes(&public)}
|
||||
}
|
||||
|
||||
pub fn from_file(filename: &str, _password: &str) -> Option<Self> {
|
||||
@@ -40,11 +42,11 @@ impl Signature {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_public(&self) -> KeyPublic {
|
||||
pub fn get_public(&self) -> Key {
|
||||
self.public_key.clone()
|
||||
}
|
||||
|
||||
pub fn get_private(&self) -> KeyPrivate {
|
||||
pub fn get_private(&self) -> Key {
|
||||
self.private_key.clone()
|
||||
}
|
||||
|
||||
@@ -53,37 +55,53 @@ impl Signature {
|
||||
}
|
||||
|
||||
pub fn check(&self, message: &[u8], public_key: &[u8], signature: &[u8]) -> bool {
|
||||
verify(message, &self.public_key.data, signature)
|
||||
verify(message, public_key, signature)
|
||||
}
|
||||
}
|
||||
|
||||
/*impl fmt::Debug for Signature {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.debug_struct("Signature")
|
||||
.field("pub", &&self.public_key[..])
|
||||
.field("priv", &&self.private_key[..])
|
||||
.finish()
|
||||
}
|
||||
}*/
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct KeyPublic {
|
||||
data: [u8; 32]
|
||||
#[derive(Clone)]
|
||||
pub struct Key {
|
||||
data: Vec<u8>
|
||||
}
|
||||
|
||||
impl KeyPublic {
|
||||
pub fn new(data: &[u8]) -> Self {
|
||||
let mut buf = [0u8; 32];
|
||||
buf.copy_from_slice(data);
|
||||
KeyPublic{ data: buf }
|
||||
impl Key {
|
||||
pub fn new(data: Vec<u8>) -> Self {
|
||||
Key { data }
|
||||
}
|
||||
|
||||
pub fn from_bytes(data: &[u8]) -> Self {
|
||||
Key { data: Vec::from(data) }
|
||||
}
|
||||
|
||||
pub fn length(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.data.is_empty()
|
||||
}
|
||||
|
||||
/// Returns a byte slice of the hash contents.
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn zero32() -> Self {
|
||||
Key { data: [0u8; 32].to_vec() }
|
||||
}
|
||||
|
||||
pub fn zero64() -> Self {
|
||||
Key { data: [0u8; 64].to_vec() }
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for KeyPublic {
|
||||
impl Default for Key {
|
||||
fn default() -> Key {
|
||||
Key { data: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Key {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
crate::utils::same_hash(&self.data, &other.data)
|
||||
}
|
||||
@@ -93,112 +111,47 @@ impl PartialEq for KeyPublic {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for KeyPublic {
|
||||
impl fmt::Debug for Key {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str(&crate::utils::to_hex(&self.data))
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for KeyPublic {
|
||||
impl Serialize for Key {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
|
||||
S: Serializer {
|
||||
serializer.serialize_str(&crate::utils::to_hex(&self.data))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct KeyPrivate {
|
||||
data: [u8; 64]
|
||||
}
|
||||
struct KeyVisitor;
|
||||
|
||||
impl KeyPrivate {
|
||||
pub fn new(data: &[u8]) -> Self {
|
||||
let mut buf = [0u8; 64];
|
||||
buf.copy_from_slice(data);
|
||||
KeyPrivate{ data: buf }
|
||||
}
|
||||
|
||||
pub fn length(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for KeyPrivate {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
crate::utils::same_hash(&self.data, &other.data)
|
||||
}
|
||||
|
||||
fn ne(&self, other: &Self) -> bool {
|
||||
!crate::utils::same_hash(&self.data, &other.data)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for KeyPrivate {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str(&crate::utils::to_hex(&self.data))
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for KeyPrivate {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
|
||||
S: Serializer {
|
||||
serializer.serialize_str(&crate::utils::to_hex(&self.data))
|
||||
}
|
||||
}
|
||||
|
||||
use serde::de::{Error as DeError, Visitor};
|
||||
use serde::export::Formatter;
|
||||
|
||||
struct PublicVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for PublicVisitor {
|
||||
type Value = KeyPublic;
|
||||
impl<'de> Visitor<'de> for KeyVisitor {
|
||||
type Value = Key;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
||||
formatter.write_str("32 bytes")
|
||||
formatter.write_str("32 or 64 bytes")
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> where E: DeError, {
|
||||
if v.len() == 32 {
|
||||
let mut h = [0; 32];
|
||||
h[..32].copy_from_slice(&v[..32]);
|
||||
Ok(KeyPublic::new(&h))
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> where E: DeError, {
|
||||
if value.len() == 64 || value.len() == 128 {
|
||||
Ok(Key::new(crate::from_hex(value).unwrap()))
|
||||
} else {
|
||||
Err(E::custom("KeyPublic must be 32 bytes!"))
|
||||
Err(E::custom("Key must be 32 or 64 bytes!"))
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E> where E: DeError, {
|
||||
if value.len() == 32 || value.len() == 64 {
|
||||
Ok(Key::from_bytes(value))
|
||||
} else {
|
||||
Err(E::custom("Key must be 32 or 64 bytes!"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'dd> Deserialize<'dd> for KeyPublic {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'dd>>::Error> where
|
||||
D: Deserializer<'dd> {
|
||||
deserializer.deserialize_bytes(PublicVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct PrivateVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for PrivateVisitor {
|
||||
type Value = KeyPrivate;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
||||
formatter.write_str("32 bytes")
|
||||
}
|
||||
|
||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> where E: DeError, {
|
||||
if v.len() == 64 {
|
||||
let mut h = [0; 64];
|
||||
h[..64].copy_from_slice(&v[..64]);
|
||||
Ok(KeyPrivate::new(&h))
|
||||
} else {
|
||||
Err(E::custom("KeyPrivate must be 64 bytes!"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'dd> Deserialize<'dd> for KeyPrivate {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'dd>>::Error> where
|
||||
D: Deserializer<'dd> {
|
||||
deserializer.deserialize_bytes(PrivateVisitor)
|
||||
impl<'dd> Deserialize<'dd> for Key {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'dd>>::Error> where D: Deserializer<'dd> {
|
||||
deserializer.deserialize_str(KeyVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user