Skip to content

Integration with hash extension #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ This extension adds one new PHP function:

It will checksum the string, and return the checksum.

## `hash()` integration

It also registers the `xx32` hash algorithm to be used in the [`hash()`](http://www.php.net/manual/en/ref.hash.php) function family:

```
hash('xx32', $data);

Or:

$h = hash_init('xx32');
hash_update($h, $data);
echo hash_final($h);
```

## License

BSD 2-clause license.
BSD 2-clause license.
41 changes: 41 additions & 0 deletions php_xxhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,56 @@
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_xxhash.h"
#include "ext/hash/php_hash.h"

#include "xxhash.c"

#ifdef COMPILE_DL_XXHASH
ZEND_GET_MODULE(xxhash)
#endif

typedef struct {
void *state;
} PHP_XXH32_CTX;

PHP_HASH_API void PHP_XXH32Init(PHP_XXH32_CTX *context)
{
context->state = XXH32_init(0);
}

PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *context, const unsigned char *input, unsigned int inputLen)
{
XXH32_feed(context->state, (void *)input, (int)inputLen);
}

PHP_HASH_API void PHP_XXH32Final(unsigned char digest[4], PHP_XXH32_CTX *context)
{
unsigned int h32;

h32 = XXH32_result(context->state);

digest[0] = (unsigned char) ((h32 >> 24) & 0xff);
digest[1] = (unsigned char) ((h32 >> 16) & 0xff);
digest[2] = (unsigned char) ((h32 >> 8) & 0xff);
digest[3] = (unsigned char) (h32 & 0xff);

context->state = XXH32_init(0);
}

const php_hash_ops php_hash_xxh32_ops = {
(php_hash_init_func_t) PHP_XXH32Init,
(php_hash_update_func_t) PHP_XXH32Update,
(php_hash_final_func_t) PHP_XXH32Final,
(php_hash_copy_func_t) php_hash_copy,
4,
4,
sizeof(PHP_XXH32_CTX)
};

PHP_MINIT_FUNCTION(xxhash)
{
php_hash_register_algo("xx32", &php_hash_xxh32_ops);

return SUCCESS;
}

Expand Down