-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.rs
More file actions
102 lines (92 loc) · 2.83 KB
/
Copy patharchive.rs
File metadata and controls
102 lines (92 loc) · 2.83 KB
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
use {
crate::error::Error,
anyhow::Result,
base64::{
engine::general_purpose::STANDARD,
Engine,
},
};
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Base64String(String);
impl Base64String {
pub fn new<T: AsRef<[u8]>>(v: T) -> Self {
Self(STANDARD.encode(v))
}
pub fn decode(&self) -> Result<Vec<u8>> {
match STANDARD.decode(&self.0) {
| Ok(v) => Ok(v),
| Err(_) => Err(Error::Decoding("base64".to_owned()).into()),
}
}
}
/// The archive that describes the single file storaing all information.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Archive {
/// Archive version.
pub version: String,
/// Unique ID.
pub uid: String,
/// Process ID.
pub pid: String,
/// Archive data.
pub data: Base64String,
}
/// The archive data.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct ArchiveData {
/// The actual share of the secret.
pub share: Share,
/// Share information.
pub info: ShareInfo,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum Checksum {
/// Sha-512
Sha512(String),
}
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct ShareInfo {
/// Some plain text comment.
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<String>,
/// Some information about the secret.
#[serde(skip_serializing_if = "Option::is_none")]
pub secret: Option<SecretInfo>,
}
// Describing an individual share.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum Share {
Plain {
data: Base64String,
checksum: Checksum,
},
Encrypted {
pass_hash: PassHash,
data: Base64String,
checksum: Checksum,
},
}
/// Describes the hash algorithm and value that is used for password
/// verification.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum PassHash {
/// Argon2id hash.
Argon2id(String),
}
/// Describes the secret that has been sharded. Contains information about
/// how to restore.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct SecretInfo {
/// The amount of shares that were generated for the secret.
pub num_shares: usize,
/// The amount of shares that are needed for restoring the secret.
pub threshold: usize,
}