-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace SensitiveString with the new implementation
- Loading branch information
Showing
26 changed files
with
225 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use zeroize::Zeroize; | ||
|
||
use crate::{CryptoError, Sensitive, SensitiveString, SensitiveVec}; | ||
|
||
impl SensitiveString { | ||
pub fn decode_base64<T: base64::Engine>(self, engine: T) -> Result<SensitiveVec, CryptoError> { | ||
// Prevent accidental copies by allocating the full size | ||
let len = base64::decoded_len_estimate(self.len()); | ||
let mut value = SensitiveVec::new(Box::new(Vec::with_capacity(len))); | ||
|
||
engine | ||
.decode_vec(self.as_str(), &mut value.value) | ||
.map_err(|_| CryptoError::InvalidKey)?; | ||
|
||
Ok(value) | ||
} | ||
} | ||
|
||
impl<T: Zeroize + AsRef<[u8]>> Sensitive<T> { | ||
pub fn encode_base64<E: base64::Engine>(self, engine: E) -> SensitiveString { | ||
use base64::engine::Config; | ||
|
||
let inner: &[u8] = self.value.as_ref().as_ref(); | ||
|
||
// Prevent accidental copies by allocating the full size | ||
let padding = engine.config().encode_padding(); | ||
let len = base64::encoded_len(inner.len(), padding).expect("Valid length"); | ||
|
||
let mut value = SensitiveVec::new(Box::new(vec![0u8; len])); | ||
engine | ||
.encode_slice(inner, &mut value.value[..len]) | ||
.expect("Valid base64 string length"); | ||
|
||
value.try_into().expect("Valid base64 string") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#[allow(clippy::module_inception)] | ||
mod sensitive; | ||
pub use sensitive::{Sensitive, SensitiveString, SensitiveVec}; | ||
pub use sensitive::{Sensitive, SensitiveVec}; | ||
mod decrypted; | ||
pub use decrypted::{Decrypted, DecryptedString, DecryptedVec}; | ||
mod string; | ||
pub use string::BitString; | ||
pub use string::SensitiveString; | ||
mod base64; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.