Ejemplo n.º 1
0
            private static List <string> ReadStringPackage(Stream stream, out byte[] hash)
            {
                var list = new List <string>();
                var buf  = ArrayPool <byte> .Shared.Rent(4096);

                var hasher = IncrementalHash.CreateHash(PackHashAlgo);

                using var zs           = new DeflateStream(stream, CompressionMode.Decompress, true);
                using var hasherStream = new HasherStream(zs, hasher, true);

                Primitives.ReadPrimitive(hasherStream, out uint count);
                for (var i = 0; i < count; ++i)
                {
                    Primitives.ReadPrimitive(hasherStream, out uint lu);
                    var l    = (int)lu;
                    var span = buf.AsSpan(0, l);
                    hasherStream.ReadExact(span);

                    var str = Encoding.UTF8.GetString(span);
                    list.Add(str);
                }

                hash = hasher.GetHashAndReset();
                return(list);
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Writes a strings package to a stream.
            /// </summary>
            /// <param name="stream">A writable stream.</param>
            /// <exception cref="NotImplementedException">Overly long string in strings package.</exception>
            private static void WriteStringPackage(string[] strings, Stream stream, out byte[] hash)
            {
                // ReSharper disable once SuggestVarOrType_Elsewhere
                Span <byte> buf = stackalloc byte[MaxMappedStringSize];

                var hasher = IncrementalHash.CreateHash(HashAlgorithmName.SHA512);

                using (var zs = new DeflateStream(stream, CompressionLevel.Optimal, true))
                {
                    using var hasherStream = new HasherStream(zs, hasher, true);
                    Primitives.WritePrimitive(hasherStream, (uint)strings.Length);

                    foreach (var str in strings)
                    {
                        DebugTools.Assert(str.Length < MaxMappedStringSize);

                        var l = Encoding.UTF8.GetBytes(str, buf);

                        if (l >= MaxMappedStringSize)
                        {
                            throw new NotImplementedException("Overly long string in strings package.");
                        }

                        Primitives.WritePrimitive(hasherStream, (uint)l);
                        hasherStream.Write(buf[..l]);
Ejemplo n.º 3
0
            private static List <string> ReadStringPackage(Stream stream, out byte[] hash)
            {
                var list = new List <string>();
                var buf  = ArrayPool <byte> .Shared.Rent(4096);

                var hasher = IncrementalHash.CreateHash(PackHashAlgo);

                using var zs           = new DeflateStream(stream, CompressionMode.Decompress, true);
                using var hasherStream = new HasherStream(zs, hasher, true);

                Primitives.ReadPrimitive(hasherStream, out uint count);
                for (var i = 0; i < count; ++i)
                {
                    Primitives.ReadPrimitive(hasherStream, out uint lu);
                    var l = (int)lu;
                    var y = hasherStream.Read(buf, 0, l);
                    if (y != l)
                    {
                        throw new InvalidDataException($"Could not read the full length of string #{i}.");
                    }

                    var str = Encoding.UTF8.GetString(buf, 0, l);
                    list.Add(str);
                }

                hash = hasher.GetHashAndReset();
                return(list);
            }
Ejemplo n.º 4
0
        private async Task EnrichWithHashAndUploadAsync(UploadAssetCommand command, string tempFile)
        {
            using (var hashStream = new HasherStream(command.File.OpenRead(), HashAlgorithmName.SHA256))
            {
                await assetFileStore.UploadAsync(tempFile, hashStream);

                command.FileHash = $"{hashStream.GetHashStringAndReset()}{command.File.FileName}{command.File.FileSize}".Sha256Base64();
            }
        }
Ejemplo n.º 5
0
        private async Task EnrichWithHashAndUploadAsync(UploadAssetCommand command, string tempFile)
        {
            using (var uploadStream = command.File.OpenRead())
            {
                using (var hashStream = new HasherStream(uploadStream, HashAlgorithmName.SHA256))
                {
                    await assetFileStore.UploadAsync(tempFile, hashStream);

                    command.FileHash = ComputeHash(command.File, hashStream);
                }
            }
        }
        private async Task <string> UploadAsync(CommandContext context, AssetFile file)
        {
            string hash;

            using (var hashStream = new HasherStream(file.OpenRead(), HashAlgorithmName.SHA256))
            {
                await assetStore.UploadAsync(context.ContextId.ToString(), hashStream);

                hash = $"{hashStream.GetHashStringAndReset()}{file.FileName}{file.FileSize}".Sha256Base64();
            }

            return(hash);
        }
Ejemplo n.º 7
0
        private static string ComputeHash(AssetFile file, HasherStream hashStream)
        {
            var steamHash = hashStream.GetHashStringAndReset();

            return($"{steamHash}{file.FileName}{file.FileSize}".ToSha256Base64());
        }