|
1 | | -use std::fmt; |
| 1 | +use std::{fmt, fmt::Write}; |
2 | 2 |
|
3 | 3 | use crate::header::{Entry, HeaderMap, HeaderValue, OccupiedEntry}; |
4 | 4 |
|
5 | | -pub fn basic_auth<U, P>(username: U, password: Option<P>) -> HeaderValue |
| 5 | +pub(crate) fn basic_auth<U, P>(username: U, password: Option<P>) -> HeaderValue |
6 | 6 | where |
7 | 7 | U: fmt::Display, |
8 | 8 | P: fmt::Display, |
9 | 9 | { |
10 | | - use std::io::Write; |
11 | | - |
12 | | - use base64::{prelude::BASE64_STANDARD, write::EncoderWriter}; |
13 | | - |
14 | | - let mut buf = b"Basic ".to_vec(); |
15 | | - { |
16 | | - let mut encoder = EncoderWriter::new(&mut buf, &BASE64_STANDARD); |
17 | | - let _ = write!(encoder, "{username}:"); |
| 10 | + let encoded = { |
| 11 | + let mut buf = b"Basic ".to_vec(); |
| 12 | + let mut buf_str = String::with_capacity(32); |
| 13 | + let _ = write!(buf_str, "{username}:"); |
18 | 14 | if let Some(password) = password { |
19 | | - let _ = write!(encoder, "{password}"); |
| 15 | + let _ = write!(buf_str, "{password}"); |
20 | 16 | } |
21 | | - } |
22 | | - let mut header = HeaderValue::from_bytes(&buf).expect("base64 is always valid HeaderValue"); |
| 17 | + |
| 18 | + let encoded = boring2::base64::encode_block(buf_str.as_bytes()); |
| 19 | + buf.extend(encoded.into_bytes()); |
| 20 | + buf |
| 21 | + }; |
| 22 | + |
| 23 | + let mut header = |
| 24 | + HeaderValue::from_maybe_shared(encoded).expect("base64 is always valid HeaderValue"); |
23 | 25 | header.set_sensitive(true); |
24 | 26 | header |
25 | 27 | } |
|
0 commit comments