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

Allow SecretBox.easy to also accept user Uint8List key instead of only generated ones #1

Closed
gabrielpacheco23 opened this issue May 10, 2021 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@gabrielpacheco23
Copy link

gabrielpacheco23 commented May 10, 2021

The problem is that I can't pass my secret key array (Uint8List type) inside the function because it only accepts SecureKey instances and SecureKey objects are only created by this library key generation.

Skycoder42 added a commit that referenced this issue May 11, 2021
@Skycoder42
Copy link
Owner

Hi @gabrielpacheco23 - thanks for trying out the library in it's early stages.

This is actually something I had on my list for quite a while. I just pushed a commit that adds a Sodium.secureCopy method, which allows you to create a SecureKey by copying a Uint8List.

However, I am still unsure about how the API should work with the keys. In other languages, I would just overload the methods to accept both, a SecureKey and a Uint8Array, but I can't do that in dart. So, I though about the following alternative approaches. I would like to hear your opinion, if you have one:

  1. Keep the API as is
  2. Add an additional Uint8List plainKey parameter and make the normal key optional - this API-consumers can decide, which type of key the want to add - however, at the cost of runtime checks, if a key was actually provided.
  3. Add factory constructors to SecureKey, to make it easier to use, as well as a SecureKey.unsafe constructor, which just wraps a Uint8List for easy consumption.

@Skycoder42 Skycoder42 self-assigned this May 11, 2021
@Skycoder42 Skycoder42 added the enhancement New feature or request label May 11, 2021
@gabrielpacheco23
Copy link
Author

gabrielpacheco23 commented May 11, 2021

Thanks for the quick reply! Before seeing your answer I used a workaround with an extension method in the SecretBox class. That's how I made it:

extension KeyExt on SecretBox {
  SecureKey keyFromBytes(Uint8List rawBytes, DynamicLibrary dylib) {
    final sodiumPtr = sodiumffi.SodiumPointer<Uint8>.alloc(
      sodiumffi.LibSodiumFFI(dylib),
      count: rawBytes.length,
    );

    sodiumPtr.fill(rawBytes);
    return SecureKeyFFI(sodiumPtr);
  }
}

I dont know if that's a efficient way to fill the memory with the raw bytes but seems to work.

For your question, I think that making factory constructors to SecureKey would be a nicer approach, since it's more similar to constructor method overloading from other languages, but I don't know it would affect your implementation.

Anyways, great work on making this library! I was just in need of a libsodium wrapper so this came in handy.

@Skycoder42
Copy link
Owner

You are actually quite on point with your implementation, it's basically the same thing the SecureKey classes do internally.
I decided to add factory constructors, and I think I like the outcome. Thanks for your input!

I will close this now as the original problem was solved. If you have any other feedback, feel free to create a new one. :)

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

No branches or pull requests

2 participants