Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace byteorder with std functions #405

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ maintenance = { status = "actively-developed" }

[dependencies]
bytes = "0.4"
byteorder = "1"
futures = "0.1"
log = "0.4"
sodiumoxide = "0.2.3"
Expand Down
10 changes: 5 additions & 5 deletions src/toxcore/crypto_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ pub use sodiumoxide::crypto::box_::*;
pub use sodiumoxide::crypto::hash::{sha256, sha512};
pub use sodiumoxide::crypto::secretbox;

use byteorder::{ByteOrder, LittleEndian, NativeEndian};

use crate::toxcore::binary_io::*;

// TODO: check if `#[inline]` is actually useful
Expand Down Expand Up @@ -37,15 +35,17 @@ pub fn random_u32() -> u32 {
trace!("Generating random u32");
let mut array = [0; 4];
randombytes_into(&mut array);
NativeEndian::read_u32(&array)
// The order we use here doesn't matter, we just want random bytes.
u32::from_be_bytes(array)
}

/// Return a random number.
pub fn random_u64() -> u64 {
trace!("Generating random u64");
let mut array = [0; 8];
randombytes_into(&mut array);
NativeEndian::read_u64(&array)
// The order we use here doesn't matter, we just want random bytes.
u64::from_be_bytes(array)
}

/// Return a random number.
Expand Down Expand Up @@ -178,7 +178,7 @@ pub fn increment_nonce_number(nonce: &mut Nonce, num: u64) {
let Nonce(ref mut bytes) = *nonce;
bytes.reverse(); // treat nonce as LE number
let mut num_bytes = [0; NONCEBYTES];
LittleEndian::write_u64(&mut num_bytes, num);
num_bytes[..8].copy_from_slice(&u64::to_le_bytes(num));
::sodiumoxide::utils::add_le(bytes, &num_bytes).unwrap(); // sizes are equal
bytes.reverse(); // treat nonce as BE number again
}
Expand Down
4 changes: 2 additions & 2 deletions src/toxcore/dht/packet/crypto_data.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*! CryptoData packet
*/

use byteorder::{ByteOrder, BigEndian};
use nom::{be_u16, be_u32, rest};
use std::convert::TryInto;

use crate::toxcore::binary_io::*;
use crate::toxcore::crypto_core::*;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl ToBytes for CryptoData {
impl CryptoData {
/// Get last two bytes of `Nonce` considering it as BigEndian number.
pub fn nonce_last_bytes(nonce: Nonce) -> u16 {
BigEndian::read_u16(&nonce.as_ref()[NONCEBYTES - 2..])
u16::from_be_bytes(nonce.as_ref()[NONCEBYTES - 2..].try_into().unwrap())
}
/// Create `CryptoData` from `CryptoDataPayload` encrypting it with
/// `shared_key`.
Expand Down
7 changes: 2 additions & 5 deletions src/toxcore/state_format/old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! better will become available.*

use std::default::Default;
use byteorder::{ByteOrder, LittleEndian};
use nom::{le_u16, be_u16, le_u8, le_u32, le_u64, rest};

use crate::toxcore::binary_io::*;
Expand Down Expand Up @@ -158,8 +157,7 @@ impl ToBytes for DhtState {
)?;

let len = (idx - start_idx - 16) as u32;
LittleEndian::write_u32(&mut buf[start_idx + 8..], len);

buf[start_idx + 8..start_idx + 12].copy_from_slice(&u32::to_le_bytes(len));
Ok((buf, idx))
}
}
Expand Down Expand Up @@ -571,8 +569,7 @@ impl ToBytes for Section {
}?;

let len = (idx - start_idx - 8) as u32;
LittleEndian::write_u32(&mut buf[start_idx..], len);

buf[start_idx..start_idx + 4].copy_from_slice(&u32::to_le_bytes(len));
Ok((buf, idx))
}
}
Expand Down