Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practices to compress/encrypt

My problem is related on how store massive informations on a 3D terrain. These informations should be secret and since they are very bulky should also be compressed. I opted for file storage and now I was wondering to know the best pracitces to encrypt/compress (or compress/encrypt) object data to files.

I just don't even know if it would be better to write to file and then compress and encrypt it or if work on a data stream and then write to file.

Any suggestion will be appreciated!

like image 498
Leggy7 Avatar asked Dec 25 '22 16:12

Leggy7


1 Answers

I just don't even know if it would be better to write to file and then compress and encrypt it or if work on a data stream and then write to file.

"Better" is not measurable. In-memory compression and encryption may be faster than directly writing to file, but can easily prove troublesome if the data is larger than what fits in memory.

As you do want the end result stored on disk, I'd approach it like this (first compress, then encrypt, then write to disk):

using (var compressionStream = new CompressionStream(rawData))
{
    using (var encryptionStream = new EncryptionStream(compressionStream))
    {
        using (var fileStream = new FileStream("outputfile"))
        {
            encryptionStream.CopyTo(fileStream);
        }
    }
}

The implementations of CompressionStream and EncryptionStream of course depend on the APIs you use.

like image 156
CodeCaster Avatar answered Jan 01 '23 19:01

CodeCaster