-
Notifications
You must be signed in to change notification settings - Fork 31
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
Expand ID range to harden brute forcing #71
Closed
+136
−52
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
use crate::db::write::Entry; | ||
use crate::errors::Error; | ||
use core::str; | ||
use std::fmt; | ||
use std::str::FromStr; | ||
|
||
const CHAR_TABLE: &[char; 64] = &[ | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', | ||
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', | ||
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', | ||
'5', '6', '7', '8', '9', '-', '+', | ||
]; | ||
const CHAR_TABLE: &[u8; 64] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+"; | ||
|
||
/// Represents a 32-bit integer either numerically or mapped to a 6 character string. | ||
pub type Inner = i64; | ||
const ID_LENGTH: usize = 11; | ||
|
||
/// Represents a 64-bit integer either numerically or mapped to a 11 character string. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
pub struct Id { | ||
n: u32, | ||
n: Inner, | ||
} | ||
|
||
impl Id { | ||
/// Return the value itself. | ||
pub fn as_u32(self) -> u32 { | ||
pub fn as_inner(self) -> Inner { | ||
self.n | ||
} | ||
|
||
|
@@ -33,41 +32,74 @@ impl Id { | |
|
||
impl fmt::Display for Id { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut s = String::with_capacity(6); | ||
|
||
s.push(CHAR_TABLE[((self.n >> 26) & 0x3f) as usize]); | ||
s.push(CHAR_TABLE[((self.n >> 20) & 0x3f) as usize]); | ||
s.push(CHAR_TABLE[((self.n >> 14) & 0x3f) as usize]); | ||
s.push(CHAR_TABLE[((self.n >> 8) & 0x3f) as usize]); | ||
s.push(CHAR_TABLE[((self.n >> 2) & 0x3f) as usize]); | ||
s.push(CHAR_TABLE[(self.n & 0x3) as usize]); | ||
|
||
write!(f, "{s}") | ||
#[allow(clippy::cast_sign_loss)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 |
||
let buf: [u8; ID_LENGTH] = [ | ||
CHAR_TABLE[((self.n >> 58) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 52) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 46) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 40) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 34) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 28) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 22) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 16) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 10) & 0x3f) as usize], | ||
CHAR_TABLE[((self.n >> 4) & 0x3f) as usize], | ||
CHAR_TABLE[(self.n & 0xf) as usize], | ||
]; | ||
|
||
let str = str::from_utf8(&buf).expect("characters are valid UTF-8"); | ||
debug_assert!(str.len() == ID_LENGTH); | ||
|
||
write!(f, "{str}") | ||
} | ||
} | ||
|
||
impl FromStr for Id { | ||
type Err = Error; | ||
|
||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
if value.len() != 6 { | ||
/* Support ID generated in the old 32-bit format */ | ||
if value.len() == 6 { | ||
let mut n: Inner = 0; | ||
|
||
for (pos, char) in value.as_bytes().iter().enumerate() { | ||
let bits: Option<Inner> = CHAR_TABLE.iter().enumerate().find_map(|(bits, c)| { | ||
(char == c).then(|| bits.try_into().expect("bits not 64 bits")) | ||
}); | ||
|
||
match bits { | ||
None => return Err(Error::IllegalCharacters), | ||
Some(bits) => { | ||
if pos < 5 { | ||
n = (n << 6) | bits; | ||
} else { | ||
n = (n << 2) | bits; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Ok(Self { n }); | ||
} | ||
|
||
if value.len() != ID_LENGTH { | ||
return Err(Error::WrongSize); | ||
} | ||
|
||
let mut n: u32 = 0; | ||
let mut n: Inner = 0; | ||
|
||
for (pos, char) in value.chars().enumerate() { | ||
let bits: Option<u32> = CHAR_TABLE.iter().enumerate().find_map(|(bits, c)| { | ||
(char == *c).then(|| bits.try_into().expect("bits not 32 bits")) | ||
for (pos, char) in value.as_bytes().iter().enumerate() { | ||
let bits: Option<Inner> = CHAR_TABLE.iter().enumerate().find_map(|(bits, c)| { | ||
(char == c).then(|| bits.try_into().expect("bits not 64 bits")) | ||
}); | ||
|
||
match bits { | ||
None => return Err(Error::IllegalCharacters), | ||
Some(bits) => { | ||
if pos < 5 { | ||
if pos < ID_LENGTH - 1 { | ||
n = (n << 6) | bits; | ||
} else { | ||
n = (n << 2) | bits; | ||
n = (n << 4) | bits; | ||
} | ||
} | ||
} | ||
|
@@ -77,32 +109,61 @@ impl FromStr for Id { | |
} | ||
} | ||
|
||
impl From<u32> for Id { | ||
fn from(n: u32) -> Self { | ||
impl From<Inner> for Id { | ||
fn from(n: Inner) -> Self { | ||
Self { n } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::i64; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn convert_u32_to_id_and_back() { | ||
fn convert_inner_to_id_and_back() { | ||
let id = Id::from(0); | ||
assert_eq!(id.to_string(), "aaaaaa"); | ||
assert_eq!(id.as_u32(), 0); | ||
assert_eq!(id.to_string(), "aaaaaaaaaaa"); | ||
assert_eq!(id.as_inner(), 0); | ||
assert_eq!(Id::from_str(&id.to_string()).unwrap(), id); | ||
|
||
let id = Id::from(-1); | ||
assert_eq!(id.to_string(), "++++++++++p"); | ||
assert_eq!(id.as_inner(), -1); | ||
assert_eq!(Id::from_str(&id.to_string()).unwrap(), id); | ||
|
||
let id = Id::from(0xffffffff); | ||
assert_eq!(id.to_string(), "+++++d"); | ||
assert_eq!(id.as_u32(), 0xffffffff); | ||
assert_eq!(id.to_string(), "aaaaap++++p"); | ||
assert_eq!(id.as_inner(), 0xffffffff); | ||
assert_eq!(Id::from_str(&id.to_string()).unwrap(), id); | ||
|
||
let id = Id::from(i64::MAX); | ||
assert_eq!(id.to_string(), "F+++++++++p"); | ||
assert_eq!(id.as_inner(), i64::MAX); | ||
assert_eq!(Id::from_str(&id.to_string()).unwrap(), id); | ||
|
||
let id = Id::from(i64::MIN); | ||
assert_eq!(id.to_string(), "Gaaaaaaaaaa"); | ||
assert_eq!(id.as_inner(), i64::MIN); | ||
assert_eq!(Id::from_str(&id.to_string()).unwrap(), id); | ||
} | ||
|
||
#[test] | ||
fn convert_id_from_string() { | ||
assert!(Id::from_str("abDE+-").is_ok()); | ||
/* Support ID generated in the old 32-bit format */ | ||
//assert!(Id::from_str("abDE+-").is_ok()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this outcommented? Also, please use proper Rust comments, i.e. |
||
assert!(Id::from_str("#bDE+-").is_err()); | ||
assert!(Id::from_str("abDE+-1").is_err()); | ||
assert!(Id::from_str("abDE+").is_err()); | ||
|
||
/* New 64-bit format */ | ||
assert_eq!( | ||
Id::from_str("abDE+-12345").unwrap(), | ||
Id::from(6578377758007225) | ||
); | ||
assert!(Id::from_str("#bDE+-12345").is_err()); | ||
assert!(Id::from_str("abDE+-123456").is_err()); | ||
assert!(Id::from_str("abDE+12345").is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stupid question: what's the point of this type alias?