Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions NBitcoin.Bench/HashBench.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BenchmarkDotNet.Attributes;
using NBitcoin.Crypto;
using NBitcoin.DataEncoders;
using System;

namespace NBitcoin.Bench;

Expand All @@ -15,6 +16,12 @@ public void Setup()
bytes = Encoders.Hex.DecodeData("d54994ece1d11b19785c7248868696250ab195605b469632b7bd68130e880c9a");
}

[Benchmark]
public void Hash160()
{
Hashes.Hash160(bytes.AsSpan());
}

[Benchmark]
public void SHA1()
{
Expand Down
15 changes: 12 additions & 3 deletions NBitcoin/Crypto/Hashes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public static uint160 Hash160(byte[] data)
{
return Hash160(data, 0, data.Length);
}

#if HAS_SPAN
public static uint160 Hash160(ReadOnlySpan<byte> data)
{
// TODO: optimize
return Hash160(data.ToArray());
return new uint160(Hash160RawBytes(data));
}
#endif

Expand All @@ -63,8 +63,17 @@ public static uint160 Hash160(byte[] data, int offset, int count)
{
return new uint160(Hash160RawBytes(data, offset, count));
}

public static byte[] Hash160RawBytes(byte[] data)
=> Hash160RawBytes(data, 0, data.Length);
=> Hash160RawBytes(data, 0, data.Length);

#if HAS_SPAN
public static byte[] Hash160RawBytes(ReadOnlySpan<byte> data)
{
return RIPEMD160(SHA256(data));
}
#endif

public static byte[] Hash160RawBytes(byte[] data, int offset, int count)
{
return RIPEMD160(SHA256(data, offset, count));
Expand Down
Loading