Beispiel #1
0
 protected override void HashCore(byte[] array, int ibStart, int cbSize)
 {
     if (HashValue is null)
     {
         Initialize();
     }
     _digest.BlockUpdate(array, ibStart, cbSize);
 }
Beispiel #2
0
        internal static byte[] Digest(byte[] data)
        {
            Sha512tDigest digest = new Sha512tDigest(256);

            digest.BlockUpdate(data, 0, data.Length);
            byte[] output = new byte[32];
            digest.DoFinal(output, 0);
            return(output);
        }
Beispiel #3
0
        /// <summary>
        /// Hashes a list of byte arrays, prefixing each one with its length.
        /// </summary>
        public byte[] Sum(byte[] domain, params byte[][] datas)
        {
            using (var stream = new MemoryStream())
            {
                byte[] ulongBytes;

                foreach (var data in datas)
                {
                    ulongBytes = this.UInt64ToBytes((ulong)data.Length);

                    stream.Write(ulongBytes, 0, ulongBytes.Length);
                    stream.Write(data, 0, data.Length);
                }

                ulongBytes = this.UInt64ToBytes((ulong)domain.Length);

                stream.Write(ulongBytes, 0, ulongBytes.Length);
                stream.Write(domain, 0, domain.Length);

                var result = stream.ToArray();

                byte[] hash;
                var    sha = new Sha512tDigest(256);

                try
                {
                    sha.BlockUpdate(result, 0, result.Length);
                    hash = new byte[sha.GetDigestSize()];
                    sha.DoFinal(hash, 0);
                }
                finally
                {
                    sha.Finish();
                }

                return(hash);
            }
        }
Beispiel #4
0
        public void Should_GeneratePointOnCurve_When_RandomHashesArePassed()
        {
            var curveParams = NistNamedCurves.GetByName("P-256");

            var swu    = new Swu(((FpCurve)curveParams.Curve).Q, curveParams.Curve.B.ToBigInteger());
            var rng    = new SecureRandom();
            var sha    = new Sha512tDigest(256);
            var random = new byte[32];

            for (int i = 0; i <= 10000; i++)
            {
                var hash = new byte[32];

                rng.NextBytes(random);
                sha.BlockUpdate(random, 0, random.Length);
                sha.DoFinal(hash, 0);

                var(x, y) = swu.HashToPoint(hash);
                Assert.True(curveParams.Curve.CreatePoint(x, y).IsValid());

                sha.Reset();
            }
        }