Improved readability of gen_password.rs by intoducing the bitflags
This commit is contained in:
parent
5566397e66
commit
c40ffafd69
17
Cargo.lock
generated
17
Cargo.lock
generated
@ -185,6 +185,12 @@ version = "1.3.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "2.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6776fc96284a0bb647b615056fc496d1fe1644a7ab01829818a6d91cae888b84"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitvec"
|
name = "bitvec"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
@ -357,7 +363,7 @@ version = "3.2.25"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
|
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
"clap_derive",
|
"clap_derive",
|
||||||
"clap_lex",
|
"clap_lex",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
@ -1308,6 +1314,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
|
"bitflags 2.3.1",
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"futures",
|
"futures",
|
||||||
@ -1572,7 +1579,7 @@ version = "0.2.16"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
|
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1754,7 +1761,7 @@ version = "0.37.19"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
|
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
"errno",
|
"errno",
|
||||||
"io-lifetimes",
|
"io-lifetimes",
|
||||||
"libc",
|
"libc",
|
||||||
@ -2201,7 +2208,7 @@ dependencies = [
|
|||||||
"ahash 0.7.6",
|
"ahash 0.7.6",
|
||||||
"atoi",
|
"atoi",
|
||||||
"bigdecimal",
|
"bigdecimal",
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
"chrono",
|
"chrono",
|
||||||
@ -2371,7 +2378,7 @@ version = "0.9.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "303db260110c238e3af77bb9dff18bf7a5b5196f783059b0852aab75f91d5a16"
|
checksum = "303db260110c238e3af77bb9dff18bf7a5b5196f783059b0852aab75f91d5a16"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
"bytes",
|
"bytes",
|
||||||
"chrono",
|
"chrono",
|
||||||
"derive_more",
|
"derive_more",
|
||||||
|
@ -14,6 +14,7 @@ members = [".", "migration"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.71"
|
anyhow = "1.0.71"
|
||||||
arrayvec = "0.7.2"
|
arrayvec = "0.7.2"
|
||||||
|
bitflags = "2.3.1"
|
||||||
chacha20poly1305 = { version = "0.10.1", features = ["std"] }
|
chacha20poly1305 = { version = "0.10.1", features = ["std"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
futures = "0.3.28"
|
futures = "0.3.28"
|
||||||
|
@ -1,26 +1,38 @@
|
|||||||
use crate::handlers::markups::deletion_markup;
|
use crate::handlers::markups::deletion_markup;
|
||||||
use arrayvec::{ArrayString, ArrayVec};
|
use arrayvec::{ArrayString, ArrayVec};
|
||||||
use rand::{rngs::OsRng, seq::SliceRandom};
|
use rand::{rngs::OsRng, seq::SliceRandom};
|
||||||
use std::str::from_utf8_unchecked;
|
use std::{fmt::Write, str::from_utf8_unchecked};
|
||||||
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
|
use teloxide::{adaptors::Throttle, prelude::*, types::ParseMode};
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
const CHARS: &[u8] = br##"!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"##;
|
const CHARS: &[u8] = br##"!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"##;
|
||||||
|
|
||||||
|
bitflags::bitflags! {
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
struct PasswordFlags: u8 {
|
||||||
|
const LOWERCASE = 0b0001;
|
||||||
|
const UPPERCASE = 0b0010;
|
||||||
|
const NUMBER = 0b0100;
|
||||||
|
const SPECIAL_CHARACTER = 0b1000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true if the generated master password is valid.
|
/// Returns true if the generated master password is valid.
|
||||||
/// It checks that it has at least one lowercase, one lowercase and one punctuation char
|
/// It checks that it has at least one lowercase, one lowercase and one punctuation char
|
||||||
#[inline]
|
#[inline]
|
||||||
fn check_generated_password(password: &[u8]) -> bool {
|
fn check_generated_password(password: &[u8]) -> bool {
|
||||||
let mut flags: u8 = 0;
|
let mut flags = PasswordFlags::empty();
|
||||||
for &byte in password {
|
for &byte in password {
|
||||||
match byte {
|
match byte {
|
||||||
b'a'..=b'z' => flags |= 0b1,
|
b'a'..=b'z' => flags |= PasswordFlags::LOWERCASE,
|
||||||
b'A'..=b'Z' => flags |= 0b10,
|
b'A'..=b'Z' => flags |= PasswordFlags::UPPERCASE,
|
||||||
b'0'..=b'9' => flags |= 0b100,
|
b'0'..=b'9' => flags |= PasswordFlags::NUMBER,
|
||||||
b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~' => flags |= 0b1000,
|
b'!'..=b'/' | b':'..=b'@' | b'['..=b'`' | b'{'..=b'~' => {
|
||||||
|
flags |= PasswordFlags::SPECIAL_CHARACTER
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
if flags == 0b1111 {
|
if flags == PasswordFlags::all() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,17 +41,15 @@ fn check_generated_password(password: &[u8]) -> bool {
|
|||||||
|
|
||||||
/// Continuously generates the password until it passes the checks
|
/// Continuously generates the password until it passes the checks
|
||||||
#[inline]
|
#[inline]
|
||||||
fn generate_passwords() -> [ArrayString<34>; 10] {
|
fn generate_passwords() -> [ArrayString<32>; 10] {
|
||||||
let mut passwords = ArrayVec::new_const();
|
let mut passwords = ArrayVec::new_const();
|
||||||
while !passwords.is_full() {
|
while !passwords.is_full() {
|
||||||
let password: ArrayVec<u8, 32> = (0..32)
|
let password: ArrayVec<u8, 32> = (0..32)
|
||||||
.map(|_| *CHARS.choose(&mut OsRng).unwrap())
|
.map(|_| *CHARS.choose(&mut OsRng).unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
if check_generated_password(&password) {
|
if check_generated_password(&password) {
|
||||||
let mut string = ArrayString::<34>::new_const();
|
let mut string = ArrayString::<32>::new_const();
|
||||||
string.push('`');
|
|
||||||
unsafe { string.push_str(from_utf8_unchecked(&password)) };
|
unsafe { string.push_str(from_utf8_unchecked(&password)) };
|
||||||
string.push('`');
|
|
||||||
passwords.push(string)
|
passwords.push(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,8 +61,7 @@ pub async fn gen_password(bot: Throttle<Bot>, msg: Message) -> crate::Result<()>
|
|||||||
let mut message: ArrayString<{ 10 + 35 * 10 }> = "Passwords:".try_into().unwrap();
|
let mut message: ArrayString<{ 10 + 35 * 10 }> = "Passwords:".try_into().unwrap();
|
||||||
let passwords = spawn_blocking(generate_passwords).await?;
|
let passwords = spawn_blocking(generate_passwords).await?;
|
||||||
for password in passwords {
|
for password in passwords {
|
||||||
message.push('\n');
|
write!(message, "\n`{password}`").unwrap();
|
||||||
message.push_str(&password)
|
|
||||||
}
|
}
|
||||||
bot.send_message(msg.chat.id, message.as_str())
|
bot.send_message(msg.chat.id, message.as_str())
|
||||||
.parse_mode(ParseMode::MarkdownV2)
|
.parse_mode(ParseMode::MarkdownV2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user