public async Task WriteRead8ByteNumber(ulong value)
        {
            using MemoryStream ms         = new MemoryStream();
            using BinaryDataStream stream = new BinaryDataStream(ms);
            await stream.Write8BitNumber(value);

            await stream.FlushAsync();

            stream.Position = 0;

            ulong readedValue = await stream.Read8BitNumber();

            Assert.AreEqual(value, readedValue);
        }
Ejemplo n.º 2
0
        public static async Task <ContainerHeader> ReadFromStream(Stream stream)
        {
            using (BinaryDataStream dataStream = new BinaryDataStream(stream, false))
            {
                await CheckPrefix(dataStream);

                ContainerHeader header = new ContainerHeader();
                header.Version = await dataStream.Read2BitNumber();

                if (header.Version != DefaultVersion)
                {
                    throw new InvalidOperationException($"Encryption file version {header.Version} is not supported.");
                }

                header.Reserved = await dataStream.Read4BitNumber();

                header.KeyType = (ContainerKeyType)await dataStream.Read4BitNumber();

                int encryptedKeySize = header.KeyType switch
                {
                    ContainerKeyType.Rsa2048 => 256,
                    ContainerKeyType.Rsa3072 => 384,
                    ContainerKeyType.Rsa4096 => 512,
                    _ => throw new NotSupportedException($"Not support key type {header.KeyType}")
                };

                header.EncryptedDataSize = await dataStream.Read8BitNumber();

                header.CertificateThumbprint = await dataStream.ReadConstatntString(40, Encoding.ASCII);

                header.EncryptedKey     = new byte[encryptedKeySize];
                header.InitializeVector = new byte[16];

                await dataStream.ReadAsync(header.EncryptedKey, 0, header.EncryptedKey.Length);

                await dataStream.ReadAsync(header.InitializeVector, 0, header.InitializeVector.Length);

                return(header);
            }
        }