3

I'm using xxHash for C# to hash a value for consistency. ComputeHash returns a byte[], but I need to store the results in a long.

I'm able to convert the results into an int32 using the BitConverter. Here is what I've tried:

var xxHash = new System.Data.HashFunction.xxHash();
byte[] hashedValue = xxHash.ComputeHash(Encoding.UTF8.GetBytes(valueItem));
long value = BitConverter.ToInt64(hashedValue, 0);

When I use int this works fine, but when I change to ToInt64 it fails.

Here's the exception I get:

Destination array is not long enough to copy all the items in the collection. Check array index and length.

ohaal
  • 5,208
  • 2
  • 34
  • 53
Desmond Nzuza
  • 130
  • 1
  • 11

3 Answers3

8

When you construct your xxHash object, you need to supply a hashsize:

var hasher = new xxHash(32);

valid hash sizes are 32 and 64.

See https://github.com/brandondahler/Data.HashFunction/blob/master/src/xxHash/xxHash.cs for the source.

spender
  • 117,338
  • 33
  • 229
  • 351
  • 1
    @leppie: Yep, it's almost worth writing `var hasher = new xxHash((int)32);` so a casual inspection of the call-site is unambiguous. – spender Jun 18 '15 at 09:57
  • Thanks for the answer spender. I specified the hash size and that sorted the issue out for me. I did not want to have to check the resulting byte[] this does it for me. Thanks. – Desmond Nzuza Jun 18 '15 at 11:37
  • 1
    @DesmondNzuza Don't forget to check this answer then ;) – Binkan Salaryman Jun 18 '15 at 11:38
2

Adding a new answer because current implementation of xxHash from Brandon Dahler uses a hashing factory where you initialize the factory with a configuration containing hashsize and seed:

using System.Data.HashFunction.xxHash;

//can also set seed here, (ulong) Seed=234567
xxHashConfig config = new xxHashConfig() { HashSizeInBits = 64 };
var factory = xxHashFactory.Instance.Create(config);
byte[] hashedValue = factory.ComputeHash(Encoding.UTF8.GetBytes(valueItem)).Hash;
Kent Kostelac
  • 2,257
  • 3
  • 29
  • 42
0

BitConverter.ToInt64 expects hashedValue to have 8 bytes (= 64bits). You could manually extend, and then pass it.

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
  • I know the reason why it was failing was becuase the length is too short. I don't have the luxury to inspect or expand the resulting byte[] becuase I don't want to affect performance. Specifying a hash size did the trick for me. Thanks. – Desmond Nzuza Jun 18 '15 at 11:35