From 4d05d57a7e593f0eaf7b58a7911c0148bc8400e4 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Mon, 2 Aug 2021 14:03:14 -0400 Subject: [PATCH] RUST-942 Properly generate 5 random bytes instead of 3 for ObjectIds --- src/oid.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/oid.rs b/src/oid.rs index 3e513860..4e824e3f 100644 --- a/src/oid.rs +++ b/src/oid.rs @@ -148,8 +148,8 @@ impl ObjectId { hex::encode(self.id) } - // Generates a new timestamp representing the current seconds since epoch. - // Represented in Big Endian. + /// Generates a new timestamp representing the current seconds since epoch. + /// Represented in Big Endian. fn gen_timestamp() -> [u8; 4] { let timestamp: u32 = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) @@ -160,22 +160,17 @@ impl ObjectId { timestamp.to_be_bytes() } - // Generate a random 5-byte array. + /// Generate a random 5-byte array. fn gen_process_id() -> [u8; 5] { lazy_static! { - static ref BUF: [u8; 5] = { - let rng = thread_rng().gen_range(0, MAX_U24) as u32; - let mut buf: [u8; 5] = [0; 5]; - buf[0..4].copy_from_slice(&rng.to_be_bytes()); - buf - }; + static ref BUF: [u8; 5] = thread_rng().gen(); } *BUF } - // Gets an incremental 3-byte count. - // Represented in Big Endian. + /// Gets an incremental 3-byte count. + /// Represented in Big Endian. fn gen_count() -> [u8; 3] { let u_counter = OID_COUNTER.fetch_add(1, Ordering::SeqCst);