Back to index

openldap  2.4.31
hash.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 2000-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2000-2003 Kurt D. Zeilenga.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 /* This implements the Fowler / Noll / Vo (FNV-1) hash algorithm.
00018  * A summary of the algorithm can be found at:
00019  *   http://www.isthe.com/chongo/tech/comp/fnv/index.html
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #include <lutil_hash.h>
00025 
00026 /* offset and prime for 32-bit FNV-1 */
00027 #define HASH_OFFSET  0x811c9dc5U
00028 #define HASH_PRIME   16777619
00029 
00030 
00031 /*
00032  * Initialize context
00033  */
00034 void
00035 lutil_HASHInit( struct lutil_HASHContext *ctx )
00036 {
00037        ctx->hash = HASH_OFFSET;
00038 }
00039 
00040 /*
00041  * Update hash
00042  */
00043 void
00044 lutil_HASHUpdate(
00045     struct lutil_HASHContext       *ctx,
00046     const unsigned char            *buf,
00047     ber_len_t        len )
00048 {
00049        const unsigned char *p, *e;
00050        ber_uint_t h;
00051 
00052        p = buf;
00053        e = &buf[len];
00054 
00055        h = ctx->hash;
00056 
00057        while( p < e ) {
00058               h *= HASH_PRIME;
00059               h ^= *p++;
00060        }
00061 
00062        ctx->hash = h;
00063 }
00064 
00065 /*
00066  * Save hash
00067  */
00068 void
00069 lutil_HASHFinal( unsigned char *digest, struct lutil_HASHContext *ctx )
00070 {
00071        ber_uint_t h = ctx->hash;
00072 
00073        digest[0] = h & 0xffU;
00074        digest[1] = (h>>8) & 0xffU;
00075        digest[2] = (h>>16) & 0xffU;
00076        digest[3] = (h>>24) & 0xffU;
00077 }