Example #1
0
        internal static byte[] Hash(byte[] data, int offset, int count)
        {
            var hasher = new Sha512();

            hasher.Update(data, offset, count);
            return(hasher.Finish());
        }
Example #2
0
        public static void KeyExchange(Span <byte> sharedKey, ReadOnlySpan <byte> publicKey, ReadOnlySpan <byte> privateKey)
        {
            if (sharedKey.Length != 32)
            {
                throw new ArgumentException("sharedKey.Count != 32");
            }
            if (publicKey.Length != 32)
            {
                throw new ArgumentException("publicKey.Count != 32");
            }
            if (privateKey.Length != 64)
            {
                throw new ArgumentException("privateKey.Count != 64");
            }

            FieldOperations.fe_frombytes(out var edwardsY, publicKey);
            FieldOperations.fe_1(out var edwardsZ);
            MontgomeryCurve25519.EdwardsToMontgomeryX(out var montgomeryX, ref edwardsY, ref edwardsZ);
            Span <byte> h = stackalloc byte[64];

            Sha512.Hash(privateKey.Slice(0, 32), h);
            ScalarOperations.sc_clamp(h);
            MontgomeryOperations.scalarmult(out var sharedMontgomeryX, h, in montgomeryX);
            CryptoBytes.Wipe(h);
            FieldOperations.fe_tobytes(sharedKey, in sharedMontgomeryX);
        }
Example #3
0
        public static void Hash(ReadOnlySpan <byte> data, Span <byte> output)
        {
            var hasher = new Sha512();

            hasher.Update(data);
            hasher.Finish(output);
        }
Example #4
0
        public static byte[] Hash(ReadOnlySpan <byte> data)
        {
            var hasher = new Sha512();

            hasher.Update(data);
            return(hasher.Finish());
        }
Example #5
0
        /// <summary>
        /// Calculates SHA-512 hash value for the given bytes array.
        /// </summary>
        /// <param name="data">Data bytes array</param>
        /// <param name="index">Offset of byte sequence</param>
        /// <param name="length">Sequence length</param>
        /// <returns>Hash bytes</returns>
        public static byte[] Hash(byte[] data, int index, int length)
        {
            Contract.Requires <ArgumentNullException>(data != null);
            Contract.Requires <ArgumentOutOfRangeException>(index >= 0 && length >= 0);
            Contract.Requires <ArgumentException>((index + length) <= data.Length);

            var hasher = new Sha512();

            hasher.Update(data, index, length);
            return(hasher.Finalize());
        }
Example #6
0
        public static void KeyExchange(ArraySegment \ \ sharedKey, ArraySegment \ \ publicKey, ArraySegment \ \ privateKey)
        {
            if (sharedKey.Array == null)
            {
                throw new ArgumentNullException("sharedKey.Array");
            }
            if (publicKey.Array == null)
            {
                throw new ArgumentNullException("publicKey.Array");
            }
            if (privateKey.Array == null)
            {
                throw new ArgumentNullException("privateKey");
            }
            if (sharedKey.Count != 32)
            {
                throw new ArgumentException("sharedKey.Count != 32");
            }
            if (publicKey.Count != 32)
            {
                throw new ArgumentException("publicKey.Count != 32");
            }
            if (privateKey.Count != 64)
            {
                throw new ArgumentException("privateKey.Count != 64");
            }

            FieldElement montgomeryX, edwardsY, edwardsZ, sharedMontgomeryX;

            FieldOperations.fe_frombytes(out edwardsY, publicKey.Array, publicKey.Offset);
            FieldOperations.fe_1(out edwardsZ);
            MontgomeryCurve25519.EdwardsToMontgomeryX(out montgomeryX, ref edwardsY, ref edwardsZ);
            byte[] h = Sha512.Hash(privateKey.Array, privateKey.Offset, 32);//ToDo: Remove alloc
            ScalarOperations.sc_clamp(h, 0);
            MontgomeryOperations.scalarmult(out sharedMontgomeryX, h, 0, ref montgomeryX);
            CryptoBytes.Wipe(h);
            FieldOperations.fe_tobytes(sharedKey.Array, sharedKey.Offset, ref sharedMontgomeryX);
            MontgomeryCurve25519.KeyExchangeOutputHashNaCl(sharedKey.Array, sharedKey.Offset);
        }
Example #7
0
 public static byte[] Hash(byte[] data, int offset, int count)
 {
     var hasher = new Sha512();
     hasher.Update(data, offset, count);
     return hasher.Finish();
 }
Example #8
0
        /// <summary>
        /// Calculates SHA-512 hash value for the given bytes array.
        /// </summary>
        /// <param name="data">Data bytes array</param>
        /// <param name="index">Offset of byte sequence</param>
        /// <param name="length">Sequence length</param>
        /// <returns>Hash bytes</returns>
        public static byte[] Hash(byte[] data, int index, int length)
        {
            Contract.Requires<ArgumentNullException>(data != null);
            Contract.Requires<ArgumentOutOfRangeException>(index >= 0 && length >= 0);
            Contract.Requires<ArgumentException>((index + length) <= data.Length);

            var hasher = new Sha512();
            hasher.Update(data, index, length);
            return hasher.Finalize();
        }