Skip to content

Commit 3589681

Browse files
committed
Make project build on stable Rust 1.28
* Remove reliance on unstable feature int_to_from_bytes * Add a fallback method until feature becomes stable in Rust 1.29 * See rust-lang/rust#51835 * Remove reliance on experimental feature attr_literals * Only used for `structopt`, but it provides `raw()` * See rust-lang/rust#34981 * Make tests run again
1 parent 960090e commit 3589681

File tree

7 files changed

+47
-35
lines changed

7 files changed

+47
-35
lines changed

src/cli/args.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ pub enum Command {
3737
#[structopt(name = "touch")]
3838
/// Initialize an empty vault file
3939
Touch {
40-
#[structopt(short = "f", long = "force", takes_value = false)]
40+
#[structopt(short = "f", long = "force", raw(takes_value = "false"))]
4141
/// Overwrite an existing file
4242
force: bool,
4343
},
4444
#[structopt(name = "ls")]
4545
/// List all records in a vault
4646
List {
47-
#[structopt(long = "disclose", takes_value = false)]
47+
#[structopt(long = "disclose", raw(takes_value = "false"))]
4848
/// Disclose secrets
4949
disclose: bool,
5050
},
@@ -58,13 +58,15 @@ pub enum OtpCommand {
5858
#[structopt(
5959
requires = "secret",
6060
long = "totp",
61-
takes_value = false,
61+
raw(takes_value = "false"),
6262
group = "algo",
6363
conflicts_with = "hotp"
6464
)]
6565
/// Use TOTP as the generation algorithm
6666
totp: bool,
67-
#[structopt(requires = "secret", long = "hotp", takes_value = false, group = "algo")]
67+
#[structopt(
68+
requires = "secret", long = "hotp", raw(takes_value = "false"), group = "algo"
69+
)]
6870
/// Use HOTP as the generation algorithm
6971
hotp: bool,
7072
/// A label for this secret
@@ -195,7 +197,7 @@ pub fn match_args(sigil: Sigil) -> Result<(), Error> {
195197
cli::password::remove_record(&vault?, &key?, ctx?, record)
196198
}
197199
PasswordCommand::GetPassword { record } => {
198-
cli::password::get_password(&vault?, ctx?, record)
200+
cli::password::get_password(&vault?, ctx?, &record)
199201
}
200202
PasswordCommand::Generate { chars } => cli::password::generate_password(chars),
201203
},
@@ -237,7 +239,7 @@ pub fn match_args(sigil: Sigil) -> Result<(), Error> {
237239
}
238240
OtpCommand::ImportUrl { url } => cli::otp::import_url(&vault?, &key?, ctx?, &url),
239241
OtpCommand::GetToken { record, counter } => {
240-
cli::otp::get_token(&vault?, ctx?, record, counter)
242+
cli::otp::get_token(&vault?, ctx?, &record, counter)
241243
}
242244
OtpCommand::Remove { record } => cli::otp::remove_record(&vault?, &key?, ctx?, record),
243245
},

src/cli/otp/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use std::path::PathBuf;
1212
pub fn get_token(
1313
vault_path: &PathBuf,
1414
mut ctx: Context,
15-
record_id: String,
15+
record_id: &str,
1616
counter: Option<u64>,
1717
) -> Result<(), Error> {
1818
tracepoint!();
1919

2020
// (1)
2121
let vault = utils::read_vault(&vault_path, &mut ctx).unwrap();
22-
let record = vault.get_otp_record(record_id)?;
22+
let record = vault.get_otp_record(&record_id)?;
2323

2424
// (2)
2525
tracepoint!();

src/cli/password/get.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ use std::path::PathBuf;
99
* 1. `read_vault`, `vault.get_record`, bail on error
1010
* 2. Return the `password` field
1111
*/
12-
pub fn get_password(
13-
vault_path: &PathBuf,
14-
mut ctx: Context,
15-
record_id: String,
16-
) -> Result<(), Error> {
12+
pub fn get_password(vault_path: &PathBuf, mut ctx: Context, record_id: &str) -> Result<(), Error> {
1713
tracepoint!();
1814

1915
// (1)
2016
let vault = utils::read_vault(&vault_path, &mut ctx).unwrap();
21-
let record = vault.get_record(record_id)?;
17+
let record = vault.get_record(&record_id)?;
2218

2319
// (2)
2420
println!("{}", record.password);

src/lib/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
pub enum VaultError {
33
#[fail(display = "Record should be updated, not added")]
44
ShouldUpdate,
5-
#[fail(display = "Failed to find record {}", 0)]
6-
UnknownRecord(String),
5+
#[fail(display = "Failed to find a matching record")]
6+
UnknownRecord,
77
#[fail(display = "Vault path already exists")]
88
Overwriting,
99
#[fail(display = "Vault path is a directory")]

src/lib/otp.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ pub fn hotp(K: &str, C: u64, N: u32, algorithm: &HmacAlgorithm) -> u32 {
7474
// (2)
7575
tracepoint!();
7676
let K = hmac::SigningKey::new(algorithm.to_algorithm(), K.as_ref());
77-
// Swap bytes because of endianess
78-
debug!("Counter ({}) is {:?}", C, C.swap_bytes().to_bytes());
79-
let H = hmac::sign(&K, &C.swap_bytes().to_bytes());
77+
// TODO Use int_to_from_bytes (see Rust PR #51835) when stabilized
78+
// in Rust 1.29. When using it, .swap_bytes() because of endianess
79+
println!("Counter ({}) is {:?}", C, u64_into_bytes(C));
80+
let H = hmac::sign(&K, &u64_into_bytes(C));
8081
debug!(
8182
"Signed digest is {}",
8283
H.as_ref()
@@ -111,7 +112,7 @@ pub fn hotp(K: &str, C: u64, N: u32, algorithm: &HmacAlgorithm) -> u32 {
111112
#[cfg(test)]
112113
mod tests {
113114
use lib::otp;
114-
use ring::digest;
115+
use lib::types::HmacAlgorithm;
115116

116117
// Test values provided in RFC 4226
117118
// Base32 for "12345678901234567890";
@@ -124,7 +125,7 @@ mod tests {
124125
fn hotp_rfc_values() {
125126
for value in 0..RFC_HOTP_VALUES.len() {
126127
assert_eq!(
127-
otp::hotp(&RFC_HOTP_SECRET, value as u64, 6, &digest::SHA1),
128+
otp::hotp(&RFC_HOTP_SECRET, value as u64, 6, &HmacAlgorithm::SHA1),
128129
RFC_HOTP_VALUES[value as usize]
129130
);
130131
}
@@ -155,7 +156,7 @@ mod tests {
155156
&RFC_TOTP_SECRET_SHA1,
156157
8,
157158
RFC_TOTP_TIMES[value],
158-
&digest::SHA1
159+
&HmacAlgorithm::SHA1
159160
),
160161
RFC_TOTP_VALUES_SHA1[value as usize]
161162
);
@@ -178,7 +179,7 @@ mod tests {
178179
&RFC_TOTP_SECRET_SHA256,
179180
8,
180181
RFC_TOTP_TIMES[value],
181-
&digest::SHA256
182+
&HmacAlgorithm::SHA256
182183
),
183184
RFC_TOTP_VALUES_SHA256[value as usize]
184185
);
@@ -201,10 +202,26 @@ mod tests {
201202
&RFC_TOTP_SECRET_SHA512,
202203
8,
203204
RFC_TOTP_TIMES[value],
204-
&digest::SHA512
205+
&HmacAlgorithm::SHA512
205206
),
206207
RFC_TOTP_VALUES_SHA512[value as usize]
207208
);
208209
}
209210
}
210211
}
212+
213+
/// Converts u64 to a u8 array
214+
/// Fallback method waiting for stabilization of int_to_from_bytes
215+
/// (see Rust PR #51835) in Rust 1.29
216+
fn u64_into_bytes(x: u64) -> [u8; 8] {
217+
[
218+
((x >> 56) & 0xff) as u8,
219+
((x >> 48) & 0xff) as u8,
220+
((x >> 40) & 0xff) as u8,
221+
((x >> 32) & 0xff) as u8,
222+
((x >> 24) & 0xff) as u8,
223+
((x >> 16) & 0xff) as u8,
224+
((x >> 8) & 0xff) as u8,
225+
(x & 0xff) as u8,
226+
]
227+
}

src/lib/types.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,41 @@ impl Vault {
4141

4242
pub fn remove_record(&mut self, record_id: String) -> Result<(), VaultError> {
4343
tracepoint!();
44-
let r = record_id.clone(); // We need ownership if we need to build an error
4544
match self.passwords.entry(record_id) {
4645
Entry::Occupied(entry) => {
4746
entry.remove();
4847
Ok(())
4948
}
50-
_ => Err(VaultError::UnknownRecord(r)),
49+
_ => Err(VaultError::UnknownRecord),
5150
}
5251
}
5352

5453
pub fn remove_otp_record(&mut self, record_id: String) -> Result<(), VaultError> {
5554
tracepoint!();
56-
let r = record_id.clone(); // We need ownership if we need to build an error
5755
match self.otps.entry(record_id) {
5856
Entry::Occupied(entry) => {
5957
entry.remove();
6058
Ok(())
6159
}
62-
_ => Err(VaultError::UnknownRecord(r)),
60+
_ => Err(VaultError::UnknownRecord),
6361
}
6462
}
6563

66-
pub fn get_record(&self, record_id: String) -> Result<&Record, VaultError> {
64+
pub fn get_record(&self, record_id: &str) -> Result<&Record, VaultError> {
6765
tracepoint!();
68-
if let Some(record) = self.passwords.get(&record_id) {
66+
if let Some(record) = self.passwords.get(record_id) {
6967
Ok(record)
7068
} else {
71-
Err(VaultError::UnknownRecord(record_id))
69+
Err(VaultError::UnknownRecord)
7270
}
7371
}
7472

75-
pub fn get_otp_record(&self, record_id: String) -> Result<&OtpRecord, VaultError> {
73+
pub fn get_otp_record(&self, record_id: &str) -> Result<&OtpRecord, VaultError> {
7674
tracepoint!();
77-
if let Some(record) = self.otps.get(&record_id) {
75+
if let Some(record) = self.otps.get(record_id) {
7876
Ok(record)
7977
} else {
80-
Err(VaultError::UnknownRecord(record_id))
78+
Err(VaultError::UnknownRecord)
8179
}
8280
}
8381

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(attr_literals)]
21
extern crate env_logger; // TODO env_logger may not be a good fit
32
#[macro_use]
43
extern crate structopt;

0 commit comments

Comments
 (0)