Skip to content

Commit c0115fd

Browse files
authored
Merge pull request #763 from Johannesd3/credentials-with-blob-args
Adjust argument types of `Credentials::with_blob` to avoid redundant UTF-8 checks
2 parents 5f99bba + 14c1779 commit c0115fd

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

core/src/authentication.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ impl Credentials {
4242
}
4343
}
4444

45-
pub fn with_blob(username: String, encrypted_blob: &str, device_id: &str) -> Credentials {
45+
pub fn with_blob(
46+
username: impl Into<String>,
47+
encrypted_blob: impl AsRef<[u8]>,
48+
device_id: impl AsRef<[u8]>,
49+
) -> Credentials {
4650
fn read_u8<R: Read>(stream: &mut R) -> io::Result<u8> {
4751
let mut data = [0u8];
4852
stream.read_exact(&mut data)?;
@@ -67,7 +71,9 @@ impl Credentials {
6771
Ok(data)
6872
}
6973

70-
let secret = Sha1::digest(device_id.as_bytes());
74+
let username = username.into();
75+
76+
let secret = Sha1::digest(device_id.as_ref());
7177

7278
let key = {
7379
let mut key = [0u8; 24];
@@ -88,9 +94,9 @@ impl Credentials {
8894
let mut data = base64::decode(encrypted_blob).unwrap();
8995
let cipher = Aes192::new(GenericArray::from_slice(&key));
9096
let block_size = <Aes192 as BlockCipher>::BlockSize::to_usize();
97+
9198
assert_eq!(data.len() % block_size, 0);
92-
// replace to chunks_exact_mut with MSRV bump to 1.31
93-
for chunk in data.chunks_mut(block_size) {
99+
for chunk in data.chunks_exact_mut(block_size) {
94100
cipher.decrypt_block(GenericArray::from_mut_slice(chunk));
95101
}
96102

@@ -102,7 +108,7 @@ impl Credentials {
102108
data
103109
};
104110

105-
let mut cursor = io::Cursor::new(&blob);
111+
let mut cursor = io::Cursor::new(blob.as_slice());
106112
read_u8(&mut cursor).unwrap();
107113
read_bytes(&mut cursor).unwrap();
108114
read_u8(&mut cursor).unwrap();

discovery/src/server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,10 @@ impl RequestHandler {
129129
GenericArray::from_slice(iv),
130130
);
131131
cipher.apply_keystream(&mut data);
132-
String::from_utf8(data).unwrap()
132+
data
133133
};
134134

135-
let credentials =
136-
Credentials::with_blob(username.to_string(), &decrypted, &self.config.device_id);
135+
let credentials = Credentials::with_blob(username, &decrypted, &self.config.device_id);
137136

138137
self.tx.send(credentials).unwrap();
139138

0 commit comments

Comments
 (0)