Comments (3)
Thank you @ia0. This is great and solves my problem nicely.
from data-encoding.
Thanks for bringing this up! Your assessment is correct:
- It is correct to emit padding multiple times.
- To avoid emitting padding multiple times, buffering is needed.
- The library doesn't provide any buffering capabilities.
I'm currently in a considerable refactor of the internals of the library to address #69 without performance cost. So this comes at the right time to be taken into account. I would need to think more about it to find a satisfying solution, but I can provide you a workaround right now to unblock you until there's a release solving your problem in a nice way:
fn basic_auth(username: &str, password: Option<&str>) -> String {
let mut buffer = Base64Buf::default(); // This line is new and replaces BASE64.
let mut output = "Basic ".to_string();
buffer.encode_append(username.as_bytes(), &mut output);
buffer.encode_append(b":", &mut output);
if let Some(password) = password {
buffer.encode_append(password.as_bytes(), &mut output);
}
buffer.finalize(&mut output); // This line is new.
output
}
#[derive(Default)]
struct Base64Buf {
buf: [u8; 4],
}
impl Base64Buf {
fn encode_append(&mut self, mut input: &[u8], output: &mut String) {
if self.buf[3] != 0 {
let len = self.buf[3] as usize;
let add = std::cmp::min(3 - len, input.len());
self.buf[len..][..add].copy_from_slice(&input[..add]);
self.buf[3] += add as u8;
input = &input[add..];
if self.buf[3] == 3 {
BASE64.encode_append(&self.buf[..3], output);
self.buf[3] = 0;
}
}
let len = input.len() / 3 * 3;
BASE64.encode_append(&input[..len], output);
input = &input[len..];
let len = input.len();
self.buf[..len].copy_from_slice(input);
self.buf[3] = len as u8;
}
fn finalize(self, output: &mut String) {
let len = self.buf[3] as usize;
BASE64.encode_append(&self.buf[..len], output);
}
}
If you don't want to use this workaround until I release something that solves both #69 and this issue, then I could consider providing a generic version of this buffered struct. But it will also take some time and will probably be removed/modified when going to v3.
from data-encoding.
Hi @mitsuhiko , I gave up on finding a nice generic solution solving #69 and this issue at the same time (I'll try again next year), and will instead solve both separately. I've merged #89 which should fix your issue. You may subscribe to #90 to be notified when it's published on crates.io. In the meantime, you may use the git version and provide feedback if it doesn't solve your issue. Here is how it would look like now:
// Sadly, the following is needed until 3.0.0 is released.
// You may share a single such static in your program.
static BASE64: data_encoding::Encoding = data_encoding::BASE64;
fn basic_auth(username: &str, password: Option<&str>) -> String {
let mut buf = "Basic ".to_string();
let mut encoder = BASE64.new_encoder(&mut buf);
encoder.append(username.as_bytes());
encoder.append(b":");
if let Some(password) = password {
encoder.append(password.as_bytes());
}
encoder.finalize();
buf
}
from data-encoding.
Related Issues (20)
- Consider `check_trailing_bits=false` for BASE64_MIME decoder HOT 1
- Release 3.0.0
- (std -> core)::fmt::Display for DecodeError and DecodeKind HOT 2
- feature `std` leaking when using macro in no_std env HOT 7
- Missing a new line and decode error HOT 2
- Rust 2021 edition is a major, not a "patch" change HOT 6
- Removing dependency on syn HOT 7
- Make `{encode,decode}_len` const fns? HOT 4
- Encode into an "impl std::fmt::Write" and/or "impl std::io::Write" HOT 5
- Breaking change wish list
- Use doc_auto_cfg once stable HOT 2
- Use as_chunks family of functions once stable
- Internal symbols array is publicly exposed and unsound HOT 4
- Deny warnings in rustdoc
- Question about Specification HOT 6
- Release 2.5.0
- SIMD optimization HOT 4
- `Encoding::encode_mut` is very code-size heavy HOT 15
- feature request - base58 d/encoding HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data-encoding.