]> granicus.if.org Git - php/commitdiff
Added Jenkins's one-at-a-time hash support to ext/hash.
authorMartin Jansen <mj@php.net>
Wed, 24 Mar 2010 15:47:40 +0000 (15:47 +0000)
committerMartin Jansen <mj@php.net>
Wed, 24 Mar 2010 15:47:40 +0000 (15:47 +0000)
NEWS
ext/hash/config.m4
ext/hash/hash.c
ext/hash/hash_joaat.c [new file with mode: 0644]
ext/hash/php_hash.h
ext/hash/php_hash_joaat.h [new file with mode: 0644]
ext/hash/tests/hash_algos.phpt
ext/hash/tests/joaat.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 7bf8c646c9f8e504e8cf0e17ddf7ce95c5ba9cd6..d8d939e446411aa74242b0dd9be20f79ad65dbaa 100644 (file)
--- 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)
        
index 61e9394f6c562e41bcc9b16e38f4b6857b0a3dde..ca4f0bcebd851b6290b28fa72b81928794890d39 100644 (file)
@@ -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], [
index 8a81faf4adce6bb609c65a71db600d0f446394b0..eb1094926fa67f7d7489f5de40f46d9289a5ab48 100644 (file)
@@ -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 (file)
index 0000000..7603765
--- /dev/null
@@ -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 <mj@php.net>                                   |
+  +----------------------------------------------------------------------+
+*/
+
+/* $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
+ */
index af88159586f2f719f41cad50303dca1d1a60e67e..4731dcaf1ca956b932f4ce8d50ffad9e364241b5 100644 (file)
@@ -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 (file)
index 0000000..4f1dccc
--- /dev/null
@@ -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 <mj@php.net>                                   | 
+  +----------------------------------------------------------------------+
+*/
+
+/* $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
+ */
index 9818dfc958c40168c42213e81b97750f86773290..f497dfe6e7e4ff64db13743dab85f7fe70e2961c 100644 (file)
@@ -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 (file)
index 0000000..fa310a9
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Jenkins's one-at-a-time
+--SKIPIF--
+<?php extension_loaded('hash') or die ('Skip - hash extension not available'); ?>
+--FILE--
+<?php
+$tests = array(
+       array("hello world", "3e4a5a57"),
+       array("", 0),
+       array("", "000000"),
+       array("a", "ca2e9442"),
+       array("aa", "7081738e"),
+);
+
+$i = 0;
+$pass = true;
+
+foreach ($tests as $test) {
+       ++$i;
+       
+       $result = hash("joaat", $test[0]);
+       if ($result != $test[1]) {
+               echo "Iteration " . $i . " failed - expected '" . $test[1] . "', got '" . $result . "' for '" . $test[1] . "'\n";
+
+               $pass = false;
+       }
+}
+
+if($pass) {
+    echo "PASS";
+}
+?>
+--EXPECT--
+PASS