mirror of
https://github.com/mii443/RustySecrets.git
synced 2025-08-22 16:25:32 +00:00
Working on test vectors.
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1,6 +1,6 @@
|
||||
[root]
|
||||
name = "rusty_secrets"
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
dependencies = [
|
||||
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -6,7 +6,6 @@ license = "GPLv3"
|
||||
readme = "README.md"
|
||||
build = "build.rs"
|
||||
|
||||
|
||||
[dependencies]
|
||||
getopts = "^0.2.14"
|
||||
rustc-serialize = "^0.3.18"
|
||||
|
@ -14,7 +14,7 @@ use std::iter::repeat;
|
||||
pub mod custom_error;
|
||||
use self::custom_error::*;
|
||||
|
||||
pub fn generate_shares(k: u8, n: u8, secret: Vec<u8>) -> io::Result<Vec<Vec<u8>>> {
|
||||
pub fn generate_shares(k: u8, n: u8, secret: &Vec<u8>) -> io::Result<Vec<String>> {
|
||||
let shares = try!(secret_share(&*secret, k, n));
|
||||
let config = base64::Config {
|
||||
pad: false,
|
||||
@ -24,8 +24,8 @@ pub fn generate_shares(k: u8, n: u8, secret: Vec<u8>) -> io::Result<Vec<Vec<u8>>
|
||||
let mut result = Vec::with_capacity(n as usize);
|
||||
|
||||
for (index, share) in shares.iter().enumerate() {
|
||||
let salad = share.to_base64(config);
|
||||
let string = format!("{}-{}-{}", k, index+1, salad).into_bytes();
|
||||
let b64_share = share.to_base64(config);
|
||||
let string = format!("{}-{}-{}", k, index+1, b64_share);
|
||||
result.push(string);
|
||||
}
|
||||
|
||||
@ -39,8 +39,8 @@ pub fn process_shares(shares_strings: Vec<String>) -> io::Result<(u8, Vec<(u8,Ve
|
||||
|
||||
for line in shares_strings {
|
||||
let parts: Vec<_> = line.trim().split('-').collect();
|
||||
if parts.len() < 3 || parts.len() > 4 {
|
||||
return Err(other_io_err("Share parse error: Expected 3 or 4 \
|
||||
if parts.len() != 3 {
|
||||
return Err(other_io_err("Share parse error: Expected 3
|
||||
parts separated by a minus sign", None));
|
||||
}
|
||||
let (k, n, p3) = {
|
||||
|
@ -106,9 +106,9 @@ fn perform_encode_from_io(k: u8, n: u8) -> io::Result<()> {
|
||||
tmp.pop();
|
||||
tmp
|
||||
};
|
||||
match lib::generate_shares(k, n, secret) {
|
||||
match lib::generate_shares(k, n, &secret) {
|
||||
Ok(shares) => {
|
||||
for share in shares {println!("{:?}", str::from_utf8(&share).unwrap())};
|
||||
for share in shares {println!("{:?}", share)};
|
||||
}
|
||||
Err(e) => { return Err(e) as io::Result<()>; }
|
||||
}
|
||||
|
17
tests/lib.rs
Normal file
17
tests/lib.rs
Normal file
@ -0,0 +1,17 @@
|
||||
extern crate rusty_secrets;
|
||||
|
||||
use rusty_secrets::{recover_secret, generate_shares};
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_reasonable_splits() {
|
||||
let max_shares = 50;
|
||||
let secret = String::from("I grew up with the understanding that the world I lived in was one where people enjoyed a sort of freedom to communicate with each other in privacy, without it being monitored, without it being measured or analyzed or sort of judged by these shadowy figures or systems, any time they mention anything that travels across public lines.").into_bytes();
|
||||
for k in 2..max_shares {
|
||||
for n in k..max_shares{
|
||||
let shares = generate_shares(k,n, &secret).unwrap();
|
||||
println!("Testing {} out-of- {}", k, n);
|
||||
assert_eq!(secret, recover_secret(shares).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
137
tests/test_vectors.rs
Normal file
137
tests/test_vectors.rs
Normal file
@ -0,0 +1,137 @@
|
||||
extern crate rusty_secrets;
|
||||
|
||||
use rusty_secrets::{recover_secret};
|
||||
|
||||
#[test]
|
||||
fn test_recover_sellibitze() {
|
||||
let share1 = "2-1-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-4-F7rAjX3UOa53KA".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
let mut secret = "My secret".to_string().into_bytes();
|
||||
secret.push(10);
|
||||
assert_eq!(recover_secret(shares).unwrap(), secret);
|
||||
}
|
||||
|
||||
// Generated with code on master branch on the 6th of April.
|
||||
#[test]
|
||||
fn test_recover_es_test_vectors() {
|
||||
let share1 = "5-1-DbuicpLQiCf7bVWiAz8eCpQGpdZmYQ7z2j2+g351tWFLOQPTZkXY8BYfwGGGjkOoz1g9x0ScmLFcWk+2tign".to_string();
|
||||
let share2 = "5-2-nShdfkY5+SlfybMyqjHXCZ01bq5N/0Lkf0nQZw5x3bnHIEVfa0RA4YcJ4SjG/UdpgO/gOcyLRkSp2Dwf8bvw".to_string();
|
||||
let share3 = "5-3-qEhJ3IVEdbDkiRoy+jOJ/KuGE9jWyGeOYEcDwPfEV8E9rfD1Bc17BQAbJ51Xd8oexS2M1qMvNgJHZUQZbUgQ".to_string();
|
||||
let share4 = "5-6-yyVPUeaYPPiWK0wIV5OQ/t61V0lSEO+7X++EWeHRlIq3sRBNwUpKNfx/C+Vc9xTzUftrqBKvkWDZQal7nyi2".to_string();
|
||||
let share5 = "5-7-i8iL6bVf272B3qIjp0QqSny6AIm+DkP7oQjkVVLvx9EMhlvd4HJOxPpmtNF/RjA/zz21d7DY/B//saOPpBQa".to_string();
|
||||
|
||||
let shares = vec![share1, share2, share3, share4, share5];
|
||||
|
||||
let secret = "The immoral cannot be made moral through the use of secret law.".to_string().into_bytes();
|
||||
assert_eq!(recover_secret(shares).unwrap(), secret);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_recover_sellibitze_more_than_threshold_shars() {
|
||||
let share1 = "2-1-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-4-F7rAjX3UOa53KA".to_string();
|
||||
let share3 = "2-2-YJZQDGm22Y77Gw".to_string();
|
||||
let share4 = "2-5-j0P4PHsw4lW+rg".to_string();
|
||||
|
||||
let shares = vec![share1, share2, share3, share4];
|
||||
|
||||
let mut secret = "My secret".to_string().into_bytes();
|
||||
secret.push(10);
|
||||
assert_eq!(recover_secret(shares).unwrap(), secret);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_sellibitze_missing_share() {
|
||||
let share1 = "2-1-1YAYwmOHqZ69jA".to_string();
|
||||
let shares = vec![share1];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_sellibitze_no_shares() {
|
||||
let shares = vec![];
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_2_parts_share() {
|
||||
let share1 = "2-1-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-F7rAjX3UOa53KA".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_incorrect_share_num() {
|
||||
let share1 = "2-1-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-DEFINITLY_NAN-YJZQDGm22Y77Gw".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_0_share_num() {
|
||||
let share1 = "2-0-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-1-YJZQDGm22Y77Gw".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_invalid_b64() {
|
||||
let share1 = "2-0-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-1-YJZQDG((((m22Y)))77Gw".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_invalid_b64_size() {
|
||||
let share1 = "2-0-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-1-YJZQDGm22Y77GwZ69jA".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_duplicate_shares() {
|
||||
let share1 = "2-0-1YAYwmOHqZ69jA".to_string();
|
||||
let share2 = "2-0-1YAYwmOHqZ69jA".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_recover_too_few_shares() {
|
||||
let share1 = "5-1-DbuicpLQiCf7bVWiAz8eCpQGpdZmYQ7z2j2+g351tWFLOQPTZkXY8BYfwGGGjkOoz1g9x0ScmLFcWk+2tign".to_string();
|
||||
let share2 = "5-2-nShdfkY5+SlfybMyqjHXCZ01bq5N/0Lkf0nQZw5x3bnHIEVfa0RA4YcJ4SjG/UdpgO/gOcyLRkSp2Dwf8bvw".to_string();
|
||||
|
||||
let shares = vec![share1, share2];
|
||||
|
||||
recover_secret(shares).unwrap();
|
||||
}
|
Reference in New Issue
Block a user