示例#1
0
 /// <summary>
 ///   Register the standard hash algorithms.
 /// </summary>
 static MultiHash()
 {
     HashingAlgorithm.Register("sha1", 0x11, 20, SHA1.Create);
     HashingAlgorithm.Register("sha2-256", 0x12, 32, SHA256.Create);
     HashingAlgorithm.Register("sha2-512", 0x13, 64, SHA512.Create);
     //HashingAlgorithm.Register("sha3-512", 0x14, 64, () => { return new SHA3.SHA3Managed(512); });
     //HashingAlgorithm.Register("blake2b", 0x40, 64);
     //HashingAlgorithm.Register("blake2s", 0x41, 32);
 }
示例#2
0
        private void RaiseUnknownHashingAlgorithm(HashingAlgorithm algorithm)
        {
            //"Unknown hashing algorithm number 0x{0:x2}.", algorithm.Code
            var handler = UnknownHashingAlgorithm;

            if (handler != null)
            {
                var args = new UnknownHashingAlgorithmEventArgs {
                    Algorithm = algorithm
                };
                handler(this, args);
            }
        }
示例#3
0
        private void ValidateAlgorithm(byte fnCode, byte size)
        {
            Algorithm = HashingAlgorithm.Codes[fnCode];

            if (Algorithm == null)
            {
                Algorithm = HashingAlgorithm.Register("custom-" + fnCode, fnCode, size);
                RaiseUnknownHashingAlgorithm(Algorithm);
            }
            else if (size != Algorithm.DigestSize)
            {
                throw new InvalidDataException(string.Format("The digest size {0} is wrong for {1}; it should be {2}.", size, Algorithm.Name, Algorithm.DigestSize));
            }
        }
示例#4
0
        /// <summary>
        ///   Creates a new instance of the <see cref="MultiHash"/> class with the
        ///   specified <see cref="HashingAlgorithm">Algorithm name</see> and <see cref="Digest"/> value.
        /// </summary>
        /// <param name="algorithmName">
        ///   A valid IPFS hashing algorithm name, e.g. "sha2-256" or "sha2-512".
        /// </param>
        /// <param name="digest">
        ///    The digest value as a byte array.
        /// </param>
        public MultiHash(string algorithmName, byte[] digest)
        {
            Guard.NotNull(algorithmName, nameof(algorithmName));
            Guard.NotNull(digest, nameof(digest));

            HashingAlgorithm a;

            if (!HashingAlgorithm.Names.TryGetValue(algorithmName, out a))
            {
                throw new ArgumentException(string.Format("The hashing algorithm '{0}' is unknown.", algorithmName));
            }
            Algorithm = a;

            if (Algorithm.DigestSize != digest.Length)
            {
                throw new ArgumentException(string.Format("The digest size for '{0}' is {1} bytes, not {2}.", algorithmName, Algorithm.DigestSize, digest.Length));
            }
            Digest = digest;
        }