pub fn unlock(shares: &[String]) -> Result<Vec<u8>>
Expand description
Attempt to unlock the secret given some shares
.
Notes
- If there aren’t enough shares to meet the threshold defined when the shares were created the resulting vector of bytes will be gibberish.
- If there are more shares supplied than were defined when the shares were created the resulting vector of bytes will be gibberish.
Errors
- This function will generate an error if the
shares
map is empty. - This function will generate an error if the
shares
within the map are not all the same length.
Example
// Generate 5 shares from the given secret
let secret = "correct horse battery staple".as_bytes();
let config = SsssConfig::default();
// Generate 5 shares to be distributed, requiring a minimum of 3 later
// to unlock the secret
let mut shares = gen_shares(&config, &secret)?;
// Check that all 5 shares can unlock the secret
assert_eq!(shares.len(), 5);
assert_eq!(unlock(&shares)?, secret);
// Remove a random share from `shares` and check that 4 shares can unlock
// the secret
let mut rng = thread_rng();
remove_random_entry(&mut rng, &mut shares);
assert_eq!(shares.len(), 4);
assert_eq!(unlock(&shares)?, secret);
// Remove another random share from `shares` and check that 3 shares can unlock
// the secret
remove_random_entry(&mut rng, &mut shares);
assert_eq!(shares.len(), 3);
assert_eq!(unlock(&shares)?, secret);
// Remove another random share from `shares` and check that 2 shares *CANNOT*
// unlock the secret
remove_random_entry(&mut rng, &mut shares);
assert_eq!(shares.len(), 2);
assert_ne!(unlock(&shares)?, secret);