diff --git a/binding.cc b/binding.cc index 281d366..026aa3c 100644 --- a/binding.cc +++ b/binding.cc @@ -182,6 +182,18 @@ objectify_hash(const String::AsciiValue &hash) { } } +Local +objectify_hashs(const uint64 hashs[], size_t size) { + Local ret = Array::New(size); + + for(int i=0; iSet(i, objectify_hash(hash)); + } + + return ret; +} + Handle node_Stringify(const Arguments& args) { HandleScope scope; @@ -292,6 +304,25 @@ node_CityHashCrc128(const Arguments& args) { return scope.Close(objectify_hash(hash)); } +Handle +node_CityHashCrc256(const Arguments& args) { + HandleScope scope; + + printf("handle"); + int args_len = args.Length(); + if(args_len != 1) { + return ThrowException(String::New("Invalid arguments.")); + } + + String::Utf8Value data(args[0]->ToString()); + const char* str = *data; + size_t len = data.length(); + + uint64 hashs[4]; + CityHashCrc256(str, len, hashs); + return scope.Close(objectify_hashs(hashs, 4)); +} + extern "C" void init (Handle target) { HandleScope scope; @@ -300,4 +331,5 @@ init (Handle target) { target->Set(String::New("hash64"), FunctionTemplate::New(node_CityHash64)->GetFunction()); target->Set(String::New("hash128"), FunctionTemplate::New(node_CityHash128)->GetFunction()); target->Set(String::New("crc128"), FunctionTemplate::New(node_CityHashCrc128)->GetFunction()); + target->Set(String::New("crc256"), FunctionTemplate::New(node_CityHashCrc256)->GetFunction()); } diff --git a/test-sample/main.cc b/test-sample/main.cc index 592d871..72fba68 100644 --- a/test-sample/main.cc +++ b/test-sample/main.cc @@ -30,6 +30,13 @@ int main (int argc, const char * argv[]) hash = CityHashCrc128WithSeed("Hello", strlen("Hello"), seed); std::cout << "CityHashCrc128WithSeed(\"Hello\") : " << hash.first << "," << hash.second << std::endl; + char *longString = "CityHash provides hash functions for strings. The latest stable version is cityhash-1.0.1.tar.gz. The functions mix the input bits thoroughly but are not suitable for cryptography. We provide reference implementations in C++, with a friendly MIT license. To download the code with svn use these instructions. The README contains a good explanation of the various CityHash functions. However, here is a short summary: CityHash64() and similar return a 64-bit hash. Inside Google, where CityHash was developed starting in 2010, we use variants of CityHash64() mainly in hash tables such as hash_map. CityHash128() and similar return a 128-bit hash and are tuned for strings of at least a few hundred bytes. Depending on your compiler and hardware, it may be faster than CityHash64() on sufficiently long strings. It is known to be slower than necessary on shorter strings, but we expect that case to be relatively unimportant. Inside Google we use variants of CityHash128() mainly for code that wants to minimize collisions. CityHashCrc128() and CityHashCrc256() and similar are additional variants, specially tuned for CPUs with SSE4.2."; + hash = CityHashCrc128(longString, strlen(longString)); + std::cout << "CityHashCrc128(longString) : " << hash.first << "," << hash.second << std::endl; + + uint64 crc256_results[4]; + CityHashCrc256("Hello", strlen("Hello"), crc256_results); + return 0; } diff --git a/test.js b/test.js index d4fb688..e86b179 100644 --- a/test.js +++ b/test.js @@ -219,4 +219,8 @@ assertEqual({ "uint128": true }, cityhash.crc128('Hello', cityhash.objectify('12343,30293')), 'Crc128 for "Hello" with objectify seed 12343,30293'); +// since my computer does not support SSE4.2, can not calculate the CRC by CityHashCrc256 algorithm. +// assertEqual({ +// }, cityhash.crc256('Hello'), 'Crc256 for "Hello"'); + end(); \ No newline at end of file