Example #1
0
        public static void Hash()
        {
            #region Incremental Hash

            // select the BLAKE2b-256 algorithm
            var algorithm = HashAlgorithm.Blake2b_256;

            // initialize the state with the algorithm
            IncrementalHash.Initialize(algorithm, out var state);

            // incrementally update the state with some data
            var lines = new[]
            {
                "It is a period of civil war.\n",
                "Rebel spaceships, striking\n",
                "from a hidden base, have won\n",
                "their first victory against\n",
                "the evil Galactic Empire.\n"
            };
            foreach (var line in lines)
            {
                IncrementalHash.Update(ref state, Encoding.UTF8.GetBytes(line));
            }

            // finalize the computation and get the result
            var hash = IncrementalHash.Finalize(ref state);

            #endregion

            Assert.Equal(algorithm.Hash(Encoding.UTF8.GetBytes(string.Concat(lines))), hash);
        }
Example #2
0
        public static void FinalizeWithSpanSuccess(Type algorithmType)
        {
            var a = (HashAlgorithm)Activator.CreateInstance(algorithmType);

            var state = default(IncrementalHash);

            Assert.Null(state.Algorithm);

            IncrementalHash.Initialize(a, out state);

            Assert.Same(a, state.Algorithm);

            IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(0, 100));
            IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(100, 100));
            IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(200, 100));

            var actual = new byte[a.HashSize];

            IncrementalHash.Finalize(ref state, actual);

            Assert.Null(state.Algorithm);

            var expected = a.Hash(Utilities.RandomBytes.Slice(0, 300));

            Assert.Equal(expected, actual);
        }
Example #3
0
        public static void FinalizeSuccess(HashAlgorithm a)
        {
            var state = default(IncrementalHash);

            Assert.Null(state.Algorithm);

            IncrementalHash.Initialize(a, out state);

            Assert.Same(a, state.Algorithm);

            IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(0, 100));
            IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(100, 100));
            IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(200, 100));

            var actual = IncrementalHash.Finalize(ref state);

            Assert.Null(state.Algorithm);

            var expected = a.Hash(Utilities.RandomBytes.Slice(0, 300));

            Assert.Equal(expected, actual);
        }
Example #4
0
        public static void UpdateInvalid()
        {
            var state = default(IncrementalHash);

            Assert.Throws <InvalidOperationException>(() => IncrementalHash.Update(ref state, Utilities.RandomBytes.Slice(0, 100)));
        }