Skip to content

Commit 8c32092

Browse files
author
Alexander Weber
committed
archive versioning
1 parent 37816c6 commit 8c32092

2 files changed

Lines changed: 56 additions & 60 deletions

File tree

src/archive.rs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
pub const VERSION_ID_LEN: usize = 36;
2-
pub const VERSION_0_1_0: &'static str = "9f1e0683-7655-4f73-940a-38fa580b5725";
2+
pub const VERSION_0_1: &'static str = "9f1e0683-7655-4f73-940a-38fa580b5725";
33

4-
/// The archive that describes the single file storaing all information.
5-
#[derive(Debug, serde::Serialize, serde::Deserialize)]
6-
pub(crate) struct Archive {
7-
/// Version of this program with which the secret has been generated.
8-
pub version: String,
9-
/// Automatically generated unique ID of this archive.
10-
pub uid: String,
11-
/// The actual share of the secret.
12-
pub share: Share,
4+
pub mod v1 {
5+
/// The archive that describes the single file storaing all information.
6+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
7+
pub(crate) struct Archive {
8+
/// Automatically generated unique ID of this archive.
9+
pub uid: String,
10+
/// The actual share of the secret.
11+
pub share: Share,
1312

14-
/// This shares name.
15-
#[serde(skip_serializing_if = "Option::is_none")]
16-
pub name: Option<String>,
17-
/// Some plain text comment.
18-
#[serde(skip_serializing_if = "Option::is_none")]
19-
pub comment: Option<String>,
20-
/// Some information about the secret.
21-
#[serde(skip_serializing_if = "Option::is_none")]
22-
pub info: Option<SecretInfo>,
23-
}
13+
/// This shares name.
14+
#[serde(skip_serializing_if = "Option::is_none")]
15+
pub name: Option<String>,
16+
/// Some plain text comment.
17+
#[serde(skip_serializing_if = "Option::is_none")]
18+
pub comment: Option<String>,
19+
/// Some information about the secret.
20+
#[serde(skip_serializing_if = "Option::is_none")]
21+
pub info: Option<SecretInfo>,
22+
}
2423

25-
// Describing an individual share.
26-
#[derive(Debug, serde::Serialize, serde::Deserialize)]
27-
pub(crate) enum Share {
28-
/// Plain base64 encoded share data.
29-
PlainBase64(String),
30-
/// Symmetrically encrypted, base64 encoded share data.
31-
EncryptedBase64 { hash: Hash, data: String },
32-
}
24+
// Describing an individual share.
25+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
26+
pub(crate) enum Share {
27+
/// Plain base64 encoded share data.
28+
PlainBase64(String),
29+
/// Symmetrically encrypted, base64 encoded share data.
30+
EncryptedBase64 { hash: Hash, data: String },
31+
}
3332

34-
/// Describes the hash algorithm and value that is used for password
35-
/// verification.
36-
#[derive(Debug, serde::Serialize, serde::Deserialize)]
37-
pub(crate) enum Hash {
38-
/// Argon2id hash.
39-
Argon2id(String),
40-
}
33+
/// Describes the hash algorithm and value that is used for password
34+
/// verification.
35+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
36+
pub(crate) enum Hash {
37+
/// Argon2id hash.
38+
Argon2id(String),
39+
}
4140

42-
/// Describes the secret that has been sharded. Contains information about how
43-
/// to restore.
44-
#[derive(Debug, serde::Serialize, serde::Deserialize)]
45-
pub(crate) struct SecretInfo {
46-
/// The amount of shares that were generated for the secret.
47-
pub num_shares: usize,
48-
/// The amount of shares that are needed for restoring the secret.
49-
pub threshold: usize,
41+
/// Describes the secret that has been sharded. Contains information about
42+
/// how to restore.
43+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
44+
pub(crate) struct SecretInfo {
45+
/// The amount of shares that were generated for the secret.
46+
pub num_shares: usize,
47+
/// The amount of shares that are needed for restoring the secret.
48+
pub threshold: usize,
49+
}
5050
}

src/engine.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use {
22
crate::{
33
archive::{
4-
Archive,
5-
Hash,
6-
SecretInfo,
7-
Share,
8-
VERSION_0_1_0,
4+
v1,
5+
VERSION_0_1,
96
VERSION_ID_LEN,
107
},
118
blueprint::Blueprint,
@@ -54,13 +51,12 @@ impl<'x> SSS<'x> {
5451
)?;
5552

5653
for z in blueprint.generate.iter().zip(shares) {
57-
let share_data = Archive {
58-
version: VERSION_0_1_0.to_owned(),
54+
let share_data = v1::Archive {
5955
uid: Uuid::new_v4().hyphenated().to_string(),
6056
name: z.0.name.clone(),
6157
comment: z.0.comment.clone(),
6258
info: if z.0.info.unwrap_or(false) {
63-
Some(SecretInfo {
59+
Some(v1::SecretInfo {
6460
num_shares: blueprint.generate.len(),
6561
threshold: blueprint.threshold,
6662
})
@@ -82,20 +78,20 @@ impl<'x> SSS<'x> {
8278
.serialize()
8379
.to_string();
8480

85-
Share::EncryptedBase64 {
81+
v1::Share::EncryptedBase64 {
8682
data: STANDARD.encode(simplecrypt::encrypt(z.1.as_bytes(), pass.as_bytes())),
87-
hash: Hash::Argon2id(hash),
83+
hash: v1::Hash::Argon2id(hash),
8884
}
8985
},
9086
| None => {
9187
let encoded_share = STANDARD.encode(z.1);
92-
Share::PlainBase64(encoded_share)
88+
v1::Share::PlainBase64(encoded_share)
9389
},
9490
},
9591
};
9692
let share_data_str = STANDARD.encode(serde_yaml::to_string(&share_data)?);
9793

98-
fs::write(&z.0.path, format!("{}{}", VERSION_0_1_0, share_data_str))?;
94+
fs::write(&z.0.path, format!("{}{}", VERSION_0_1, share_data_str))?;
9995
}
10096
Ok(())
10197
}
@@ -106,11 +102,11 @@ impl<'x> SSS<'x> {
106102
let version = String::from_utf8(s.1[0..VERSION_ID_LEN].to_vec())?;
107103
let data = &s.1[VERSION_ID_LEN..];
108104
match version.as_str() {
109-
| VERSION_0_1_0 => {
110-
let archive = serde_yaml::from_str::<Archive>(&String::from_utf8(STANDARD.decode(&data)?)?)?;
105+
| VERSION_0_1 => {
106+
let archive = serde_yaml::from_str::<v1::Archive>(&String::from_utf8(STANDARD.decode(&data)?)?)?;
111107
let data = match archive.share {
112-
| Share::PlainBase64(v) => STANDARD.decode(v)?,
113-
| Share::EncryptedBase64 { hash, data } => {
108+
| v1::Share::PlainBase64(v) => STANDARD.decode(v)?,
109+
| v1::Share::EncryptedBase64 { hash, data } => {
114110
let pw: String = dialoguer::Password::new()
115111
.with_prompt(format!(
116112
"Enter password for share (path: {}, name: {})",
@@ -119,7 +115,7 @@ impl<'x> SSS<'x> {
119115
))
120116
.interact()?;
121117
match hash {
122-
| Hash::Argon2id(v) => {
118+
| v1::Hash::Argon2id(v) => {
123119
let pw_hash = argon2::PasswordHash::new(&v).or(Err(Error::PasswordVerification))?;
124120
self.argon
125121
.verify_password(pw.as_bytes(), &pw_hash)

0 commit comments

Comments
 (0)