Example #1
0
        private void RaiseUnknownHashingAlgorithm(HashingAlgorithm algorithm)
        {
            Logger.Error("Unknown hashing algorithm number 0x{0:x2}.", algorithm.Code);
            var handler = UnknownHashingAlgorithm;

            if (handler != null)
            {
                var args = new UnknownHashingAlgorithmEventArgs {
                    Algorithm = algorithm
                };
                handler(this, args);
            }
        }
Example #2
0
        private void Read(CodedInputStream stream)
        {
            var code       = stream.ReadInt32();
            var digestSize = stream.ReadLength();

            HashingAlgorithm.Codes.TryGetValue(code, out var a);
            Algorithm = a;
            if (Algorithm == null)
            {
                Algorithm = HashingAlgorithm.Register("ipfs-" + code, code, digestSize);
                RaiseUnknownHashingAlgorithm(Algorithm);
            }
            else if (Algorithm.DigestSize != 0 && digestSize != Algorithm.DigestSize)
            {
                throw new InvalidDataException(string.Format("The digest size {0} is wrong for {1}; it should be {2}.",
                                                             digestSize, Algorithm.Name, Algorithm.DigestSize));
            }

            Digest = stream.ReadSomeBytes(digestSize);
        }
        /// <summary>
        ///     Register a new IPFS hashing algorithm.
        /// </summary>
        /// <param name="name">
        ///     The name of the algorithm.
        /// </param>
        /// <param name="code">
        ///     The IPFS number assigned to the hashing algorithm.
        /// </param>
        /// <param name="digestSize">
        ///     The size, in bytes, of the digest value.
        /// </param>
        /// <param name="hasher">
        ///     A <c>Func</c> that returns a <see cref="HashAlgorithm" />.  If not specified, then a <c>Func</c> is created to
        ///     throw a <see cref="NotImplementedException" />.
        /// </param>
        /// <returns>
        ///     A new <see cref="HashingAlgorithm" />.
        /// </returns>
        public static HashingAlgorithm Register(string name, int code, int digestSize,
                                                Func <HashAlgorithm> hasher = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }
            if (Names.ContainsKey(name))
            {
                throw new ArgumentException(string.Format("The IPFS hashing algorithm '{0}' is already defined.",
                                                          name));
            }
            if (Codes.ContainsKey(code))
            {
                throw new ArgumentException(
                          string.Format("The IPFS hashing algorithm code 0x{0:x2} is already defined.", code));
            }
            if (hasher == null)
            {
                hasher = () =>
                {
                    throw new NotImplementedException(
                              string.Format("The IPFS hashing algorithm '{0}' is not implemented.", name));
                }
            }
            ;

            var a = new HashingAlgorithm
            {
                Name       = name,
                Code       = code,
                DigestSize = digestSize,
                Hasher     = hasher
            };

            Names[name] = a;
            Codes[code] = a;

            return(a);
        }
 /// <summary>
 ///     Remove an IPFS hashing algorithm from the registry.
 /// </summary>
 /// <param name="algorithm">
 ///     The <see cref="HashingAlgorithm" /> to remove.
 /// </param>
 public static void Deregister(HashingAlgorithm algorithm)
 {
     Names.Remove(algorithm.Name);
     Codes.Remove(algorithm.Code);
 }