openldap/libraries/liblutil/hash.c

78 lines
1.5 KiB
C
Raw Normal View History

/* $OpenLDAP$ */
2003-11-26 07:17:08 +08:00
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
2006-01-04 07:11:52 +08:00
* Copyright 2000-2006 The OpenLDAP Foundation.
2003-11-26 07:17:08 +08:00
* Portions Copyright 2000-2003 Kurt D. Zeilenga.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
2003-05-25 11:31:21 +08:00
*/
2003-11-26 07:17:08 +08:00
/* This implements the Fowler / Noll / Vo (FNV-1) hash algorithm.
* A summary of the algorithm can be found at:
* http://www.isthe.com/chongo/tech/comp/fnv/index.html
*/
#include "portable.h"
#include <lutil_hash.h>
/* offset and prime for 32-bit FNV-1 */
2000-10-18 08:29:21 +08:00
#define HASH_OFFSET 0x811c9dc5U
#define HASH_PRIME 16777619
/*
* Initialize context
*/
void
lutil_HASHInit( struct lutil_HASHContext *ctx )
{
ctx->hash = HASH_OFFSET;
}
/*
* Update hash
*/
void
lutil_HASHUpdate(
struct lutil_HASHContext *ctx,
const unsigned char *buf,
2004-09-27 07:42:16 +08:00
ber_len_t len )
{
const unsigned char *p, *e;
ber_uint_t h;
p = buf;
e = &buf[len];
h = ctx->hash;
while( p < e ) {
h *= HASH_PRIME;
h ^= *p++;
}
ctx->hash = h;
}
/*
* Save hash
*/
void
lutil_HASHFinal( unsigned char *digest, struct lutil_HASHContext *ctx )
{
ber_uint_t h = ctx->hash;
2000-10-18 08:29:21 +08:00
digest[0] = h & 0xffU;
digest[1] = (h>>8) & 0xffU;
digest[2] = (h>>16) & 0xffU;
digest[3] = (h>>24) & 0xffU;
}