From 6d0df0fe7caebc44e9f05058c7b4cb9b6fd97ed9 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 4 Aug 2024 22:01:54 +0300 Subject: kittybox-indieauth: allow using custom RNGs for PKCE --- indieauth/src/pkce.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'indieauth/src/pkce.rs') diff --git a/indieauth/src/pkce.rs b/indieauth/src/pkce.rs index bf8d1a0..88444b6 100644 --- a/indieauth/src/pkce.rs +++ b/indieauth/src/pkce.rs @@ -47,12 +47,21 @@ impl PKCEVerifier { /// Generate a new PKCE verifier string of 128 bytes in length. #[allow(clippy::new_without_default)] pub fn new() -> Self { - let bytes = rand::thread_rng() + Self::from_rng(&mut rand::thread_rng()) + } + + /// Generate a new PKCE string of 128 bytes in length, using + /// the provided random number generator. + pub fn from_rng(rng: &mut (impl rand::CryptoRng + rand::Rng)) -> Self { + use rand::{Rng, distributions::Alphanumeric}; + + let bytes = rng .sample_iter(&Alphanumeric) .take(128) .collect::>(); Self(String::from_utf8(bytes).unwrap()) } + } /// A PKCE challenge as described in [RFC7636]. -- cgit 1.4.1