Rename methods ahead of FFI support.

This commit is contained in:
Frederic Jacobs
2016-04-02 16:29:30 +02:00
parent 8c37b2f629
commit da01e4e450
4 changed files with 40 additions and 41 deletions

3
Cargo.lock generated
View File

@ -1,9 +1,8 @@
[root]
name = "rusty-secrets"
name = "rusty_secrets"
version = "0.0.1"
dependencies = [
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@ -1,5 +1,5 @@
[package]
name = "rusty-secrets"
name = "rusty_secrets"
version = "0.0.1"
description = "Implementation of threshold Shamir secret sharing in the Rust programming language."
license = "GPLv3"
@ -9,11 +9,11 @@ readme = "README.md"
getopts = "^0.2.14"
rustc-serialize = "^0.3.18"
rand = "^0.3.14"
libc = "^0.2.0"
[lib]
name = "rusty_secrets_lib"
name = "rusty_secrets"
path = "src/lib/mod.rs"
crate_type = ["rlib"]
[[bin]]
name = "rusty_secrets_bin"

View File

@ -14,6 +14,40 @@ 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>>> {
let shares = try!(secret_share(&*secret, k, n));
let config = base64::Config {
pad: false,
..base64::STANDARD
};
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();
result.push(string);
}
Ok(result)
}
pub fn recover_secret(k: u8, shares: Vec<(u8,Vec<u8>)>) -> io::Result<Vec<u8>> {
assert!(!shares.is_empty());
let slen = shares[0].1.len();
let mut col_in = Vec::with_capacity(k as usize);
let mut secret = Vec::with_capacity(slen);
for byteindex in 0 .. slen {
col_in.clear();
for s in shares.iter().take(k as usize) {
col_in.push((s.0, s.1[byteindex]));
}
secret.push(lagrange_interpolate(&*col_in, 0u8));
}
return Ok(secret) as io::Result<Vec<u8>>;
}
fn new_vec<T: Clone>(n: usize, x: T) -> Vec<T> {
repeat(x).take(n).collect()
}
@ -75,37 +109,3 @@ fn secret_share(src: &[u8], k: u8, n: u8) -> io::Result<Vec<Vec<u8>>> {
}
Ok(result)
}
pub fn perform_encode(k: u8, n: u8, secret: Vec<u8>) -> io::Result<Vec<Vec<u8>>> {
let shares = try!(secret_share(&*secret, k, n));
let config = base64::Config {
pad: false,
..base64::STANDARD
};
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();
result.push(string);
}
Ok(result)
}
pub fn perform_decode(k: u8, shares: Vec<(u8,Vec<u8>)>) -> io::Result<Vec<u8>> {
assert!(!shares.is_empty());
let slen = shares[0].1.len();
let mut col_in = Vec::with_capacity(k as usize);
let mut secret = Vec::with_capacity(slen);
for byteindex in 0 .. slen {
col_in.clear();
for s in shares.iter().take(k as usize) {
col_in.push((s.0, s.1[byteindex]));
}
secret.push(lagrange_interpolate(&*col_in, 0u8));
}
return Ok(secret) as io::Result<Vec<u8>>;
}

View File

@ -107,7 +107,7 @@ fn perform_encode_from_io(k: u8, n: u8) -> io::Result<()> {
}
tmp
};
match lib::perform_encode(k, n, secret) {
match lib::generate_shares(k, n, secret) {
Ok(shares) => {
for share in shares {println!("{:?}", str::from_utf8(&share).unwrap())};
}
@ -168,7 +168,7 @@ fn read_shares() -> io::Result<(u8, Vec<(u8,Vec<u8>)>)> {
fn perform_decode_from_io() -> io::Result<()> {
let (k, shares) = try!(read_shares());
return match lib::perform_decode(k, shares) {
return match lib::recover_secret(k, shares) {
Ok(secret) => {
let mut out = io::stdout();
try!(out.write_all(&*secret));