- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1k
          Murmur3 should not be cryptographic hash algorithm
          #3668
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
e88a1ea
              4868dc8
              c2fc2e5
              a8855d2
              9c5e956
              dfbfb95
              4894dfc
              24991ed
              8acc8f5
              90dee11
              25df0aa
              8273b9a
              6536483
              ee87f06
              d933edd
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -11,13 +11,15 @@ | |
|  | ||
| using System; | ||
| using System.Buffers.Binary; | ||
| using System.Runtime.CompilerServices; | ||
|  | ||
| namespace Neo.Cryptography | ||
| { | ||
| /// <summary> | ||
| /// Computes the murmur hash for the input data. | ||
| /// <remarks>Murmur32 is a non-cryptographic hash function.</remarks> | ||
| /// </summary> | ||
| public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm | ||
| public sealed class Murmur32 | ||
| { | ||
| private const uint c1 = 0xcc9e2d51; | ||
| private const uint c2 = 0x1b873593; | ||
|  | @@ -26,41 +28,40 @@ public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm | |
| private const uint m = 5; | ||
| private const uint n = 0xe6546b64; | ||
|  | ||
| private readonly uint seed; | ||
| private uint hash; | ||
| private int length; | ||
| private readonly uint _seed; | ||
| private uint _hash; | ||
| private int _length; | ||
|  | ||
| public const int HashSizeInBits = 32; | ||
| public override int HashSize => HashSizeInBits; | ||
|  | ||
| [Obsolete("Use HashSizeInBits")] | ||
| public int HashSize => HashSizeInBits; | ||
|  | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Murmur32"/> class with the specified seed. | ||
| /// </summary> | ||
| /// <param name="seed">The seed to be used.</param> | ||
| public Murmur32(uint seed) | ||
| { | ||
| this.seed = seed; | ||
| HashSizeValue = HashSizeInBits; | ||
| Initialize(); | ||
| } | ||
|  | ||
| protected override void HashCore(byte[] array, int ibStart, int cbSize) | ||
| { | ||
| HashCore(array.AsSpan(ibStart, cbSize)); | ||
| _seed = seed; | ||
| } | ||
|  | ||
| protected override void HashCore(ReadOnlySpan<byte> source) | ||
| /// <summary> | ||
| /// Append data to murmur computation | ||
| /// </summary> | ||
| /// <param name="source">Source</param> | ||
| public void Append(ReadOnlySpan<byte> source) | ||
|          | ||
| { | ||
| length += source.Length; | ||
| _length += source.Length; | ||
| for (; source.Length >= 4; source = source[4..]) | ||
| { | ||
| uint k = BinaryPrimitives.ReadUInt32LittleEndian(source); | ||
| var k = BinaryPrimitives.ReadUInt32LittleEndian(source); | ||
| k *= c1; | ||
| k = Helper.RotateLeft(k, r1); | ||
| k *= c2; | ||
| hash ^= k; | ||
| hash = Helper.RotateLeft(hash, r2); | ||
| hash = hash * m + n; | ||
| _hash ^= k; | ||
| _hash = Helper.RotateLeft(_hash, r2); | ||
| _hash = _hash * m + n; | ||
| } | ||
| if (source.Length > 0) | ||
| { | ||
|  | @@ -74,34 +75,61 @@ protected override void HashCore(ReadOnlySpan<byte> source) | |
| remainingBytes *= c1; | ||
| remainingBytes = Helper.RotateLeft(remainingBytes, r1); | ||
| remainingBytes *= c2; | ||
| hash ^= remainingBytes; | ||
| _hash ^= remainingBytes; | ||
| } | ||
| } | ||
|  | ||
| protected override byte[] HashFinal() | ||
| private uint GetCurrentHashUInt32() | ||
| { | ||
| byte[] buffer = new byte[sizeof(uint)]; | ||
| TryHashFinal(buffer, out _); | ||
| return buffer; | ||
| var state = _hash ^ (uint)_length; | ||
| state ^= state >> 16; | ||
| state *= 0x85ebca6b; | ||
| state ^= state >> 13; | ||
| state *= 0xc2b2ae35; | ||
| state ^= state >> 16; | ||
| return state; | ||
| } | ||
|  | ||
| protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten) | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void Initialize() | ||
|         
                  nan01ab marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| { | ||
| hash ^= (uint)length; | ||
| hash ^= hash >> 16; | ||
| hash *= 0x85ebca6b; | ||
| hash ^= hash >> 13; | ||
| hash *= 0xc2b2ae35; | ||
| hash ^= hash >> 16; | ||
| _hash = _seed; | ||
| _length = 0; | ||
| } | ||
|  | ||
| bytesWritten = Math.Min(destination.Length, sizeof(uint)); | ||
| return BinaryPrimitives.TryWriteUInt32LittleEndian(destination, hash); | ||
| /// <summary> | ||
| /// Computes the murmur hash for the input data and resets the state. | ||
| /// </summary> | ||
| /// <param name="data">The input to compute the hash code for.</param> | ||
| /// <returns>The computed hash code in byte[4].</returns> | ||
| public byte[] ComputeHash(ReadOnlySpan<byte> data) | ||
| { | ||
| var buffer = new byte[HashSizeInBits / 8]; | ||
| BinaryPrimitives.WriteUInt32LittleEndian(buffer, ComputeHashUInt32(data)); | ||
| return buffer; | ||
| } | ||
|  | ||
| public override void Initialize() | ||
| /// <summary> | ||
| /// Computes the murmur hash for the input data and resets the state. | ||
| /// </summary> | ||
| /// <param name="data">The input to compute the hash code for.</param> | ||
| /// <returns>The computed hash code in uint.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public uint ComputeHashUInt32(ReadOnlySpan<byte> data) | ||
| { | ||
| hash = seed; | ||
| length = 0; | ||
| Initialize(); | ||
| Append(data); | ||
| return GetCurrentHashUInt32(); | ||
| } | ||
|  | ||
| /// <summary> | ||
| /// Computes the murmur hash for the input data. | ||
| /// </summary> | ||
| /// <param name="data">The input to compute the hash code for.</param> | ||
| /// <param name="seed">The seed used by the murmur algorithm.</param> | ||
| /// <returns>The computed hash code in uint.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static uint HashToUInt32(ReadOnlySpan<byte> data, uint seed) | ||
| => new Murmur32(seed).ComputeHashUInt32(data); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
NonCryptographicHashAlgorithmas inherit classUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More updates are needed, because
Appendnot supported.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? You can still use
append.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current murmur32 impl in neo doesn’t support multiple Append operations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you can't use it in
NonCryptographicHashAlgorithm?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the current logic does not allow this interface, feel free to create a new PR for this purpose
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well then I don't find this as a fix. Using these interfaces makes the hash a lot faster to process data. I want to see a benchmark of this new implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic is the same, should be faster but never slower, why do you think it will be slower?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the
classinterfaces use an engine for fast hashing for huge amounts of data up to 2GB.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No significant performance difference.