-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from securesecrets/viewing-keys
Viewing keys
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod permit; | ||
pub mod transaction; | ||
pub mod viewing_keys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
use std::convert::TryInto; | ||
use sha2::{Digest, Sha256}; | ||
|
||
pub trait ViewingKey<const KEY_SIZE: usize>: ToString { | ||
fn compare_hashes(s1: &[u8], s2: &[u8]) -> bool { | ||
s1.eq(s2) | ||
} | ||
|
||
fn compare(&self, hashed: &[u8]) -> bool { | ||
Self::compare_hashes(&self.hash(), hashed) | ||
} | ||
|
||
fn hash(&self) -> [u8; KEY_SIZE] { | ||
Sha256::digest(self.to_string().as_bytes()) | ||
.as_slice() | ||
.try_into() | ||
.expect("Incorrect password length") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod viewing_key_tests { | ||
use crate::viewing_keys::ViewingKey; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
#[serde(rename_all = "snake_case")] | ||
struct Key(pub String); | ||
|
||
impl ToString for Key { | ||
fn to_string(&self) -> String { | ||
self.0.clone() | ||
} | ||
} | ||
|
||
impl ViewingKey<32> for Key {} | ||
|
||
#[test] | ||
fn hash_creation() { | ||
let pwd = Key("password".to_string()); | ||
|
||
let hashed = pwd.hash(); | ||
|
||
assert_eq!(hashed.len(), 32) | ||
} | ||
|
||
#[test] | ||
fn hash_comparing() { | ||
let pwd = Key("password".to_string()); | ||
let hashed = pwd.hash(); | ||
|
||
assert!(pwd.compare(&hashed)); | ||
assert!(Key::compare_hashes(&hashed, &Key("password".to_string()).hash())); | ||
|
||
let wrong_pwd = Key("wrong_password".to_string()); | ||
let wrong_hashed = wrong_pwd.hash(); | ||
|
||
for i in 0..32 { | ||
assert_ne!(hashed[i], wrong_hashed[i]); | ||
} | ||
assert!(!pwd.compare(&wrong_hashed)); | ||
assert!(!Key::compare_hashes(&hashed, &wrong_hashed)); | ||
} | ||
} |