Giter VIP home page Giter VIP logo

Comments (3)

mitsuhiko avatar mitsuhiko commented on July 20, 2024 1

Thank you @ia0. This is great and solves my problem nicely.

from data-encoding.

ia0 avatar ia0 commented on July 20, 2024

Thanks for bringing this up! Your assessment is correct:

  1. It is correct to emit padding multiple times.
  2. To avoid emitting padding multiple times, buffering is needed.
  3. 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.

ia0 avatar ia0 commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.