Skip to content

Use "inner" / "lower-level" libsodium functions #34

@Jack-Edwards

Description

@Jack-Edwards

Libsodium.js offers some nice wrappers to make using the library a lot simpler. However, using these wrappers means we are often copying data to and from JavaScript on every call.

It's possible to use MemoryView in .NET 7 to give JavaScript access to managed memory. For example:

C#

public static void MemoryViewTest()
{
   byte[] buffer = new byte[10];
   MemoryViewTest(buffer, 10);
}

[JSImport("memoryViewTest", "blazorSodium")]
public static partial void MemoryViewTest([JSMarshalAs<JSType.MemoryView>] ArraySegment<byte> buffer, int length);

JS

export function memoryViewTest(arraySegment, length) {
   var copy = arraySegment.slice();
   copy[0]++;
   arraySegment.set(copy);
}

The buffer created in managed memory can be directly accessed and manipulated by JavaScript without having to copy the data.

One problem (among several) I'm facing is that the lower-level libsodium.js functions have extra, undocumented arguments. I opened a discussion topic for this on the main libsodium repo: jedisct1/libsodium#1222

Another problem I'm facing is libsodium.js rejects the raw arraySegment, as it is neither a Uint8Array nor a string. slice() should work to convert the data into an array that libsodium.js will accept, but that probably means I'll need to write a layer of JavaScript to interop between the C# calls and libsodium.js. Another option may be to create a fork of libsodium.js that tries to slice() the data on it's own, but I really want to avoid this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions