mirror of
https://github.com/mii443/RustySecrets.git
synced 2025-08-23 00:35:38 +00:00
Rename methods ahead of FFI support.
This commit is contained in:
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -1,9 +1,8 @@
|
|||||||
[root]
|
[root]
|
||||||
name = "rusty-secrets"
|
name = "rusty_secrets"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rusty-secrets"
|
name = "rusty_secrets"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
description = "Implementation of threshold Shamir secret sharing in the Rust programming language."
|
description = "Implementation of threshold Shamir secret sharing in the Rust programming language."
|
||||||
license = "GPLv3"
|
license = "GPLv3"
|
||||||
@ -9,11 +9,11 @@ readme = "README.md"
|
|||||||
getopts = "^0.2.14"
|
getopts = "^0.2.14"
|
||||||
rustc-serialize = "^0.3.18"
|
rustc-serialize = "^0.3.18"
|
||||||
rand = "^0.3.14"
|
rand = "^0.3.14"
|
||||||
libc = "^0.2.0"
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "rusty_secrets_lib"
|
name = "rusty_secrets"
|
||||||
path = "src/lib/mod.rs"
|
path = "src/lib/mod.rs"
|
||||||
|
crate_type = ["rlib"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "rusty_secrets_bin"
|
name = "rusty_secrets_bin"
|
||||||
|
@ -14,6 +14,40 @@ use std::iter::repeat;
|
|||||||
pub mod custom_error;
|
pub mod custom_error;
|
||||||
use self::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> {
|
fn new_vec<T: Clone>(n: usize, x: T) -> Vec<T> {
|
||||||
repeat(x).take(n).collect()
|
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)
|
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>>;
|
|
||||||
}
|
|
||||||
|
@ -107,7 +107,7 @@ fn perform_encode_from_io(k: u8, n: u8) -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
tmp
|
tmp
|
||||||
};
|
};
|
||||||
match lib::perform_encode(k, n, secret) {
|
match lib::generate_shares(k, n, secret) {
|
||||||
Ok(shares) => {
|
Ok(shares) => {
|
||||||
for share in shares {println!("{:?}", str::from_utf8(&share).unwrap())};
|
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<()> {
|
fn perform_decode_from_io() -> io::Result<()> {
|
||||||
let (k, shares) = try!(read_shares());
|
let (k, shares) = try!(read_shares());
|
||||||
|
|
||||||
return match lib::perform_decode(k, shares) {
|
return match lib::recover_secret(k, shares) {
|
||||||
Ok(secret) => {
|
Ok(secret) => {
|
||||||
let mut out = io::stdout();
|
let mut out = io::stdout();
|
||||||
try!(out.write_all(&*secret));
|
try!(out.write_all(&*secret));
|
||||||
|
Reference in New Issue
Block a user