Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add crc256 method

  • Loading branch information...
commit 6fe1c75e28a6efb03ef2ae3d3bdacd87f67e0ad4 1 parent 1089505
@nashby authored
View
1  README.md
@@ -23,6 +23,7 @@ CityHash.hash128(text) # => 124124989950401219618153994964897029896
CityHash.hash128(text, seed1) # => 101668641288246442316643001405184598611
CityHash.hash128crc(text) # => 124124989950401219618153994964897029896
CityHash.hash128crc(text, seed1) # => 101668641288246442316643001405184598611
+CityHash.hash256crc(text) # => 11964743055457135893972873789222488394617411264226841264756
```
### Contributing to cityhash
View
9 ext/cityhash/cityhash.cc
@@ -55,6 +55,13 @@ extern "C" VALUE cityhash_hashcrc128_with_seed(VALUE mod, VALUE input, VALUE see
uint128 hash = CityHashCrc128WithSeed(StringValuePtr(input), RSTRING_LEN(input), seed);
return rb_str_new((char *)&hash, sizeof(hash));
}
+
+extern "C" VALUE cityhash_hashcrc256(VALUE mod, VALUE input)
+{
+ uint64 hash[3] = {};
+ CityHashCrc256(StringValuePtr(input), RSTRING_LEN(input), hash);
+ return rb_str_new((char *)&hash, sizeof(hash));
+}
#endif
extern "C" void Init_cityhash()
@@ -74,5 +81,7 @@ extern "C" void Init_cityhash()
#ifdef __SSE4_2__
rb_define_singleton_method(mInternal, "hash128crc", (ruby_method*) &cityhash_hashcrc128, 1);
rb_define_singleton_method(mInternal, "hash128crc_with_seed", (ruby_method*) &cityhash_hashcrc128_with_seed, 2);
+
+ rb_define_singleton_method(mInternal, "hash256crc", (ruby_method*) &cityhash_hashcrc256, 1);
#endif
}
View
6 lib/cityhash.rb
@@ -35,6 +35,12 @@ def self.hash128crc(input, seed=nil)
unpacked_digest(digest)
end
+ def self.hash256crc(input)
+ digest = Internal.hash256crc(input)
+
+ [0..7, 8..15, 16..23].map { |r| digest[r].unpack('Q').first.to_s }.join.to_i
+ end
+
private
def self.packed_seed(seed)
View
4 test/cityhash_test.rb
@@ -34,4 +34,8 @@
seed = (123 << 64) | 123
assert_equal 1834994000056895780313918994795281207519, CityHash.hash128crc("test", seed)
end
+
+ it "returns 256bit crc hash" do
+ assert_equal 11964743055457135893972873789222488394617411264226841264756, CityHash.hash256crc("test")
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.