fastauth-core 0.8.0

Core of fastauth: the builder, plugin system, sessions, crypto and routing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Secrets, derived keys, signing, encryption, hashing and password hashing.
//! See `docs/concepts/03-security.md`.

use std::fmt;
use std::sync::Arc;

use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use chacha20poly1305::XChaCha20Poly1305;
use chacha20poly1305::aead::{Aead, KeyInit};
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;
use tokio::sync::Semaphore;
use zeroize::Zeroizing;

use crate::error::BuildError;

type HmacSha256 = Hmac<Sha256>;

/// A master secret. Zeroized on drop; never printed.
#[derive(Clone)]
pub struct Secret(Zeroizing<Vec<u8>>);

impl Secret {
    pub const MIN_LEN: usize = 32;

    /// The least estimated entropy `.build()` accepts for a production `base_url`, as in
    /// Better Auth. See [`Secret::entropy_bits`].
    pub const MIN_ENTROPY_BITS: f64 = 120.0;

    /// A rough entropy estimate: the bytes a guesser can't predict, times log2 of the
    /// alphabet. A run of four or more bytes that repeats earlier bytes (`changeme` again,
    /// `aaaa`) or counts up or down by one (`abcd`, `6543`) counts as two bytes, where it
    /// starts and what it follows. The alphabet is the smallest of hex digits, letters and
    /// digits, base64, printable ASCII or any byte that holds every byte of the secret, but at
    /// most twice the distinct bytes seen, so a secret drawn from a handful of characters
    /// scores low. A generated secret scores about
    /// 440 bits, `openssl rand -hex 16` about 128; one repeated byte scores 0.
    pub fn entropy_bits(&self) -> f64 {
        let bytes: &[u8] = &self.0;
        let mut seen = [false; 256];
        for byte in bytes {
            seen[usize::from(*byte)] = true;
        }
        let distinct = seen.iter().filter(|s| **s).count();
        if distinct < 2 {
            return 0.0;
        }
        let all = |class: fn(&u8) -> bool| bytes.iter().all(class);
        let class = if all(u8::is_ascii_hexdigit)
            && (all(|b| !b.is_ascii_uppercase()) || all(|b| !b.is_ascii_lowercase()))
        {
            16
        } else if all(u8::is_ascii_alphanumeric) {
            62
        } else if all(|b| b.is_ascii_alphanumeric() || b"-_+/=".contains(b)) {
            64
        } else if all(|b| (0x20..0x7f).contains(b)) {
            95
        } else {
            256
        };
        let alphabet = class.min(2 * distinct) as f64;
        let mut unpredictable = 0;
        let mut i = 0;
        while i < bytes.len() {
            let repeat = (0..i)
                .map(|j| {
                    (0..bytes.len() - i)
                        .take_while(|&k| bytes[j + k] == bytes[i + k])
                        .count()
                })
                .max()
                .unwrap_or(0);
            let step = |delta: i16| {
                1 + (i + 1..bytes.len())
                    .take_while(|&k| i16::from(bytes[k]) - i16::from(bytes[k - 1]) == delta)
                    .count()
            };
            let run = repeat.max(step(1)).max(step(-1));
            if run >= 4 {
                unpredictable += 2;
                i += run;
            } else {
                unpredictable += 1;
                i += 1;
            }
        }
        unpredictable as f64 * alphabet.log2()
    }

    /// Raw bytes. Prefer `from_env` or `from_base64` so the secret never sits in source.
    pub fn from_bytes(bytes: impl Into<Vec<u8>>) -> Self {
        Secret(Zeroizing::new(bytes.into()))
    }

    /// Accepts base64url (what `fastauth secret` prints) or, failing that, the raw string.
    pub fn from_base64(value: &str) -> Self {
        match B64.decode(value.trim()) {
            Ok(bytes) if bytes.len() >= Self::MIN_LEN => Secret::from_bytes(bytes),
            _ => Secret::from_bytes(value.as_bytes().to_vec()),
        }
    }

    pub fn from_env(var: &str) -> Result<Self, BuildError> {
        let value = std::env::var(var)
            .map_err(|_| BuildError::new(format!("environment variable `{var}` is not set")))?;
        Ok(Secret::from_base64(&value))
    }

    /// 64 random bytes as base64url: a fresh secret.
    pub fn generate() -> String {
        B64.encode(random_bytes::<64>())
    }

    pub(crate) fn len(&self) -> usize {
        self.0.len()
    }

    fn derive(&self, info: &str) -> Zeroizing<[u8; 32]> {
        let hk = hkdf::Hkdf::<Sha256>::new(Some(b"fastauth"), &self.0);
        let mut out = Zeroizing::new([0u8; 32]);
        hk.expand(info.as_bytes(), out.as_mut())
            .expect("32 bytes is a valid HKDF output length");
        out
    }
}

impl fmt::Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Secret(..)")
    }
}

pub fn random_bytes<const N: usize>() -> [u8; N] {
    let mut buf = [0u8; N];
    getrandom::fill(&mut buf).expect("the OS random number generator is available");
    buf
}

/// 32 random bytes as base64url (43 chars): session tokens, verification values, state.
pub fn random_token() -> String {
    B64.encode(random_bytes::<32>())
}

/// A random string from `alphabet`, without modulo bias.
pub fn random_string(len: usize, alphabet: &[u8]) -> String {
    assert!(!alphabet.is_empty() && alphabet.len() <= 256);
    let limit = 256 - (256 % alphabet.len());
    let mut out = String::with_capacity(len);
    while out.len() < len {
        for byte in random_bytes::<64>() {
            if (byte as usize) < limit && out.len() < len {
                out.push(alphabet[byte as usize % alphabet.len()] as char);
            }
        }
    }
    out
}

pub fn base64url(bytes: &[u8]) -> String {
    B64.encode(bytes)
}

pub fn base64url_decode(s: &str) -> Option<Vec<u8>> {
    B64.decode(s).ok()
}

/// SHA-256 digest. Stored instead of tokens.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Hash([u8; 32]);

impl Hash {
    pub fn sha256(data: impl AsRef<[u8]>) -> Self {
        Hash(Sha256::digest(data.as_ref()).into())
    }

    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    pub fn from_slice(bytes: &[u8]) -> Option<Self> {
        bytes.try_into().ok().map(Hash)
    }
}

impl fmt::Debug for Hash {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Hash({}..)", &base64url(&self.0)[..8])
    }
}

/// Constant-time string equality.
pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
    a.ct_eq(b).into()
}

/// Keys derived from the configured secrets. The first secret signs, verifies and encrypts.
/// Every secret decrypts, so encrypted columns stay readable after a rotation, and every
/// secret verifies a signed session token ([`Keys::verify_token`]), which is only a
/// pointer to a session row, so rotating signs nobody out. A retired secret can't sign
/// anything self-contained (the cookie cache, plugin cookies) that is accepted.
pub struct Keys {
    cookie: Vec<Zeroizing<[u8; 32]>>,
    cache: Vec<Zeroizing<[u8; 32]>>,
    encrypt: Vec<Zeroizing<[u8; 32]>>,
    secrets: Vec<Secret>,
    better_auth: Option<Secret>,
}

impl fmt::Debug for Keys {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Keys({} secrets)", self.secrets.len())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SignPurpose {
    Cookie,
    CookieCache,
}

impl Keys {
    pub(crate) fn new(secrets: Vec<Secret>) -> Self {
        Self {
            cookie: secrets
                .iter()
                .map(|s| s.derive("fastauth/v1/cookie"))
                .collect(),
            cache: secrets
                .iter()
                .map(|s| s.derive("fastauth/v1/cookie-cache"))
                .collect(),
            encrypt: secrets
                .iter()
                .map(|s| s.derive("fastauth/v1/encrypt"))
                .collect(),
            secrets,
            better_auth: None,
        }
    }

    /// Signs and verifies [`SignPurpose::Cookie`] values the way `signing` says.
    pub(crate) fn with_cookie_signing(mut self, signing: &crate::config::CookieSigning) -> Self {
        self.better_auth = match signing {
            crate::config::CookieSigning::Fastauth => None,
            crate::config::CookieSigning::BetterAuth(secret) => Some(secret.clone()),
        };
        self
    }

    fn sign_keys(&self, purpose: SignPurpose) -> &[Zeroizing<[u8; 32]>] {
        match purpose {
            SignPurpose::Cookie => &self.cookie,
            SignPurpose::CookieCache => &self.cache,
        }
    }

    fn mac(key: &[u8; 32], value: &str) -> HmacSha256 {
        let mut mac =
            <HmacSha256 as KeyInit>::new_from_slice(key).expect("HMAC takes any key length");
        mac.update(value.as_bytes());
        mac
    }

    /// `value.signature`, signature base64url. With `CookieSigning::BetterAuth`, a
    /// [`SignPurpose::Cookie`] value is signed in Better Auth's format instead.
    pub fn sign(&self, purpose: SignPurpose, value: &str) -> String {
        if let (SignPurpose::Cookie, Some(secret)) = (purpose, &self.better_auth) {
            return crate::compat::sign_cookie(&secret.0, value);
        }
        let tag = Self::mac(&self.sign_keys(purpose)[0], value)
            .finalize()
            .into_bytes();
        format!("{value}.{}", B64.encode(tag))
    }

    /// The value, when the current (first) secret signed it. For values that carry their
    /// own meaning, like the cookie cache or a plugin's signed cookie.
    ///
    /// `signed` is the value after [`Keys::decode_cookie`]: with `CookieSigning::BetterAuth`,
    /// a cookie as the browser sent it may be percent-encoded.
    pub fn verify<'a>(&self, purpose: SignPurpose, signed: &'a str) -> Option<&'a str> {
        if let (SignPurpose::Cookie, Some(secret)) = (purpose, &self.better_auth) {
            return crate::compat::verify_cookie(&secret.0, signed);
        }
        Self::verify_with(&self.sign_keys(purpose)[..1], signed)
    }

    /// A session token signed with [`SignPurpose::Cookie`] by any configured secret. Only
    /// for raw session tokens, and for markers that only restrict the session they come
    /// with (the `rememberMe: false` flag): a token means nothing until its session row is
    /// found, and a forged marker only shortens a session, so a retired secret can't gain
    /// anything, and values signed before a rotation keep working.
    ///
    /// With `CookieSigning::BetterAuth`, only its one secret verifies, as in Better Auth.
    pub fn verify_token<'a>(&self, signed: &'a str) -> Option<&'a str> {
        if let Some(secret) = &self.better_auth {
            return crate::compat::verify_cookie(&secret.0, signed);
        }
        Self::verify_with(&self.cookie, signed)
    }

    /// A signed cookie's value as [`Keys::verify`] and [`Keys::verify_token`] take it. With
    /// `CookieSigning::BetterAuth`, a value holding a `%` is percent-decoded the way
    /// better-call does (kept as it came when it isn't valid); otherwise it's returned as it is.
    pub fn decode_cookie<'a>(&self, raw: &'a str) -> std::borrow::Cow<'a, str> {
        match self.better_auth {
            Some(_) => crate::compat::decode_cookie(raw),
            None => std::borrow::Cow::Borrowed(raw),
        }
    }

    fn verify_with<'a>(keys: &[Zeroizing<[u8; 32]>], signed: &'a str) -> Option<&'a str> {
        let (value, sig) = signed.rsplit_once('.')?;
        let sig = B64.decode(sig).ok()?;
        keys.iter()
            .any(|key| Self::mac(key, value).verify_slice(&sig).is_ok())
            .then_some(value)
    }

    /// `[key id][24-byte nonce][ciphertext]`.
    pub fn encrypt(&self, plaintext: &[u8]) -> Vec<u8> {
        let cipher =
            XChaCha20Poly1305::new_from_slice(self.encrypt[0].as_ref()).expect("32-byte key");
        let nonce_bytes = random_bytes::<24>();
        let nonce = chacha20poly1305::XNonce::from(nonce_bytes);
        let ciphertext = cipher
            .encrypt(&nonce, plaintext)
            .expect("XChaCha20-Poly1305 encryption cannot fail");
        let mut out = Vec::with_capacity(1 + 24 + ciphertext.len());
        out.push(0);
        out.extend_from_slice(&nonce_bytes);
        out.extend_from_slice(&ciphertext);
        out
    }

    pub fn decrypt(&self, data: &[u8]) -> Option<Vec<u8>> {
        if data.len() < 25 {
            return None;
        }
        let (nonce, ciphertext) = data[1..].split_at(24);
        let nonce = chacha20poly1305::XNonce::try_from(nonce).ok()?;
        self.encrypt.iter().find_map(|key| {
            let cipher = XChaCha20Poly1305::new_from_slice(key.as_ref()).ok()?;
            cipher.decrypt(&nonce, ciphertext).ok()
        })
    }

    /// The key of the rate limiter's account buckets, the same on every instance.
    pub(crate) fn rate_limit_key(&self) -> Zeroizing<[u8; 32]> {
        self.secrets[0].derive("fastauth/v1/rate-limit")
    }

    /// A per-plugin key, so plugins can't reach core's keys.
    pub fn derive_key(&self, plugin_id: &str) -> Zeroizing<[u8; 32]> {
        self.secrets[0].derive(&format!("fastauth/v1/plugin/{plugin_id}"))
    }
}

/// Password hashing. Runs on a blocking thread; see `Passwords`.
pub trait PasswordHasher: Send + Sync + 'static {
    fn hash(&self, password: &str) -> Result<String, String>;
    fn verify(&self, password: &str, hash: &str) -> Result<bool, String>;
    /// Whether a stored hash should be replaced on the next successful sign-in.
    fn needs_rehash(&self, hash: &str) -> bool {
        let _ = hash;
        false
    }
}

/// argon2id with OWASP parameters: 19 MiB, t=2, p=1.
#[derive(Debug, Clone)]
pub struct Argon2id {
    params: argon2::Params,
}

impl Argon2id {
    pub fn owasp() -> Self {
        Self::with_params(19 * 1024, 2, 1)
    }

    pub fn with_params(m_cost_kib: u32, t_cost: u32, p_cost: u32) -> Self {
        let params =
            argon2::Params::new(m_cost_kib, t_cost, p_cost, None).expect("valid argon2 parameters");
        Self { params }
    }

    fn engine(&self) -> argon2::Argon2<'static> {
        argon2::Argon2::new(
            argon2::Algorithm::Argon2id,
            argon2::Version::V0x13,
            self.params.clone(),
        )
    }
}

impl Default for Argon2id {
    fn default() -> Self {
        Self::owasp()
    }
}

impl PasswordHasher for Argon2id {
    fn hash(&self, password: &str) -> Result<String, String> {
        use argon2::password_hash::PasswordHasher as _;
        self.engine()
            .hash_password(password.as_bytes())
            .map(|h| h.to_string())
            .map_err(|e| e.to_string())
    }

    fn verify(&self, password: &str, hash: &str) -> Result<bool, String> {
        use argon2::password_hash::PasswordVerifier as _;
        let parsed =
            argon2::password_hash::phc::PasswordHash::new(hash).map_err(|e| e.to_string())?;
        Ok(self
            .engine()
            .verify_password(password.as_bytes(), &parsed)
            .is_ok())
    }

    fn needs_rehash(&self, hash: &str) -> bool {
        let Ok(parsed) = argon2::password_hash::phc::PasswordHash::new(hash) else {
            return true;
        };
        let Ok(params) = argon2::Params::try_from(&parsed) else {
            return true;
        };
        parsed.algorithm.as_str() != "argon2id"
            || params.m_cost() < self.params.m_cost()
            || params.t_cost() < self.params.t_cost()
    }
}

/// Runs a `PasswordHasher` on the blocking pool behind a semaphore, so a burst of sign-ins
/// can't allocate unbounded argon2 memory.
#[derive(Clone)]
pub struct Passwords {
    hasher: Arc<dyn PasswordHasher>,
    permits: Arc<Semaphore>,
    dummy_hash: Arc<std::sync::OnceLock<String>>,
}

impl fmt::Debug for Passwords {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Passwords(..)")
    }
}

impl Passwords {
    /// At most as many hashes at once as there are CPUs.
    pub fn new(hasher: Arc<dyn PasswordHasher>) -> Self {
        let cpus = std::thread::available_parallelism().map_or(2, |n| n.get());
        Self::with_concurrency(hasher, cpus)
    }

    /// At most `permits` hashes at once (at least one).
    pub fn with_concurrency(hasher: Arc<dyn PasswordHasher>, permits: usize) -> Self {
        Self {
            hasher,
            permits: Arc::new(Semaphore::new(permits.max(1))),
            dummy_hash: Arc::default(),
        }
    }

    /// The permit moves into the blocking task and is released when the hash finishes, not
    /// when the request's future does: a request dropped mid-hash (a client that disconnects,
    /// a timeout layer) leaves argon2 running, and its permit with it.
    async fn run<T: Send + 'static>(
        &self,
        f: impl FnOnce(&dyn PasswordHasher) -> T + Send + 'static,
    ) -> T {
        let permit = self
            .permits
            .clone()
            .acquire_owned()
            .await
            .expect("semaphore is never closed");
        let hasher = self.hasher.clone();
        match tokio::runtime::Handle::try_current() {
            Ok(handle) => handle
                .spawn_blocking(move || {
                    let _permit = permit;
                    f(hasher.as_ref())
                })
                .await
                .expect("password hashing task panicked"),
            Err(_) => f(hasher.as_ref()),
        }
    }

    pub async fn hash(&self, password: &str) -> Result<String, String> {
        let password = Zeroizing::new(password.to_owned());
        self.run(move |h| h.hash(&password)).await
    }

    pub async fn verify(&self, password: &str, hash: &str) -> Result<bool, String> {
        let password = Zeroizing::new(password.to_owned());
        let hash = hash.to_owned();
        self.run(move |h| h.verify(&password, &hash)).await
    }

    pub fn needs_rehash(&self, hash: &str) -> bool {
        self.hasher.needs_rehash(hash)
    }

    /// Spends the same time as a real verification, for unknown emails: one run of the
    /// hasher. The first call hashes the dummy password, which costs what a verification
    /// does, and later calls verify against it.
    pub async fn verify_dummy(&self, password: &str) {
        match self.dummy_hash.get() {
            Some(dummy) => {
                let _ = self.verify(password, &dummy.clone()).await;
            }
            None => {
                let dummy = self
                    .hash("fastauth-dummy-password")
                    .await
                    .unwrap_or_default();
                let _ = self.dummy_hash.set(dummy);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn keys() -> Keys {
        Keys::new(vec![Secret::from_bytes(vec![7u8; 32])])
    }

    #[test]
    fn sign_round_trips_and_rejects_tampering() {
        let keys = keys();
        let signed = keys.sign(SignPurpose::Cookie, "token");
        assert_eq!(keys.verify(SignPurpose::Cookie, &signed), Some("token"));
        assert_eq!(keys.verify(SignPurpose::CookieCache, &signed), None);
        assert_eq!(
            keys.verify(SignPurpose::Cookie, &signed.replace("token", "tokem")),
            None
        );
    }

    #[test]
    fn rotation_keeps_old_tokens_and_ciphertexts_but_not_old_signatures() {
        let old = keys();
        let signed = old.sign(SignPurpose::Cookie, "v");
        let cached = old.sign(SignPurpose::CookieCache, "v");
        let rotated = Keys::new(vec![
            Secret::from_bytes(vec![9u8; 32]),
            Secret::from_bytes(vec![7u8; 32]),
        ]);
        assert_eq!(rotated.verify_token(&signed), Some("v"));
        assert_eq!(rotated.verify(SignPurpose::Cookie, &signed), None);
        assert_eq!(rotated.verify(SignPurpose::CookieCache, &cached), None);
        let fresh = rotated.sign(SignPurpose::CookieCache, "v");
        assert_eq!(rotated.verify(SignPurpose::CookieCache, &fresh), Some("v"));
        assert_eq!(
            rotated.decrypt(&old.encrypt(b"hi")).as_deref(),
            Some(&b"hi"[..])
        );
    }

    #[test]
    fn encryption_round_trips() {
        let keys = keys();
        let sealed = keys.encrypt(b"secret");
        assert_ne!(&sealed[25..], b"secret");
        assert_eq!(keys.decrypt(&sealed).unwrap(), b"secret");
    }

    #[test]
    fn argon2_hashes_and_verifies() {
        let hasher = Argon2id::with_params(1024, 1, 1);
        let hash = hasher.hash("correct horse").unwrap();
        assert!(hash.starts_with("$argon2id$"));
        assert!(hasher.verify("correct horse", &hash).unwrap());
        assert!(!hasher.verify("wrong", &hash).unwrap());
        assert!(Argon2id::owasp().needs_rehash(&hash));
    }

    #[test]
    fn entropy_estimate_separates_generated_from_guessable_secrets() {
        assert!(Secret::from_base64(&Secret::generate()).entropy_bits() > 300.0);
        assert_eq!(Secret::from_bytes(vec![0u8; 32]).entropy_bits(), 0.0);
        assert!(
            Secret::from_base64("password password password pass!").entropy_bits()
                < Secret::MIN_ENTROPY_BITS
        );
    }

    #[test]
    fn random_string_uses_alphabet() {
        let s = random_string(40, b"ab");
        assert_eq!(s.len(), 40);
        assert!(s.bytes().all(|b| b == b'a' || b == b'b'));
    }
}