1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::{
algorithm::{AES128GCM_ALG_ID, AES256GCM_ALG_ID, ED25519_ALG_ID},
encoding::Encodable,
error::Error,
};
mod aes;
mod ed25519;
pub use self::aes::{Aes128GcmKey, Aes256GcmKey};
pub use self::ed25519::Ed25519SecretKey;
pub enum SecretKey {
Aes128Gcm(Aes128GcmKey),
Aes256Gcm(Aes256GcmKey),
Ed25519(Ed25519SecretKey),
}
impl SecretKey {
pub fn new(alg: &str, slice: &[u8]) -> Result<Self, Error> {
let result = match alg {
AES128GCM_ALG_ID => SecretKey::Aes128Gcm(Aes128GcmKey::from_slice(slice)?),
AES256GCM_ALG_ID => SecretKey::Aes256Gcm(Aes256GcmKey::from_slice(slice)?),
ED25519_ALG_ID => SecretKey::Ed25519(Ed25519SecretKey::from_slice(slice)?),
_ => fail!(AlgorithmInvalid, "{}", alg),
};
Ok(result)
}
pub fn aes128gcm_key(&self) -> Option<&Aes128GcmKey> {
match self {
SecretKey::Aes128Gcm(ref key) => Some(key),
_ => None,
}
}
pub fn is_aes128gcm_key(&self) -> bool {
self.aes128gcm_key().is_some()
}
pub fn aes256gcm_key(&self) -> Option<&Aes256GcmKey> {
match self {
SecretKey::Aes256Gcm(ref key) => Some(key),
_ => None,
}
}
pub fn is_aes256gcm_key(&self) -> bool {
self.aes256gcm_key().is_some()
}
pub fn ed25519_key(&self) -> Option<&Ed25519SecretKey> {
match self {
SecretKey::Ed25519(ref digest) => Some(digest),
_ => None,
}
}
pub fn is_ed25519_key(&self) -> bool {
self.ed25519_key().is_some()
}
}
impl Encodable for SecretKey {
fn to_uri_string(&self) -> String {
match self {
SecretKey::Aes128Gcm(ref key) => key.to_uri_string(),
SecretKey::Aes256Gcm(ref key) => key.to_uri_string(),
SecretKey::Ed25519(ref key) => key.to_uri_string(),
}
}
fn to_dasherized_string(&self) -> String {
match self {
SecretKey::Aes128Gcm(ref key) => key.to_uri_string(),
SecretKey::Aes256Gcm(ref key) => key.to_uri_string(),
SecretKey::Ed25519(ref key) => key.to_dasherized_string(),
}
}
}
pub trait AsSecretSlice {
fn as_secret_slice(&self) -> &[u8];
}