Пример #1
0
        public static void FinalizeSuccess(Type algorithmType)
        {
            var a = (MacAlgorithm)Activator.CreateInstance(algorithmType);

            using (var k = new Key(a))
            {
                var state = default(IncrementalMac);

                Assert.Null(state.Algorithm);

                IncrementalMac.Initialize(k, out state);

                Assert.Same(a, state.Algorithm);

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

                var actual = IncrementalMac.Finalize(ref state);

                Assert.Null(state.Algorithm);

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

                Assert.Equal(expected, actual);
            }
        }
Пример #2
0
        public static void FinalizeWithSpanSuccess(MacAlgorithm a)
        {
            using (var k = new Key(a))
            {
                var state = default(IncrementalMac);

                Assert.Null(state.Algorithm);

                IncrementalMac.Initialize(k, out state);

                Assert.Same(a, state.Algorithm);

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

                var actual = new byte[a.MacSize];

                IncrementalMac.Finalize(ref state, actual);

                Assert.Null(state.Algorithm);

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

                Assert.Equal(expected, actual);
            }
        }
Пример #3
0
        public static void Mac()
        {
            #region Incremental MAC

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

            // create a new key
            using (var key = Key.Create(algorithm))
            {
                // initialize the state with the key
                IncrementalMac.Initialize(key, out var state);

                // incrementally update the state with some data
                var lines = new[]
                {
                    "It is a dark time for the\n",
                    "Rebellion. Although the Death\n",
                    "Star has been destroyed,\n",
                    "Imperial troops have driven the\n",
                    "Rebel forces from their hidden\n",
                    "base and pursued them across\n",
                    "the galaxy.\n"
                };
                foreach (var line in lines)
                {
                    IncrementalMac.Update(ref state, Encoding.UTF8.GetBytes(line));
                }

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

                Assert.Equal(algorithm.Mac(key, Encoding.UTF8.GetBytes(string.Concat(lines))), mac);
            }

            #endregion
        }
Пример #4
0
        public static void UpdateInvalid()
        {
            var state = default(IncrementalMac);

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