Coder Social home page Coder Social logo

Comments (5)

sungam3r avatar sungam3r commented on July 25, 2024

You may use ToArray safely in both cases but the main idea for RMS usage is to not allocate arrays again and again. The all project is about reusing buffers. If called/returning APIs support only arrays then you may find RMS useless. In the second example I suggest to use byte[] EncryptCbc(readonlyspan) overload so you save one array allocation.

from microsoft.io.recyclablememorystream.

LeaFrock avatar LeaFrock commented on July 25, 2024

I appreciate your reply @sungam3r .

In the second example I suggest to use byte[] EncryptCbc(readonlyspan) overload so you save one array allocation

Would you mind paste some codes as a demo here? I don't know how to convert ReadOnlySequence to Span. What I know is the following way, which seems not correct when using AES.

foreach(var m in ms.GetReadOnlySequence())
{
    byte[] block = aes.EncryptCbc(m.Span, iv, PaddingMode.PKCS7); // Then combine several blocks into a full byte array? Seems not correct.
}

(updated)
Well, I realize the way. Do you mean using GetBuffer instead?

        private async Task<byte[]> EncryptCore(IFormFile file, byte[] key, byte[] iv)
        {
            using var ms = RMS.GetStream(Guid.NewGuid(), "myTag", file.Length);
            await file.CopyToAsync(ms);
            using var aes = Aes.Create();
            aes.Key = key;
            var buffer = ms.GetBuffer();
            var data = aes.EncryptCbc(buffer.AsSpan(0, (int)ms.Length), iv, PaddingMode.PKCS7);
            return data;
        }

from microsoft.io.recyclablememorystream.

sungam3r avatar sungam3r commented on July 25, 2024

Yes - just use span projection over buffer returned from RMS.

from microsoft.io.recyclablememorystream.

sungam3r avatar sungam3r commented on July 25, 2024

In that way RMS ecosystem does not allocate arrays. But you as the caller of .EncryptCbc still allocate since that API was designed to return array. You may try to find other overload or combination of calls to save another array allocation if clients of EncryptCore method can work with shared buffers as well.

from microsoft.io.recyclablememorystream.

LeaFrock avatar LeaFrock commented on July 25, 2024

You may try to find other overload...

Got it. It's another issue but still thanks a lot!

from microsoft.io.recyclablememorystream.

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.