Skip to content

Commit cfc59cd

Browse files
committed
fix docstring
1 parent 4a14f37 commit cfc59cd

File tree

4 files changed

+117
-71
lines changed

4 files changed

+117
-71
lines changed

Cargo.lock

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/crypto/encryption.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
use aes::cipher::{block_padding::{Pkcs7, UnpadError}, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
1+
use aes::cipher::{
2+
block_padding::{Pkcs7, UnpadError},
3+
BlockDecryptMut, BlockEncryptMut, KeyIvInit,
4+
};
25

36
pub type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
47
pub type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
58

69
/// Returns resulting ciphertext (Uses Pkcs7 padding)
7-
///
10+
///
811
/// Usage:
12+
/// ```rust
13+
/// use stacks_rs::crypto::encryption::cbc_encrypt;
14+
/// use stacks_rs::crypto::encryption::Aes128CbcEnc;
15+
/// let key = hex::decode("1fe107d14dd8b152580f3dea8591fc3b").unwrap();
16+
/// let iv = hex::decode("7b6070a896d41d227cc0cebbd92d797e").unwrap();
17+
/// let plain = hex::decode("13eb26baf2b688574cadac6dba").unwrap();
18+
/// let ciphertext = cbc_encrypt::<Aes128CbcEnc>(&key, &iv, &plain);
919
/// ```
10-
/// let ciphertext = cbc_encrypt::<Aes128CbcEnc>(&enc_key, &iv, &plaintext)
11-
/// ```
12-
pub fn cbc_encrypt<T>(enc_key: &[u8], iv: &[u8], plaintext: &[u8]) -> Vec<u8>
13-
where
20+
pub fn cbc_encrypt<T>(enc_key: &[u8], iv: &[u8], plaintext: &[u8]) -> Vec<u8>
21+
where
1422
T: KeyIvInit,
15-
T: BlockEncryptMut
23+
T: BlockEncryptMut,
1624
{
1725
<T as KeyIvInit>::new(enc_key.into(), iv.into()).encrypt_padded_vec_mut::<Pkcs7>(plaintext)
1826
}
1927

20-
2128
/// Returns resulting plaintext. (Uses Pkcs7 padding)
22-
///
29+
///
2330
/// Returns [`UnpadError`] if padding is malformed or if input length is
2431
/// not multiple of the algorithm's `BlockSize` (e.g. AES128-CBC uses blocks of `128 bits`).
25-
///
32+
///
2633
/// Usage:
27-
/// ```
28-
/// let plaintext = cbc_decrypt::<Aes128CbcEnc>(&enc_key, &iv, &ciphertext)
34+
/// ```rust
35+
/// use stacks_rs::crypto::encryption::Aes128CbcDec;
36+
/// use stacks_rs::crypto::encryption::cbc_decrypt;
37+
/// let key = hex::decode("1fe107d14dd8b152580f3dea8591fc3b").unwrap();
38+
/// let iv = hex::decode("7b6070a896d41d227cc0cebbd92d797e").unwrap();
39+
/// let cipher = hex::decode("a4bfd6586344bcdef94f09d871ca8a16").unwrap();
40+
/// let plaintext = cbc_decrypt::<Aes128CbcDec>(&key, &iv, &cipher);
2941
/// ```
3042
pub fn cbc_decrypt<T>(enc_key: &[u8], iv: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, UnpadError>
31-
where
43+
where
3244
T: KeyIvInit,
33-
T: BlockDecryptMut
45+
T: BlockDecryptMut,
3446
{
3547
<T as KeyIvInit>::new(enc_key.into(), iv.into()).decrypt_padded_vec_mut::<Pkcs7>(ciphertext)
3648
}
@@ -45,9 +57,9 @@ mod tests {
4557
* Test vector: https://github.com/geertj/bluepass/blob/master/tests/vectors/aes-cbc-pkcs7.txt
4658
*/
4759
let key = hex::decode("1fe107d14dd8b152580f3dea8591fc3b").unwrap();
48-
let iv=hex::decode("7b6070a896d41d227cc0cebbd92d797e").unwrap();
60+
let iv = hex::decode("7b6070a896d41d227cc0cebbd92d797e").unwrap();
4961
let plain = hex::decode("13eb26baf2b688574cadac6dba").unwrap();
50-
let cipher= hex::decode("a4bfd6586344bcdef94f09d871ca8a16").unwrap();
62+
let cipher = hex::decode("a4bfd6586344bcdef94f09d871ca8a16").unwrap();
5163

5264
let encrypted = cbc_encrypt::<Aes128CbcEnc>(&key, &iv, &plain);
5365

@@ -60,12 +72,12 @@ mod tests {
6072
* Test vector: https://github.com/geertj/bluepass/blob/master/tests/vectors/aes-cbc-pkcs7.txt
6173
*/
6274
let key = hex::decode("1fe107d14dd8b152580f3dea8591fc3b").unwrap();
63-
let iv=hex::decode("7b6070a896d41d227cc0cebbd92d797e").unwrap();
75+
let iv = hex::decode("7b6070a896d41d227cc0cebbd92d797e").unwrap();
6476
let plain = hex::decode("13eb26baf2b688574cadac6dba").unwrap();
65-
let cipher= hex::decode("a4bfd6586344bcdef94f09d871ca8a16").unwrap();
77+
let cipher = hex::decode("a4bfd6586344bcdef94f09d871ca8a16").unwrap();
6678

6779
let decrypted = cbc_decrypt::<Aes128CbcDec>(&key, &iv, &cipher).unwrap();
6880

6981
assert_eq!(decrypted, plain);
7082
}
71-
}
83+
}

src/crypto/hash.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ pub type U64 = aes::cipher::consts::U64;
77
///
88
/// Usage:
99
/// ### Sha256 hash
10-
/// ```
11-
/// let digest = compute_hash::<Sha256, U32>("aaaaaa".to_bytes());
12-
/// -
13-
/// let digest = compute_hash::<Sha256, _>("aaaaaa".to_bytes());
10+
/// ```rust
11+
///
12+
/// use sha2::Sha256;
13+
/// use aes::cipher::consts::U32;
14+
/// use stacks_rs::crypto::hash::compute_hash;
15+
/// let digest = compute_hash::<Sha256, U32>("aaaaaa".as_bytes());
16+
/// let digest = compute_hash::<Sha256, _>("aaaaaa".as_bytes());
1417
/// ```
1518
/// ### Sha512 hash
19+
/// ```rust
20+
/// use sha2::Sha512;
21+
/// use aes::cipher::consts::U64;
22+
/// use stacks_rs::crypto::hash::compute_hash;
23+
/// let digest = compute_hash::<Sha512, U64>("aaaaaa".as_bytes());
24+
/// let digest = compute_hash::<Sha512, _>("aaaaaa".as_bytes());
1625
/// ```
17-
/// let digest = compute_hash::<Sha512, U64>("aaaaaa".to_bytes());
18-
/// -
19-
/// let digest = compute_hash::<Sha512, _>("aaaaaa".to_bytes());
2026
pub fn compute_hash<D, L1>(input_data: &[u8]) -> Vec<u8>
2127
where
2228
D: Digest<OutputSize = L1>,
@@ -38,14 +44,16 @@ mod tests {
3844
fn test_sha256() {
3945
let digest = compute_hash::<Sha256, U32>("aaaaaaaaaaaaaaaa".as_bytes());
4046
let hex_digest = hex::encode(digest);
41-
assert_eq!(hex_digest, "0c0beacef8877bbf2416eb00f2b5dc96354e26dd1df5517320459b1236860f8c")
47+
assert_eq!(
48+
hex_digest,
49+
"0c0beacef8877bbf2416eb00f2b5dc96354e26dd1df5517320459b1236860f8c"
50+
)
4251
}
4352

4453
#[test]
4554
fn test_sha512() {
4655
let digest = compute_hash::<Sha512, U64>("aaaaaaaaaaaaaaaa".as_bytes());
4756
let hex_digest = hex::encode(digest);
48-
assert_eq!(hex_digest,
49-
"987d0fc93db6a73fdb16493690fb42455c7c6fbafe9a276965424b12afad3512fb808d902faa8a019d639dc5ad07c235805e08f396147cf435913cfed501f65a")
57+
assert_eq!(hex_digest, "987d0fc93db6a73fdb16493690fb42455c7c6fbafe9a276965424b12afad3512fb808d902faa8a019d639dc5ad07c235805e08f396147cf435913cfed501f65a")
5058
}
51-
}
59+
}

src/crypto/hmac.rs

+47-21
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
use std::fmt;
22

3-
use aes::cipher::{consts::U256, typenum::{IsLess, Le, NonZero}, BlockSizeUser, InvalidLength, KeyInit};
4-
use hmac::{digest::{block_buffer::Eager, core_api::{BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore}, HashMarker}, Hmac, Mac};
5-
use sha2::{Sha256, Sha512};
63
use super::utils;
4+
use aes::cipher::{
5+
consts::U256,
6+
typenum::{IsLess, Le, NonZero},
7+
BlockSizeUser, InvalidLength, KeyInit,
8+
};
9+
use hmac::{
10+
digest::{
11+
block_buffer::Eager,
12+
core_api::{BufferKindUser, CoreProxy, FixedOutputCore, UpdateCore},
13+
HashMarker,
14+
},
15+
Hmac, Mac,
16+
};
17+
use sha2::{Sha256, Sha512};
718

819
pub type HmacSha256 = Hmac<Sha256>;
920
pub type HmacSha512 = Hmac<Sha512>;
1021

1122
#[derive(Clone, Copy, Debug)]
1223
pub enum HmacError {
13-
InvalidKeyLength(InvalidLength)
24+
InvalidKeyLength(InvalidLength),
1425
}
1526

1627
impl fmt::Display for HmacError {
@@ -20,26 +31,31 @@ impl fmt::Display for HmacError {
2031
}
2132
impl std::error::Error for HmacError {}
2233

23-
2434
/// Computes the hmac using the provided Hash algorithm.
25-
///
35+
///
2636
/// Usage:
2737
/// ```
38+
/// use stacks_rs::crypto::hmac::HmacSha256;
39+
/// use stacks_rs::crypto::hmac::HmacSha512;
40+
/// use stacks_rs::crypto::hmac::compute_hmac;
2841
/// let digest_sha256 = compute_hmac::<HmacSha256>("what do ya want for nothing?".as_bytes(), "Jefe".as_bytes()).unwrap();
2942
/// let digest_sha512 = compute_hmac::<HmacSha512>("what do ya want for nothing?".as_bytes(), "Jefe".as_bytes()).unwrap();
3043
/// ```
31-
pub fn compute_hmac<D>(payload: &[u8], mac_key: &[u8]) -> Result<Vec<u8>, HmacError>
44+
pub fn compute_hmac<D>(payload: &[u8], mac_key: &[u8]) -> Result<Vec<u8>, HmacError>
3245
where
3346
D: KeyInit + Mac,
3447
{
35-
let mut hmac_digest = <D as KeyInit>::new_from_slice(mac_key).map_err(|err|{
36-
HmacError::InvalidKeyLength(err)
37-
})?;
48+
let mut hmac_digest =
49+
<D as KeyInit>::new_from_slice(mac_key).map_err(|err| HmacError::InvalidKeyLength(err))?;
3850
hmac_digest.update(payload);
3951
Ok(hmac_digest.finalize().into_bytes().to_vec())
4052
}
4153

42-
pub fn get_pbkdf2_hmac_keys<D>(password: &[u8], salt: Option<[u8; 16]>, rounds: u32) -> (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)
54+
pub fn get_pbkdf2_hmac_keys<D>(
55+
password: &[u8],
56+
salt: Option<[u8; 16]>,
57+
rounds: u32,
58+
) -> (Vec<u8>, Vec<u8>, Vec<u8>, Vec<u8>)
4359
where
4460
D: CoreProxy,
4561
D::Core: Sync
@@ -61,7 +77,12 @@ where
6177
};
6278
let mut keys_and_iv = [0u8; 48];
6379
pbkdf2::pbkdf2_hmac::<D>(password, &salt, rounds, &mut keys_and_iv);
64-
(keys_and_iv[0..16].to_vec(), keys_and_iv[16..32].to_vec(), keys_and_iv[32..48].to_vec(), salt)
80+
(
81+
keys_and_iv[0..16].to_vec(),
82+
keys_and_iv[16..32].to_vec(),
83+
keys_and_iv[32..48].to_vec(),
84+
salt,
85+
)
6586
}
6687

6788
#[cfg(test)]
@@ -74,17 +95,22 @@ mod tests {
7495
* Test vector: https://datatracker.ietf.org/doc/html/rfc4231#section-4
7596
*/
7697
let digest = compute_hmac::<HmacSha256>(
77-
"what do ya want for nothing?".as_bytes(), "Jefe".as_bytes()
78-
).unwrap();
98+
"what do ya want for nothing?".as_bytes(),
99+
"Jefe".as_bytes(),
100+
)
101+
.unwrap();
79102
let hex_digest = hex::encode(digest);
80-
assert_eq!(hex_digest, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
103+
assert_eq!(
104+
hex_digest,
105+
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
106+
);
81107

82108
let digest = compute_hmac::<HmacSha512>(
83-
"what do ya want for nothing?".as_bytes(),
84-
"Jefe".as_bytes()
85-
).unwrap();
109+
"what do ya want for nothing?".as_bytes(),
110+
"Jefe".as_bytes(),
111+
)
112+
.unwrap();
86113
let hex_digest = hex::encode(digest);
87-
assert_eq!(hex_digest,
88-
"164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737");
114+
assert_eq!(hex_digest, "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737");
89115
}
90-
}
116+
}

0 commit comments

Comments
 (0)