Skip to content

Commit 218daf6

Browse files
reformat code to rust coding style
Signed-off-by: Nikola Pajkovsky <nikolap@openssl.org>
1 parent 33c8a30 commit 218daf6

62 files changed

Lines changed: 2953 additions & 1379 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cli/src/encoders_cmd.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use std::io;
22
use std::io::{Read, Write};
33

4-
use bouncycastle::hex;
54
use bouncycastle::base64;
5+
use bouncycastle::hex;
66

77
pub(crate) fn hex_encode_cmd() {
88
// Stream from stdin to stdout in chunks of 1 kb
99
let mut buf: [u8; 1024] = [0u8; 1024];
1010
let mut bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
1111
while bytes_read != 0 {
12-
io::stdout().write_all(
13-
hex::encode(&buf[..bytes_read]).as_bytes()
14-
).expect("Failed to write to stdout");
12+
io::stdout()
13+
.write_all(hex::encode(&buf[..bytes_read]).as_bytes())
14+
.expect("Failed to write to stdout");
1515

1616
bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
1717
}
@@ -22,13 +22,12 @@ pub(crate) fn hex_decode_cmd() {
2222
let mut buf: [u8; 1024] = [0u8; 1024];
2323
let mut bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
2424
while bytes_read != 0 {
25-
let chunk_str: String = String::from_utf8(
26-
Vec::from(&buf[..bytes_read])
27-
).expect("Input was not valid utf8.");
25+
let chunk_str: String =
26+
String::from_utf8(Vec::from(&buf[..bytes_read])).expect("Input was not valid utf8.");
2827

29-
io::stdout().write_all(
30-
&*hex::decode(chunk_str.as_str()).expect("Input was not valid hex.")
31-
).expect("Failed to write to stdout");
28+
io::stdout()
29+
.write_all(&*hex::decode(chunk_str.as_str()).expect("Input was not valid hex."))
30+
.expect("Failed to write to stdout");
3231

3332
bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
3433
}
@@ -40,9 +39,9 @@ pub(crate) fn base64_encode_cmd() {
4039
let mut buf: [u8; 1024] = [0u8; 1024];
4140
let mut bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
4241
while bytes_read != 0 {
43-
io::stdout().write_all(
44-
encoder.do_update(&buf[..bytes_read]).as_bytes()
45-
).expect("Failed to write to stdout");
42+
io::stdout()
43+
.write_all(encoder.do_update(&buf[..bytes_read]).as_bytes())
44+
.expect("Failed to write to stdout");
4645

4746
bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
4847
}
@@ -54,14 +53,18 @@ pub(crate) fn base64_decode_cmd() {
5453
let mut decoder = base64::Base64Decoder::new(true);
5554
let mut bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
5655
while bytes_read != 0 {
57-
let chunk_str: String = String::from_utf8(
58-
Vec::from(&buf[..bytes_read])
59-
).expect("Input was not valid utf8.");
56+
let chunk_str: String =
57+
String::from_utf8(Vec::from(&buf[..bytes_read])).expect("Input was not valid utf8.");
6058

61-
io::stdout().write_all(
62-
decoder.do_update(chunk_str.as_str()).expect("Input was not valid base64.").as_slice()
63-
).expect("Failed to write to stdout");
59+
io::stdout()
60+
.write_all(
61+
decoder
62+
.do_update(chunk_str.as_str())
63+
.expect("Input was not valid base64.")
64+
.as_slice(),
65+
)
66+
.expect("Failed to write to stdout");
6467

6568
bytes_read = io::stdin().read(&mut buf).expect("Failed to read from stdin");
6669
}
67-
}
70+
}

cli/src/helpers.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
use std::{io};
1+
use bouncycastle::core::key_material::{KeyMaterial, KeyMaterialTrait, KeyType};
2+
use bouncycastle::core::traits::SecurityStrength;
3+
use bouncycastle::hex;
24
use std::fs::File;
5+
use std::io;
36
use std::io::{Read, Write};
47
use std::process::exit;
5-
use bouncycastle::core::key_material::{KeyMaterial, KeyMaterialTrait, KeyType};
6-
use bouncycastle::core::traits::{SecurityStrength};
7-
use bouncycastle::hex;
88

99
/// Reads either bin or hex
1010
pub(crate) fn read_from_file(filename: &str) -> Vec<u8> {
1111
let file = File::open(&filename);
1212
if file.is_ok() {
1313
let mut buf = Vec::<u8>::new();
1414
match file.unwrap().read_to_end(&mut buf) {
15-
Ok(_bytes_read) => {
15+
Ok(_bytes_read) => {
1616
// try hex decoding it
1717
match hex::decode(&buf) {
18-
Ok(decoded) => { decoded },
18+
Ok(decoded) => decoded,
1919
Err(_) => {
2020
// well, it's not hex, so return it raw
2121
buf
22-
},
22+
}
2323
}
24-
},
24+
}
2525
Err(_) => {
2626
eprintln!("Error: couldn't open file '{}'", &filename);
2727
exit(-1);
28-
},
28+
}
2929
}
3030
} else {
3131
eprintln!("Error: couldn't open file '{}'", &filename);
@@ -35,22 +35,21 @@ pub(crate) fn read_from_file(filename: &str) -> Vec<u8> {
3535

3636
/// Reads either bin or hex
3737
pub(crate) fn read_from_file_or_stdin(filename: &Option<String>) -> Vec<u8> {
38-
3938
if filename.is_some() {
4039
// This already reads either bin or hex
4140
return read_from_file(filename.as_ref().unwrap());
4241
}
43-
42+
4443
let mut buf = Vec::<u8>::new();
4544
io::stdin().read_to_end(&mut buf).expect("Failed to read from stdin");
4645

4746
// try hex decoding it
4847
match hex::decode(&buf) {
49-
Ok(decoded) => { decoded },
48+
Ok(decoded) => decoded,
5049
Err(_) => {
5150
// well, it's not hex, so return it raw
5251
buf
53-
},
52+
}
5453
}
5554
}
5655

@@ -84,15 +83,15 @@ pub(crate) fn parse_seed<const SEED_LEN: usize>(bytes: &[u8]) -> Result<KeyMater
8483
// try decoding it as hex first
8584
let seed_bytes: [u8; SEED_LEN] = match &hex::decode(&bytes) {
8685
Ok(decoded_bytes) => {
87-
if decoded_bytes.len() < SEED_LEN || decoded_bytes.len() > SEED_LEN +1 {
86+
if decoded_bytes.len() < SEED_LEN || decoded_bytes.len() > SEED_LEN + 1 {
8887
// it was valid hex, but the wrong length
8988
return Err(());
9089
}
9190
decoded_bytes[..SEED_LEN].try_into().unwrap()
9291
}
9392
Err(_) => {
9493
// it's not hex, so take the fist SEED_LEN bytes of the raw binary
95-
if bytes.len() < SEED_LEN || bytes.len() > SEED_LEN +1 {
94+
if bytes.len() < SEED_LEN || bytes.len() > SEED_LEN + 1 {
9695
return Err(());
9796
}
9897
bytes[..SEED_LEN].try_into().unwrap()
@@ -113,4 +112,4 @@ pub(crate) fn parse_seed<const SEED_LEN: usize>(bytes: &[u8]) -> Result<KeyMater
113112
seed.drop_hazardous_operations();
114113
}
115114
Ok(seed)
116-
}
115+
}

cli/src/hkdf_cmd.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
use std::{fs, io};
21
use std::io::Write;
32
use std::process::exit;
3+
use std::{fs, io};
44

55
use bouncycastle::core::key_material::{KeyMaterial, KeyMaterialTrait, KeyType};
66
use bouncycastle::hex;
77
use bouncycastle::hkdf;
88

9-
pub(crate) fn hkdf_cmd(hkdfname: &str,
10-
salt: &Option<String>,
11-
salt_file: &Option<String>,
12-
ikm: &Option<String>,
13-
ikm_file: &Option<String>,
14-
additional_input: &Option<String>,
15-
additional_input_file: &Option<String>,
16-
len: usize,
17-
output_hex: bool ) {
9+
pub(crate) fn hkdf_cmd(
10+
hkdfname: &str,
11+
salt: &Option<String>,
12+
salt_file: &Option<String>,
13+
ikm: &Option<String>,
14+
ikm_file: &Option<String>,
15+
additional_input: &Option<String>,
16+
additional_input_file: &Option<String>,
17+
len: usize,
18+
output_hex: bool,
19+
) {
1820
let salt_bytes: Vec<u8>;
1921
let ikm_bytes: Vec<u8>;
2022
let additional_input_bytes: Vec<u8>;
@@ -53,7 +55,6 @@ pub(crate) fn hkdf_cmd(hkdfname: &str,
5355
exit(-1)
5456
};
5557

56-
5758
additional_input_bytes = if additional_input.is_some() {
5859
hex::decode(additional_input.as_ref().unwrap()).unwrap()
5960
} else if additional_input.is_some() {
@@ -72,21 +73,24 @@ pub(crate) fn hkdf_cmd(hkdfname: &str,
7273
h.do_extract_update_bytes(ikm_bytes.as_slice()).unwrap();
7374
h.do_extract_update_bytes(additional_input_bytes.as_slice()).unwrap();
7475
h.do_extract_final_out(&mut out_key).unwrap();
75-
},
76+
}
7677
"HKDF-SHA512" => {
7778
let mut h = hkdf::HKDF_SHA512::new();
7879
h.do_extract_init(&salt_key).unwrap();
7980
h.do_extract_update_bytes(ikm_bytes.as_slice()).unwrap();
8081
h.do_extract_update_bytes(additional_input_bytes.as_slice()).unwrap();
8182
h.do_extract_final_out(&mut out_key).unwrap();
82-
},
83-
_ => { panic!("{} is not a supported HKDF variant.", hkdfname); }
83+
}
84+
_ => {
85+
panic!("{} is not a supported HKDF variant.", hkdfname);
86+
}
8487
}
8588

86-
8789
if output_hex {
8890
for b in out_key.ref_to_bytes().iter() {
8991
print!("{b:02x}");
9092
}
91-
} else { io::stdout().write(&out_key.ref_to_bytes()).unwrap(); }
92-
}
93+
} else {
94+
io::stdout().write(&out_key.ref_to_bytes()).unwrap();
95+
}
96+
}

cli/src/mac_cmd.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@ pub(crate) fn mac_cmd(
3737
key.allow_hazardous_operations();
3838
key.convert_key_type(KeyType::MACKey).unwrap();
3939

40-
4140
// instantiate the MAC object and call do_mac()
4241
match hmac_variant {
4342
HMACVariant::SHA256 => {
4443
let mac = HMAC_SHA256::new_allow_weak_key(&key).unwrap();
4544
do_mac(mac, verify_val, output_hex);
46-
},
45+
}
4746
HMACVariant::SHA512 => {
4847
let mac = HMAC_SHA512::new_allow_weak_key(&key).unwrap();
4948
do_mac(mac, verify_val, output_hex);
5049
}
5150
}
5251
}
5352

54-
5553
fn do_mac(mut mac: impl MAC, verify_val: &Option<String>, output_hex: bool) {
5654
// read the content to be MAC'd from stdin
5755
let mut buf: [u8; 1024] = [0u8; 1024];

0 commit comments

Comments
 (0)