public async Task Should_produce_read_bytes_hash(int bufferSize)
        {
            var data = CryptographicBuffer.GenerateRandom(2048);

            var expected = HashAlgorithmProvider
                .OpenAlgorithm(HashAlgorithmNames.Sha256)
                .HashData(data);

            using (var file = new InMemoryRandomAccessStream())
            {
                await file.WriteAsync(data);
                file.Seek(0);

                var buffer = WindowsRuntimeBuffer.Create(bufferSize);

                using (var hashed = new HashedInputStream(file))
                {
                    for (var i = 0; i < 8; i++)
                    {
                        await hashed.ReadAsync(
                            buffer, buffer.Capacity);
                    }

                    var hash = hashed.GetHashAndReset();
                    Assert.Equal(expected.ToArray(), hash.ToArray());
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the headers of the specified database file stream.
        /// </summary>
        /// <param name="input">The input stream.</param>
        /// <returns>The read result.</returns>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="input"/> cannot be <c>null</c>.
        /// </exception>
        public static async Task<ReadHeaderResult> Headers(IInputStream input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            var hash = new HashedInputStream(input);
            var buffer = WindowsRuntimeBuffer.Create(128);

            // Signature
            buffer = await hash.ReadAsync(buffer, 8);

            var format = CheckSignature(buffer);
            if (format != FileFormats.Supported)
            {
                return new ReadHeaderResult
                {
                    Format = format,
                };
            }

            // Schema version
            buffer = await hash.ReadAsync(buffer, 4);

            var version = GetVersion(buffer);
            format = CheckCompatibility(version);
            switch (format)
            {
                case FileFormats.Supported:
                case FileFormats.PartialSupported:
                    break;

                default:
                    return new ReadHeaderResult
                    {
                        Format = format,
                    };
            }

            // Fields
            var headers = await GetHeaders(hash, buffer);
            headers.Hash = hash.GetHashAndReset();

            return new ReadHeaderResult
            {
                Format = format,
                Headers = headers,
            };
        }