Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API usage and confusion #156

Open
hannesm opened this issue May 8, 2024 · 1 comment
Open

API usage and confusion #156

hannesm opened this issue May 8, 2024 · 1 comment

Comments

@hannesm
Copy link
Member

hannesm commented May 8, 2024

Hey,

I'm not entirely sure I understand the API design. We have two types, ctx and t -- but what is the fundamental difference?

A ctx we can create by either empty or init -- hold on, should the ctx returned by empty be immutable or better not being used?

A t we receive from get, but also from the functions that take care of computing the hash of a string / string list /...

Now, the nicely introduced get_into_bytes is defined on ctx, so if I computed the hash with digest_string, I can't extract it into an existing byte vector.

Would it be possible to simplify the API and only provide a single type t? Since it is private anyways, I'd expect no issues... What do you think?

@hannesm
Copy link
Member Author

hannesm commented May 8, 2024

I got confused when thinking about this code from mirage-crypto's hmac_drbg RNG:

    let rec go off k v = function
      | 0 -> v
      | 1 ->
        let v = H.hmac_string ~key:k v |> H.to_raw_string in
        let len =
          let rem = len mod H.digest_size in
          if rem = 0 then H.digest_size else rem
        in
        Bytes.unsafe_blit_string v 0 buf off len;
        v
      | i ->
        let v = H.hmac_string ~key:k v |> H.to_raw_string in
        Bytes.unsafe_blit_string v 0 buf off H.digest_size;
        go (off + H.digest_size) k v (pred i)

To avoid further allocations, also a ?len parameter for get_into_bytes would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant