Пример #1
0
        public static byte[] ComputeHash(byte[] data, int start, int count, Blake2sConfig config)
        {
            var hasher = Create(config);

            hasher.Update(data, start, count);
            return(hasher.Finish());
        }
Пример #2
0
    public static byte[] GetHash2snet()
    {
        var cfg = new Blake2s.Blake2sConfig()
        {
            OutputSizeInBytes = BenchConfig.HashBytes,
            Key = BenchConfig.Key
        };

        return(Blake2s.Blake2S.ComputeHash(BenchConfig.Data, cfg));
    }
Пример #3
0
        /// <summary>Generates a hash based on a key, salt and personal bytes</summary>
        /// <returns><c>byte</c> hashed message</returns>
        /// <param name="message">Message.</param>
        /// <param name="key">Key.</param>
        /// <param name="salt">Salt.</param>
        /// <param name="personal">Personal string.</param>
        /// <param name="bytes">The size (in bytes) of the desired result.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="SaltOutOfRangeException"></exception>
        /// <exception cref="PersonalOutOfRangeException"></exception>
        /// <exception cref="BytesOutOfRangeException"></exception>
        public static byte[] HashSaltPersonal(byte[] message, byte[] key, byte[] salt, byte[] personal, int bytes = OutBytes)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message", "Message cannot be null");
            }

            if (salt == null)
            {
                throw new ArgumentNullException("salt", "Salt cannot be null");
            }

            if (personal == null)
            {
                throw new ArgumentNullException("personal", "Personal string cannot be null");
            }

            if (key != null && (key.Length > KeyBytesMax || key.Length < KeyBytesMin))
            {
                throw new KeyOutOfRangeException(string.Format("key must be between {0} and {1} bytes in length.", KeyBytesMin, KeyBytesMax));
            }

            if (key == null)
            {
                key = new byte[0];
            }

            if (salt.Length != SaltBytes)
            {
                throw new SaltOutOfRangeException(string.Format("Salt must be {0} bytes in length.", SaltBytes));
            }

            if (personal.Length != PersonalBytes)
            {
                throw new PersonalOutOfRangeException(string.Format("Personal bytes must be {0} bytes in length.", PersonalBytes));
            }

            //validate output length
            if (bytes > BytesMax || bytes < BytesMin)
            {
                throw new BytesOutOfRangeException("bytes", bytes,
                                                   string.Format("bytes must be between {0} and {1} bytes in length.", BytesMin, BytesMax));
            }

            var config = new Blake2sConfig
            {
                Key = key,
                OutputSizeInBytes = bytes,
                Personalization   = personal,
                Salt = salt
            };

            return(ComputeHash(message, 0, message.Length, config));
        }
Пример #4
0
 public Blake2sConfig Clone()
 {
     var result = new Blake2sConfig();
     result.OutputSizeInBytes = OutputSizeInBytes;
     if (Key != null)
         result.Key = (byte[])Key.Clone();
     if (Personalization != null)
         result.Personalization = (byte[])Personalization.Clone();
     if (Salt != null)
         result.Salt = (byte[])Salt.Clone();
     return result;
 }
Пример #5
0
 public Blake2sHasher(Blake2sConfig config)
 {
     if (config == null)
     {
         config = DefaultConfig;
     }
     rawConfig = Blake2sIvBuilder.ConfigS(config); //, null); no tree config;
     if (config.Key != null && config.Key.Length != 0)
     {
         key = new byte[Blake2sCore.BlockSizeInBytes]; //  DOES THIS NEED TO BE THE BLOCK LENGTH?!
         Array.Copy(config.Key, key, config.Key.Length);
     }
     outputSizeInBytes = config.OutputSizeInBytes;
     Init();
 }
Пример #6
0
        /// <summary>
        /// </summary>
        /// <param name="config">A valid Blake2sConfig.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static uint[] ConfigS(Blake2sConfig config)
        {
            var rawConfig = new uint[8];

            //digest length
            if (config.OutputSizeInBytes <= 0 | config.OutputSizeInBytes > 32) //
            {
                throw new ArgumentOutOfRangeException("config.OutputSize");
            }
            rawConfig[0] |= (uint)config.OutputSizeInBytes;  //

            //Key length
            if (config.Key != null)
            {
                if (config.Key.Length > 32) //
                {
                    throw new ArgumentException("config.Key", "Key too long");
                }
                rawConfig[0] |= (uint)(config.Key.Length << 8);  //
            }
            // Fan Out =1 and Max Height / Depth = 1
            rawConfig[0] |= 1 << 16;
            rawConfig[0] |= 1 << 24;
            // Leaf Length and Inner Length 0, no need to worry about them
            // Salt
            if (config.Salt != null)
            {
                if (config.Salt.Length != 8)
                {
                    throw new ArgumentException("config.Salt has invalid length");
                }
                rawConfig[4] = Blake2sCore.BytesToUInt32(config.Salt, 0);
                rawConfig[5] = Blake2sCore.BytesToUInt32(config.Salt, 4);
            }
            // Personalization
            if (config.Personalization != null)
            {
                if (config.Personalization.Length != 8)
                {
                    throw new ArgumentException("config.Personalization has invalid length");
                }
                rawConfig[6] = Blake2sCore.BytesToUInt32(config.Personalization, 0);
                rawConfig[7] = Blake2sCore.BytesToUInt32(config.Personalization, 4);
            }

            return(rawConfig);
        }
Пример #7
0
        public Blake2sConfig Clone()
        {
            var result = new Blake2sConfig();

            result.OutputSizeInBytes = OutputSizeInBytes;
            if (Key != null)
            {
                result.Key = (byte[])Key.Clone();
            }
            if (Personalization != null)
            {
                result.Personalization = (byte[])Personalization.Clone();
            }
            if (Salt != null)
            {
                result.Salt = (byte[])Salt.Clone();
            }
            return(result);
        }
Пример #8
0
        /// <summary>
        /// </summary>
        /// <param name="config">A valid Blake2sConfig.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static uint[] ConfigS(Blake2sConfig config)
        {
            var rawConfig = new uint[8];
            //digest length
            if (config.OutputSizeInBytes <= 0 | config.OutputSizeInBytes > 32) //
                throw new ArgumentOutOfRangeException("config.OutputSize");
            rawConfig[0] |= (uint) config.OutputSizeInBytes; //

            //Key length
            if (config.Key != null)
            {
                if (config.Key.Length > 32) //
                    throw new ArgumentException("config.Key", "Key too long");
                rawConfig[0] |= (uint) (config.Key.Length << 8); //
            }
            // Fan Out =1 and Max Height / Depth = 1
            rawConfig[0] |= 1 << 16;
            rawConfig[0] |= 1 << 24;
            // Leaf Length and Inner Length 0, no need to worry about them
            // Salt
            if (config.Salt != null)
            {
                if (config.Salt.Length != 8)
                    throw new ArgumentException("config.Salt has invalid length");
                rawConfig[4] = Blake2sCore.BytesToUInt32(config.Salt, 0);
                rawConfig[5] = Blake2sCore.BytesToUInt32(config.Salt, 4);
            }
            // Personalization
            if (config.Personalization != null)
            {
                if (config.Personalization.Length != 8)
                    throw new ArgumentException("config.Personalization has invalid length");
                rawConfig[6] = Blake2sCore.BytesToUInt32(config.Personalization, 0);
                rawConfig[7] = Blake2sCore.BytesToUInt32(config.Personalization, 4);
            }

            return rawConfig;
        }
Пример #9
0
        /// <summary>Hashes a message, with an optional key, using the BLAKE2s primitive.</summary>
        /// <param name="message">The message to be hashed.</param>
        /// <param name="key">The key; may be null, otherwise between 16 and 64 bytes.</param>
        /// <param name="bytes">The size (in bytes) of the desired result.</param>
        /// <returns>Returns a byte array.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="BytesOutOfRangeException"></exception>
        /// <exception cref="OverflowException"></exception>
        public static byte[] Hash(byte[] message, byte[] key, int bytes)
        {
            //validate the length of the key
            if (key != null)
            {
                if (key.Length > KeyBytesMax || key.Length < KeyBytesMin)
                {
                    throw new KeyOutOfRangeException(string.Format("key must be between {0} and {1} bytes in length.",
                                                                   KeyBytesMin, KeyBytesMax));
                }
            }
            else
            {
                key = new byte[0];
            }

            //validate output length
            if (bytes > BytesMax || bytes < BytesMin)
            {
                throw new BytesOutOfRangeException("bytes", bytes,
                                                   string.Format("bytes must be between {0} and {1} bytes in length.", BytesMin, BytesMax));
            }

            var config = new Blake2sConfig
            {
                Key = key,
                OutputSizeInBytes = bytes
            };

            if (message == null)
            {
                message = new byte[0];
            }

            return(ComputeHash(message, 0, message.Length, config));
        }
Пример #10
0
 public static byte[] ComputeHash(byte[] data, Blake2sConfig config)
 {
     return(ComputeHash(data, 0, data.Length, config));
 }
Пример #11
0
 public static Hasher Create(Blake2sConfig config)
 {
     return(new Blake2sHasher(config));
 }
Пример #12
0
 public static Hasher Create(Blake2sConfig config)
 {
     return new Blake2sHasher(config);
 }
Пример #13
0
 public static byte[] ComputeHash(byte[] data, int start, int count, Blake2sConfig config)
 {
     var hasher = Create(config);
     hasher.Update(data, start, count);
     return hasher.Finish();
 }
Пример #14
0
 public static byte[] ComputeHash(byte[] data, Blake2sConfig config)
 {
     return ComputeHash(data, 0, data.Length, config);
 }
Пример #15
0
        /// <summary>Generates a hash based on a key, salt and personal bytes</summary>
        /// <returns><c>byte</c> hashed message</returns>
        /// <param name="message">Message.</param>
        /// <param name="key">Key.</param>
        /// <param name="salt">Salt.</param>
        /// <param name="personal">Personal string.</param>
        /// <param name="bytes">The size (in bytes) of the desired result.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="SaltOutOfRangeException"></exception>
        /// <exception cref="PersonalOutOfRangeException"></exception>
        /// <exception cref="BytesOutOfRangeException"></exception>
        public static byte[] HashSaltPersonal(byte[] message, byte[] key, byte[] salt, byte[] personal, int bytes = OutBytes)
        {
            if (message == null)
                throw new ArgumentNullException("message", "Message cannot be null");

            if (salt == null)
                throw new ArgumentNullException("salt", "Salt cannot be null");

            if (personal == null)
                throw new ArgumentNullException("personal", "Personal string cannot be null");

            if (key != null && (key.Length > KeyBytesMax || key.Length < KeyBytesMin))
                throw new KeyOutOfRangeException(string.Format("key must be between {0} and {1} bytes in length.", KeyBytesMin, KeyBytesMax));

            if (key == null)
                key = new byte[0];

            if (salt.Length != SaltBytes)
                throw new SaltOutOfRangeException(string.Format("Salt must be {0} bytes in length.", SaltBytes));

            if (personal.Length != PersonalBytes)
                throw new PersonalOutOfRangeException(string.Format("Personal bytes must be {0} bytes in length.", PersonalBytes));

            //validate output length
            if (bytes > BytesMax || bytes < BytesMin)
                throw new BytesOutOfRangeException("bytes", bytes,
                  string.Format("bytes must be between {0} and {1} bytes in length.", BytesMin, BytesMax));

            var config = new Blake2sConfig
            {
                Key = key,
                OutputSizeInBytes = bytes,
                Personalization = personal,
                Salt = salt
            };

            return ComputeHash(message, 0, message.Length, config);
        }
Пример #16
0
        /// <summary>Hashes a message, with an optional key, using the BLAKE2s primitive.</summary>
        /// <param name="message">The message to be hashed.</param>
        /// <param name="key">The key; may be null, otherwise between 16 and 64 bytes.</param>
        /// <param name="bytes">The size (in bytes) of the desired result.</param>
        /// <returns>Returns a byte array.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="BytesOutOfRangeException"></exception>
        /// <exception cref="OverflowException"></exception>
        public static byte[] Hash(byte[] message, byte[] key, int bytes)
        {
            //validate the length of the key
            if (key != null)
            {
                if (key.Length > KeyBytesMax || key.Length < KeyBytesMin)
                {
                    throw new KeyOutOfRangeException(string.Format("key must be between {0} and {1} bytes in length.",
                        KeyBytesMin, KeyBytesMax));
                }
            }
            else
            {
                key = new byte[0];
            }

            //validate output length
            if (bytes > BytesMax || bytes < BytesMin)
                throw new BytesOutOfRangeException("bytes", bytes,
                    string.Format("bytes must be between {0} and {1} bytes in length.", BytesMin, BytesMax));

            var config = new Blake2sConfig
            {
                Key = key,
                OutputSizeInBytes = bytes
            };

            if (message == null)
            {
                message = new byte[0];
            }

            return ComputeHash(message, 0, message.Length, config);
        }