pub struct Salt(/* private fields */);
Expand description
Random salt used to decorrelate other information.
A Salt
is a cryptographic primitive consisting of random data that is used
to modify the output of a cryptographic function. Salts are primarily used
in password hashing to defend against dictionary attacks, rainbow table
attacks, and pre-computation attacks. They are also used in other
cryptographic contexts to ensure uniqueness and prevent correlation between
different parts of a cryptosystem.
Unlike a Nonce
which has a fixed size, a Salt
in this
implementation can have a variable length (minimum 8 bytes). Different salt
creation methods are provided to generate salts of appropriate sizes for
different use cases.
§Minimum Size Requirement
For security reasons, salts must be at least 8 bytes long. Attempting to create a salt with fewer than 8 bytes will result in an error.
§CBOR Serialization
Salt
implements the CBORTaggedCodable
trait, which means it can be
serialized to and deserialized from CBOR with a specific tag. The tag used
is TAG_SALT
defined in the tags
module.
§UR Serialization
When serialized as a Uniform Resource (UR), a Salt
is represented as a
binary blob with the type “salt”.
§Common Uses
- Password hashing and key derivation functions
- Preventing correlation in cryptographic protocols
- Randomizing data before encryption to prevent pattern recognition
- Adding entropy to improve security in various cryptographic functions
§Examples
Creating a salt with a specific length:
use bc_components::Salt;
// Generate a salt with 16 bytes
let salt = Salt::new_with_len(16).unwrap();
assert_eq!(salt.len(), 16);
Creating a salt with a length proportional to data size:
use bc_components::Salt;
// Generate a salt proportional to 100 bytes of data
let salt = Salt::new_for_size(100);
// Salts for larger data will be larger (but still efficient)
let big_salt = Salt::new_for_size(1000);
assert!(big_salt.len() > salt.len());
Creating a salt with a length in a specific range:
use bc_components::Salt;
// Generate a salt with length between 16 and 32 bytes
let salt = Salt::new_in_range(16..=32).unwrap();
assert!(salt.len() >= 16 && salt.len() <= 32);
Implementations§
Source§impl Salt
impl Salt
Sourcepub fn new_with_len(count: usize) -> Result<Self>
pub fn new_with_len(count: usize) -> Result<Self>
Create a specific number of bytes of salt.
If the number of bytes is less than 8, this will return None
.
Sourcepub fn new_with_len_using(
count: usize,
rng: &mut impl RandomNumberGenerator,
) -> Result<Self>
pub fn new_with_len_using( count: usize, rng: &mut impl RandomNumberGenerator, ) -> Result<Self>
Create a specific number of bytes of salt.
If the number of bytes is less than 8, this will return None
.
Sourcepub fn new_in_range(range: RangeInclusive<usize>) -> Result<Self>
pub fn new_in_range(range: RangeInclusive<usize>) -> Result<Self>
Create a number of bytes of salt chosen randomly from the given range.
If the minimum number of bytes is less than 8, this will return None
.
Sourcepub fn new_in_range_using(
range: &RangeInclusive<usize>,
rng: &mut impl RandomNumberGenerator,
) -> Result<Self>
pub fn new_in_range_using( range: &RangeInclusive<usize>, rng: &mut impl RandomNumberGenerator, ) -> Result<Self>
Create a number of bytes of salt chosen randomly from the given range.
If the minimum number of bytes is less than 8, this will return None
.
Sourcepub fn new_for_size(size: usize) -> Self
pub fn new_for_size(size: usize) -> Self
Create a number of bytes of salt generally proportionate to the size of the object being salted.
Sourcepub fn new_for_size_using(
size: usize,
rng: &mut impl RandomNumberGenerator,
) -> Self
pub fn new_for_size_using( size: usize, rng: &mut impl RandomNumberGenerator, ) -> Self
Create a number of bytes of salt generally proportionate to the size of the object being salted.
Trait Implementations§
Source§impl AsRef<Salt> for Salt
Provides a self-reference, enabling API consistency with other types.
impl AsRef<Salt> for Salt
Provides a self-reference, enabling API consistency with other types.
Source§impl CBORTagged for Salt
Identifies the CBOR tags used for Salt serialization.
impl CBORTagged for Salt
Identifies the CBOR tags used for Salt serialization.
Source§impl CBORTaggedDecodable for Salt
Defines how a Salt is decoded from CBOR.
impl CBORTaggedDecodable for Salt
Defines how a Salt is decoded from CBOR.
Source§fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for Salt
Defines how a Salt is encoded as CBOR (as a byte string).
impl CBORTaggedEncodable for Salt
Defines how a Salt is encoded as CBOR (as a byte string).
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl<'a> From<&'a Salt> for &'a [u8]
Allows accessing the underlying data as a byte slice reference.
impl<'a> From<&'a Salt> for &'a [u8]
Allows accessing the underlying data as a byte slice reference.
Source§impl From<&Salt> for Vec<u8>
Converts a Salt reference into a Vec<u8>
containing the salt bytes.
impl From<&Salt> for Vec<u8>
Converts a Salt reference into a Vec<u8>
containing the salt bytes.
impl Eq for Salt
impl StructuralPartialEq for Salt
Auto Trait Implementations§
impl Freeze for Salt
impl RefUnwindSafe for Salt
impl Send for Salt
impl Sync for Salt
impl Unpin for Salt
impl UnwindSafe for Salt
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Lower case
letters are used (e.g. f9b4ca
)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Upper case
letters are used (e.g. F9B4CA
)