Adjust arg types of Credentials::with_blob

... to avoid redundant utf-8 checking
This commit is contained in:
johannesd3 2021-05-26 20:45:22 +02:00
parent d8ec98015c
commit 14c1779056
No known key found for this signature in database
GPG key ID: 8C2739E91D410F75
2 changed files with 13 additions and 8 deletions

View file

@ -42,7 +42,11 @@ impl Credentials {
} }
} }
pub fn with_blob(username: String, encrypted_blob: &str, device_id: &str) -> Credentials { pub fn with_blob(
username: impl Into<String>,
encrypted_blob: impl AsRef<[u8]>,
device_id: impl AsRef<[u8]>,
) -> Credentials {
fn read_u8<R: Read>(stream: &mut R) -> io::Result<u8> { fn read_u8<R: Read>(stream: &mut R) -> io::Result<u8> {
let mut data = [0u8]; let mut data = [0u8];
stream.read_exact(&mut data)?; stream.read_exact(&mut data)?;
@ -67,7 +71,9 @@ impl Credentials {
Ok(data) Ok(data)
} }
let secret = Sha1::digest(device_id.as_bytes()); let username = username.into();
let secret = Sha1::digest(device_id.as_ref());
let key = { let key = {
let mut key = [0u8; 24]; let mut key = [0u8; 24];
@ -88,9 +94,9 @@ impl Credentials {
let mut data = base64::decode(encrypted_blob).unwrap(); let mut data = base64::decode(encrypted_blob).unwrap();
let cipher = Aes192::new(GenericArray::from_slice(&key)); let cipher = Aes192::new(GenericArray::from_slice(&key));
let block_size = <Aes192 as BlockCipher>::BlockSize::to_usize(); let block_size = <Aes192 as BlockCipher>::BlockSize::to_usize();
assert_eq!(data.len() % block_size, 0); assert_eq!(data.len() % block_size, 0);
// replace to chunks_exact_mut with MSRV bump to 1.31 for chunk in data.chunks_exact_mut(block_size) {
for chunk in data.chunks_mut(block_size) {
cipher.decrypt_block(GenericArray::from_mut_slice(chunk)); cipher.decrypt_block(GenericArray::from_mut_slice(chunk));
} }
@ -102,7 +108,7 @@ impl Credentials {
data data
}; };
let mut cursor = io::Cursor::new(&blob); let mut cursor = io::Cursor::new(blob.as_slice());
read_u8(&mut cursor).unwrap(); read_u8(&mut cursor).unwrap();
read_bytes(&mut cursor).unwrap(); read_bytes(&mut cursor).unwrap();
read_u8(&mut cursor).unwrap(); read_u8(&mut cursor).unwrap();

View file

@ -129,11 +129,10 @@ impl RequestHandler {
GenericArray::from_slice(iv), GenericArray::from_slice(iv),
); );
cipher.apply_keystream(&mut data); cipher.apply_keystream(&mut data);
String::from_utf8(data).unwrap() data
}; };
let credentials = let credentials = Credentials::with_blob(username, &decrypted, &self.config.device_id);
Credentials::with_blob(username.to_string(), &decrypted, &self.config.device_id);
self.tx.send(credentials).unwrap(); self.tx.send(credentials).unwrap();