Esempio n. 1
0
 public MS2FileHeader(IMS2SizeHeader size, uint id, long offset, CompressionType compressionType = CompressionType.None)
 {
     this.Size            = size ?? throw new ArgumentNullException(nameof(size));
     this.Id              = id;
     this.Offset          = offset;
     this.CompressionType = compressionType;
 }
Esempio n. 2
0
        public async Task Save_OneFileToPath_ArchiveHeaderEqualsExpectedData()
        {
            const string   FileName             = nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            string         headerPath           = FileName + HeaderFileExtension;
            string         dataPath             = FileName + DataFileExtension;
            string         input                = "inputdata123" + nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            string         encryptedInput       = "encrypteddata654" + nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            var            sizeMock             = CreateSizeMock(1, 20, 8);
            IMS2SizeHeader expectedFileInfoSize = sizeMock.Object;
            IMS2SizeHeader expectedFileDataSize = sizeMock.Object;
            long           expectedFileCount    = 1;
            MS2CryptoMode  expectedCryptoMode   = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo    = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsHeader = File.OpenRead(headerPath);
            using var br       = new BinaryReader(fsHeader, EncodingTest, true);
            MS2CryptoMode actualCryptoMode = (MS2CryptoMode)br.ReadUInt32();

            var(actualFileInfoSize, actualFileDataSize, actualFileCount) = await repo.GetArchiveHeaderCrypto().ReadAsync(fsHeader);

            Assert.AreEqual(expectedCryptoMode, actualCryptoMode);
            Assert.AreEqual(expectedFileInfoSize.EncodedSize, actualFileInfoSize.EncodedSize);
            Assert.AreEqual(expectedFileInfoSize.CompressedSize, actualFileInfoSize.CompressedSize);
            Assert.AreEqual(expectedFileInfoSize.Size, actualFileInfoSize.Size);
            Assert.AreEqual(expectedFileDataSize.EncodedSize, actualFileDataSize.EncodedSize);
            Assert.AreEqual(expectedFileDataSize.CompressedSize, actualFileDataSize.CompressedSize);
            Assert.AreEqual(expectedFileDataSize.Size, actualFileDataSize.Size);
            Assert.AreEqual(expectedFileCount, actualFileCount);
        }
Esempio n. 3
0
 public FakeCryptoRepository(MS2CryptoMode cryptoMode, Encoding encoding, string decrypted, string encrypted, IMS2SizeHeader encryptedSize)
 {
     this.CryptoMode    = cryptoMode;
     this.Encoding      = encoding;
     this.Decrypted     = decrypted;
     this.Encrypted     = encrypted;
     this.EncryptedSize = encryptedSize;
 }
Esempio n. 4
0
        public Task WriteAsync(Stream stream, IMS2SizeHeader fileInfoSize, IMS2SizeHeader fileDataSize, long fileCount)
        {
            using var bw = new BinaryWriter(stream, Encoding.ASCII, true);

            bw.Write((uint)fileCount);
            bw.Write(fileDataSize.CompressedSize);
            bw.Write(fileDataSize.EncodedSize);
            bw.Write(fileInfoSize.Size);
            bw.Write(fileInfoSize.CompressedSize);
            bw.Write(fileInfoSize.EncodedSize);
            bw.Write(fileDataSize.Size);

            return(Task.CompletedTask);
        }
Esempio n. 5
0
        public Task <Stream> GetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, bool zlibCompressed)
        {
            Stream result = new MemoryStream(this.Encoding.GetBytes(this.Decrypted));

            return(Task.FromResult(result));
        }
Esempio n. 6
0
        private static async Task <MemoryStream> InternalGetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, IBufferedCipher cipher, bool zlibCompressed)
        {
            using var cs = new CipherStream(input, cipher, null);
            byte[] bytes = new byte[size.Size];

            int readBytes;

            if (zlibCompressed)
            {
                using var z = new ZlibStream(cs, CompressionMode.Decompress, true);
                readBytes   = await z.ReadAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
            }
            else
            {
                readBytes = await cs.ReadAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
            }

            if (readBytes != size.Size)
            {
                throw new ArgumentException("Size bytes from input do not match with header size.", nameof(input));
            }

            return(new MemoryStream(bytes));
        }
Esempio n. 7
0
        private static async Task <MemoryStream> InternalGetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, IMultiArray key, IMultiArray iv, bool zlibCompressed)
        {
            var cipher   = CipherUtilities.GetCipher("AES/CTR/NoPadding");
            var keyParam = ParameterUtilities.CreateKeyParameter("AES", key[(uint)size.CompressedSize]);

            cipher.Init(true, new ParametersWithIV(keyParam, iv[(uint)size.CompressedSize]));

            return(await InternalGetDecryptionStreamAsync(input, size, cipher, zlibCompressed).ConfigureAwait(false));
        }
Esempio n. 8
0
        public static async Task <MemoryStream> GetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, IMultiArray key, IMultiArray iv, bool zlibCompressed)
        {
            using var ms = new MemoryStream();

            byte[] encodedBytes = new byte[size.EncodedSize];
            await input.ReadAsync(encodedBytes, 0, encodedBytes.Length).ConfigureAwait(false);

            var encoder = new Base64Encoder();

            encoder.Decode(encodedBytes, 0, encodedBytes.Length, ms);
            if (ms.Length != size.CompressedSize)
            {
                throw new ArgumentException("Compressed bytes from input do not match with header size.", nameof(input));
            }

            ms.Position = 0;
            return(await InternalGetDecryptionStreamAsync(ms, size, key, iv, zlibCompressed).ConfigureAwait(false));
        }
Esempio n. 9
0
 public async Task <Stream> GetDecryptionStreamAsync(Stream input, IMS2SizeHeader size, bool zlibCompressed) =>
 await CryptoHelper.GetDecryptionStreamAsync(input, size, Cryptography.MS2F.Key, Cryptography.MS2F.IV, zlibCompressed).ConfigureAwait(false);
Esempio n. 10
0
 bool IEquatable <IMS2SizeHeader> .Equals(IMS2SizeHeader other)
 {
     return(this.Equals(other as MS2SizeHeader));
 }