From 3842a993fee5324dd9bbcc879aaa880c8a43624e Mon Sep 17 00:00:00 2001 From: Martin Jansen Date: Wed, 24 Mar 2010 15:47:40 +0000 Subject: [PATCH] Added Jenkins's one-at-a-time hash support to ext/hash. --- NEWS | 1 + ext/hash/config.m4 | 4 +- ext/hash/hash.c | 2 + ext/hash/hash_joaat.c | 100 +++++++++++++++++++++++++++++++++ ext/hash/php_hash.h | 1 + ext/hash/php_hash_joaat.h | 43 ++++++++++++++ ext/hash/tests/hash_algos.phpt | 36 +++++++----- ext/hash/tests/joaat.phpt | 34 +++++++++++ 8 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 ext/hash/hash_joaat.c create mode 100644 ext/hash/php_hash_joaat.h create mode 100644 ext/hash/tests/joaat.phpt diff --git a/NEWS b/NEWS index 7bf8c646c9..d8d939e446 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 201?, PHP 5.3.99 +- Added Jenkins's one-at-a-time hash support to ext/hash. (Martin Jansen) - Added FNV-1 hash support to ext/hash. (Michael Maclean) - default_charset if not specified is now UTF-8 instead of ISO-8859-1. (Rasmus) diff --git a/ext/hash/config.m4 b/ext/hash/config.m4 index 61e9394f6c..ca4f0bcebd 100644 --- a/ext/hash/config.m4 +++ b/ext/hash/config.m4 @@ -27,11 +27,11 @@ if test "$PHP_HASH" != "no"; then EXT_HASH_SOURCES="hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c \ hash_tiger.c hash_gost.c hash_snefru.c hash_whirlpool.c hash_adler32.c \ - hash_crc32.c hash_salsa.c hash_fnv.c" + hash_crc32.c hash_salsa.c hash_fnv.c hash_joaat.c" EXT_HASH_HEADERS="php_hash.h php_hash_md.h php_hash_sha.h php_hash_ripemd.h \ php_hash_haval.h php_hash_tiger.h php_hash_gost.h php_hash_snefru.h \ php_hash_whirlpool.h php_hash_adler32.h php_hash_crc32.h php_hash_salsa.h \ - php_hash_fnv.h php_hash_types.h" + php_hash_fnv.h php_hash_joaat.h php_hash_types.h" PHP_NEW_EXTENSION(hash, $EXT_HASH_SOURCES, $ext_shared) ifdef([PHP_INSTALL_HEADERS], [ diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 8a81faf4ad..eb1094926f 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -79,6 +79,7 @@ static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = { {"FNV1A32", "fnv1a32", 30}, {"FNV164", "fnv164", 31}, {"FNV1A64", "fnv1a64", 32}, + {"JOAAT", "joaat", 33}, }; #endif @@ -847,6 +848,7 @@ PHP_MINIT_FUNCTION(hash) php_hash_register_algo("salsa20", &php_hash_salsa20_ops); php_hash_register_algo("fnv132", &php_hash_fnv132_ops); php_hash_register_algo("fnv164", &php_hash_fnv164_ops); + php_hash_register_algo("joaat", &php_hash_joaat_ops); PHP_HASH_HAVAL_REGISTER(3,128); PHP_HASH_HAVAL_REGISTER(3,160); diff --git a/ext/hash/hash_joaat.c b/ext/hash/hash_joaat.c new file mode 100644 index 0000000000..7603765b97 --- /dev/null +++ b/ext/hash/hash_joaat.c @@ -0,0 +1,100 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Martin Jansen | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +/* Implements Jenkins's one-at-a-time hashing algorithm as presented on + * http://www.burtleburtle.net/bob/hash/doobs.html. + */ + +#include "php_hash.h" +#include "php_hash_joaat.h" + +const php_hash_ops php_hash_joaat_ops = { + (php_hash_init_func_t) PHP_JOAATInit, + (php_hash_update_func_t) PHP_JOAATUpdate, + (php_hash_final_func_t) PHP_JOAATFinal, + (php_hash_copy_func_t) php_hash_copy, + 4, + 4, + sizeof(PHP_JOAAT_CTX) +}; + +PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context) +{ + context->state = 0; +} + +PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, unsigned int inputLen) +{ + context->state = joaat_buf((void *)input, inputLen); +} + +PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[4], PHP_JOAAT_CTX * context) +{ +#ifdef WORDS_BIGENDIAN + memcpy(digest, &context->state, 4); +#else + int i = 0; + unsigned char *c = (unsigned char *) &context->state; + + for (i = 0; i < 4; i++) { + digest[i] = c[3 - i]; + } +#endif + context->state = 0; +} + +/* + * joaat_buf - perform a Jenkins's one-at-a-time hash on a buffer + * + * input: + * buf - start of buffer to hash + * len - length of buffer in octets + * + * returns: + * 32 bit hash as a static hash type + */ +static php_hash_uint32 +joaat_buf(void *buf, size_t len) +{ + php_hash_uint32 hval; + size_t i; + unsigned char *input = (unsigned char *)buf; + + for (i = 0; i < len; i++) { + hval += input[i]; + hval += (hval << 10); + hval ^= (hval >> 6); + } + + hval += (hval << 3); + hval ^= (hval >> 11); + hval += (hval << 15); + + return hval; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/hash/php_hash.h b/ext/hash/php_hash.h index af88159586..4731dcaf1c 100644 --- a/ext/hash/php_hash.h +++ b/ext/hash/php_hash.h @@ -82,6 +82,7 @@ extern const php_hash_ops php_hash_salsa10_ops; extern const php_hash_ops php_hash_salsa20_ops; extern const php_hash_ops php_hash_fnv132_ops; extern const php_hash_ops php_hash_fnv164_ops; +extern const php_hash_ops php_hash_joaat_ops; #define PHP_HASH_HAVAL_OPS(p,b) extern const php_hash_ops php_hash_##p##haval##b##_ops; diff --git a/ext/hash/php_hash_joaat.h b/ext/hash/php_hash_joaat.h new file mode 100644 index 0000000000..4f1dccc2d8 --- /dev/null +++ b/ext/hash/php_hash_joaat.h @@ -0,0 +1,43 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2010 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Martin Jansen | + +----------------------------------------------------------------------+ +*/ + +/* $Id*/ + +#ifndef PHP_HASH_JOAAT_H +#define PHP_HASH_JOAAT_H + +typedef struct { + php_hash_uint32 state; +} PHP_JOAAT_CTX; + +PHP_HASH_API void PHP_JOAATInit(PHP_JOAAT_CTX *context); +PHP_HASH_API void PHP_JOAATUpdate(PHP_JOAAT_CTX *context, const unsigned char *input, unsigned int inputLen); +PHP_HASH_API void PHP_JOAATFinal(unsigned char digest[16], PHP_JOAAT_CTX * context); + +static php_hash_uint32 joaat_buf(void *buf, size_t len); + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --git a/ext/hash/tests/hash_algos.phpt b/ext/hash/tests/hash_algos.phpt index 9818dfc958..f497dfe6e7 100644 --- a/ext/hash/tests/hash_algos.phpt +++ b/ext/hash/tests/hash_algos.phpt @@ -18,7 +18,7 @@ var_dump(hash_algos()); ===Done=== --EXPECTF-- *** Testing hash_algos() : basic functionality *** -array(42) { +array(45) { [0]=> string(3) "md2" [1]=> @@ -74,34 +74,40 @@ array(42) { [26]=> string(7) "salsa20" [27]=> - string(10) "haval128,3" + string(6) "fnv132" [28]=> - string(10) "haval160,3" + string(6) "fnv164" [29]=> - string(10) "haval192,3" + string(5) "joaat" [30]=> - string(10) "haval224,3" + string(10) "haval128,3" [31]=> - string(10) "haval256,3" + string(10) "haval160,3" [32]=> - string(10) "haval128,4" + string(10) "haval192,3" [33]=> - string(10) "haval160,4" + string(10) "haval224,3" [34]=> - string(10) "haval192,4" + string(10) "haval256,3" [35]=> - string(10) "haval224,4" + string(10) "haval128,4" [36]=> - string(10) "haval256,4" + string(10) "haval160,4" [37]=> - string(10) "haval128,5" + string(10) "haval192,4" [38]=> - string(10) "haval160,5" + string(10) "haval224,4" [39]=> - string(10) "haval192,5" + string(10) "haval256,4" [40]=> - string(10) "haval224,5" + string(10) "haval128,5" [41]=> + string(10) "haval160,5" + [42]=> + string(10) "haval192,5" + [43]=> + string(10) "haval224,5" + [44]=> string(10) "haval256,5" } ===Done=== \ No newline at end of file diff --git a/ext/hash/tests/joaat.phpt b/ext/hash/tests/joaat.phpt new file mode 100644 index 0000000000..fa310a96e2 --- /dev/null +++ b/ext/hash/tests/joaat.phpt @@ -0,0 +1,34 @@ +--TEST-- +Jenkins's one-at-a-time +--SKIPIF-- + +--FILE-- + +--EXPECT-- +PASS -- 2.40.0