/// <summary> /// Register the standard hash algorithms for IPFS. /// </summary> static MultiHash() { HashingAlgorithm.Register("sha1", 0x11, 20, () => { return(new SHA1Managed()); }); HashingAlgorithm.Register("sha2-256", 0x12, 32, () => { return(new SHA256Managed()); }); HashingAlgorithm.Register("sha2-512", 0x13, 64, () => { return(new SHA512Managed()); }); HashingAlgorithm.Register("sha3-512", 0x14, 64, () => { return(new SHA3.SHA3Managed(512)); }); HashingAlgorithm.Register("blake2b", 0x40, 64); HashingAlgorithm.Register("blake2s", 0x41, 32); }
public void Compute_Not_Implemented_Hash_Array() { var alg = HashingAlgorithm.Register("not-implemented", 0x0F, 32); try { var hello = Encoding.UTF8.GetBytes("Hello, world."); ExceptionAssert.Throws <NotImplementedException>(() => MultiHash.ComputeHash(hello, "not-implemented")); } finally { HashingAlgorithm.Deregister(alg); } }
public void Constructor_Should_Throw_On_Hash_Default_Size_Above_159() { HashingAlgorithm.Register("TooLong", 0x9999, 160); var toLongHashingAlgorithm = HashingAlgorithm.All.First(x => x.DigestSize > 159); var longEnoughHashingAlgorithm = HashingAlgorithm.All.First(x => x.DigestSize <= 159); // ReSharper disable once ObjectCreationAsStatement new Action(() => new DevDfs(_fileSystem, new HashProvider(toLongHashingAlgorithm))).Should() .Throw <ArgumentException>() .And.Message.Should().Contain(nameof(HashingAlgorithm)); // ReSharper disable once ObjectCreationAsStatement new Action(() => new DevDfs(_fileSystem, new HashProvider(longEnoughHashingAlgorithm))).Should() .NotThrow <ArgumentException>(); }
void Read(CodedInputStream stream) { byte code = (byte)stream.ReadTag(); byte digestSize = (byte)stream.ReadLength(); Algorithm = HashingAlgorithm.Codes[code]; if (Algorithm == null) { Algorithm = HashingAlgorithm.Register("ipfs-" + code, code, digestSize); RaiseUnknownHashingAlgorithm(Algorithm); } else if (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); }
public HashingBenchmark() { const string name = nameof(NoopHash); HashingAlgorithm.Register(name, 1234123421, NoopHash.DigestSize, () => new NoopHash()); var bytes = ByteString.CopyFrom(Enumerable.Range(1, 32).Select(i => (byte)i).ToArray()); var amount = ByteString.CopyFrom(343434.ToByteArray()); _entry = new PublicEntry { Amount = amount, GasPrice = amount, ReceiverAddress = bytes, SenderAddress = bytes }; _provider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata(name)); }
void Read(CodedInputStream stream) { var code = stream.ReadInt32(); var digestSize = stream.ReadLength(); HashingAlgorithm.Codes.TryGetValue(code, out HashingAlgorithm 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); }
public void HashingAlgorithm_Name_Already_Exists() { Assert.Throws <ArgumentException>(() => HashingAlgorithm.Register("sha1", 0x11, 1)); }
public void HashingAlgorithm_Bad_Name() { Assert.Throws <ArgumentNullException>(() => HashingAlgorithm.Register(null, 1, 1)); Assert.Throws <ArgumentNullException>(() => HashingAlgorithm.Register("", 1, 1)); Assert.Throws <ArgumentNullException>(() => HashingAlgorithm.Register(" ", 1, 1)); }