Code Monkey home page Code Monkey logo

Comments (3)

mattosaurus avatar mattosaurus commented on June 12, 2024

Yes, you can just pass the network stream object directly to the method. Use of memory stream in the docs is just as an example.

from pgpcore.

rinzed avatar rinzed commented on June 12, 2024

I ran into a similar problem today and I have found at least one case in which a non-seekable input stream is not working.

I was using Azure Blob storage and was using a Azure.Storage.NonDisposingStream, which I was trying to Encrypt & Sign using EncryptStreamAndSignAsync.

This resulted in the following exception:

System.NotSupportedException: Specified method is not supported.
   at Azure.Core.Pipeline.RetriableStream+RetriableStreamImpl.get_Length (Azure.Storage.Blobs, Version=12.19.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at Azure.Storage.NonDisposingStream.get_Length (Azure.Storage.Blobs, Version=12.19.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at PgpCore.PGP.ChainLiteralStreamOut (PgpCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=e84be4b896fe5158)
   at PgpCore.PGP+<OutputEncryptedAsync>d__36.MoveNext (PgpCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=e84be4b896fe5158)
   ...
   at PgpCore.PGP+<EncryptAndSignAsync>d__101.MoveNext (PgpCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=e84be4b896fe5158)
   ...
   at PgpCore.PGP+<EncryptStreamAndSignAsync>d__104.MoveNext (PgpCore, Version=6.0.0.0, Culture=neutral, PublicKeyToken=e84be4b896fe5158)
   ...

Some streams are not seakable and can throw an Exception while checking for the Length of the stream. This is documented by Microsoft, see Stream.Length.

I have worked around this issue by copying the data from my Blob storage stream to a MemoryStream before passing it to PGPCore.

using var seekableInputStream = new MemoryStream();
await inputStream.CopyToAsync(seekableInputStream);
await pgp.EncryptStreamAndSignAsync(inputStream, outputStream);

Not sure of more changes are needed, but looking at the PGPCore code, the issue is with the initialization of PGPLiteralDataGenerator using the Length of the input stream, without checking if the input stream CanSeek equals true.
Looking at bouncycastle documentation, there should also be an option to initialize without specifiling the length of the input stream.
https://javadoc.io/static/org.bouncycastle/bcpg-jdk15on/1.64/index.html?org/bouncycastle/openpgp/PGPLiteralDataGenerator.html

from pgpcore.

mattosaurus avatar mattosaurus commented on June 12, 2024

Thanks for doing some initial investigation on this. I seem to remember that there's some other points where we need a seekable stream but I'll take a look when I get a chance to see if changes to PGPLiteralDataGenerator is enough to resolve it.

from pgpcore.

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.