aboutsummaryrefslogtreecommitdiff
path: root/src/model.rs
diff options
context:
space:
mode:
authorJoris Guyonvarch2025-10-25 18:58:17 +0200
committerJoris Guyonvarch2025-10-25 18:58:17 +0200
commit8fcce388b0a66ca69c71f2f428940a6f7f9258f2 (patch)
tree8bc22ca7b8c9fc530f49c3f3458ff3216a249232 /src/model.rs
parent73745b090846f3ffeeb5b279f8685f9212d88ef9 (diff)
Use base62 characters for the file idmain
Diffstat (limited to 'src/model.rs')
-rw-r--r--src/model.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/model.rs b/src/model.rs
index ed4fbf8..12b0433 100644
--- a/src/model.rs
+++ b/src/model.rs
@@ -1,6 +1,5 @@
-use base64::{engine::general_purpose::URL_SAFE, Engine as _};
use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
-use rand_core::{OsRng, RngCore};
+use rand::{distributions::Alphanumeric, Rng};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File {
@@ -18,15 +17,17 @@ pub fn local_time() -> DateTime<Local> {
}
}
-// Using 20 bytes (160 bits) to file identifiers
+// Using 28 Base 62 characters, which corresponds to 166 bits of entropy
// https://owasp.org/www-community/vulnerabilities/Insufficient_Session-ID_Length
// https://www.rfc-editor.org/rfc/rfc6749.html#section-10.10
-const FILE_ID_BYTES: usize = 20;
+const FILE_ID_CHARS: usize = 28;
pub fn generate_file_id() -> String {
- let mut token = [0u8; FILE_ID_BYTES];
- OsRng.fill_bytes(&mut token);
- URL_SAFE.encode(token)
+ rand::thread_rng()
+ .sample_iter(&Alphanumeric)
+ .take(FILE_ID_CHARS)
+ .map(char::from)
+ .collect()
}
const FORMAT: &str = "%Y-%m-%d %H:%M:%S";