diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2020-04-03 17:34:28 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2020-05-11 15:53:38 +0200 |
commit | bad0aa4e36e6c504627bde16ae95daeb557d301b (patch) | |
tree | df4dfe796e821d845fb823a62648c957633975a1 /openpgp/src/utils.rs | |
parent | 8e4932b7084ed0ed10fe250b5bdc69bc9001880c (diff) |
openpgp: Use {to,from}_be_bytes.
- Replace bitshifts with the conversion functions from the standard
library.
Diffstat (limited to 'openpgp/src/utils.rs')
-rw-r--r-- | openpgp/src/utils.rs | 22 |
1 files changed, 4 insertions, 18 deletions
diff --git a/openpgp/src/utils.rs b/openpgp/src/utils.rs index 5abc968b..d77dfb67 100644 --- a/openpgp/src/utils.rs +++ b/openpgp/src/utils.rs @@ -1,27 +1,13 @@ //! Utility functions that don't fit anywhere else. +use std::convert::TryFrom; pub fn read_be_u64(b: &[u8]) -> u64 { - assert_eq!(b.len(), 8); - ((b[0] as u64) << 56) as u64 - | ((b[1] as u64) << 48) - | ((b[2] as u64) << 40) - | ((b[3] as u64) << 32) - | ((b[4] as u64) << 24) - | ((b[5] as u64) << 16) - | ((b[6] as u64) << 8) - | ((b[7] as u64) << 0) + let array = <[u8; 8]>::try_from(b).unwrap(); + u64::from_be_bytes(array) } pub fn write_be_u64(b: &mut [u8], n: u64) { - assert_eq!(b.len(), 8); - b[0] = (n >> 56) as u8; - b[1] = (n >> 48) as u8; - b[2] = (n >> 40) as u8; - b[3] = (n >> 32) as u8; - b[4] = (n >> 24) as u8; - b[5] = (n >> 16) as u8; - b[6] = (n >> 8) as u8; - b[7] = (n >> 0) as u8; + b.copy_from_slice(&n.to_be_bytes()); } #[cfg(test)] |