Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SafePassword struct #46

Merged
merged 4 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ newtype-ops = "0.1.4"
serde = { version = "1.0.0", features = ["derive"] }
serde_json = "1.0.0"
thiserror = "1.0.0"
zeroize = "1.4.0"

[dev-dependencies]
rand = "0.7.3"
2 changes: 2 additions & 0 deletions src/hidden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ impl<T: PartialEq> PartialEq for Hidden<T> {
}
}

impl<T: Eq> Eq for Hidden<T> {}

#[cfg(test)]
mod test {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ pub mod hex;
pub mod locks;
pub mod hidden;
pub mod message_format;
pub mod password;
pub mod serde;

pub use self::{
byte_array::{ByteArray, ByteArrayError},
hash::Hashable,
hidden::Hidden,
password::SafePassword,
};
56 changes: 56 additions & 0 deletions src/password.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! A module with a safe password wrapper.

use std::{error::Error, fmt, str::FromStr};

use serde::{Deserialize, Serialize};
use zeroize::Zeroize;

use crate::Hidden;

/// A hidden string that implements [`Zeroize`].
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(transparent)]
pub struct SafePassword {
password: Hidden<Box<[u8]>>,
}

impl From<String> for SafePassword {
fn from(s: String) -> Self {
Self {
password: Hidden::from(s.into_bytes().into_boxed_slice()),
}
}
}

impl Drop for SafePassword {
fn drop(&mut self) {
self.password.zeroize();
}
}

/// An error for parsing a password from string.
#[derive(Debug)]
pub struct PasswordError;

impl Error for PasswordError {}

impl fmt::Display for PasswordError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PasswordError")
}
}

impl FromStr for SafePassword {
type Err = PasswordError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s.to_owned()))
}
}

impl SafePassword {
/// Gets a reference to bytes of a passphrase.
pub fn reveal(&self) -> &[u8] {
self.password.as_ref()
}
}
2 changes: 1 addition & 1 deletion src/serde/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod tests {
let hex_or_bytes = HexOrBytes([1, 2, 3, 255]);
let expected = "\"010203ff\"";
assert_eq!(serde_json::to_string(&hex_or_bytes).unwrap(), expected);
let restored: HexOrBytes = serde_json::from_str(&expected).unwrap();
let restored: HexOrBytes = serde_json::from_str(expected).unwrap();
assert_eq!(hex_or_bytes, restored);
}

Expand Down