· 7 years ago · May 07, 2018, 08:14 AM
1use base64;
2use failure;
3
4#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
5pub struct SecretKey(Option<Vec<u8>>);
6
7impl SecretKey {
8 // constructors
9 pub fn from_base64_encoded_string(s: String) -> Result<SecretKey, failure::Error> {
10 let bytes = base64::decode_config(&s, base64::STANDARD)?;
11 Ok(SecretKey(Some(bytes)))
12 }
13 pub fn from_string(s: String) -> SecretKey {
14 SecretKey(Some(s.into_bytes()))
15 }
16 pub fn from_bytes(b: Vec<u8>) -> SecretKey {
17 SecretKey(Some(b))
18 }
19 pub fn none() -> SecretKey {
20 SecretKey(None)
21 }
22
23 // getters
24 pub fn bytes(&self) -> &[u8] {
25 match (*self).0 {
26 Some(ref b) => b,
27 None => &[],
28 }
29 }
30 pub fn bytes_mut(&mut self) -> &mut [u8] {
31 match (*self).0 {
32 Some(ref mut b) => b,
33 None => &mut [],
34 }
35 }
36 pub fn into_bytes(self) -> Vec<u8> {
37 match self.0 {
38 Some(b) => b,
39 None => vec![],
40 }
41 }
42}