2 * hash - one-way hash routines
4 * Copyright (C) 1999-2007 Landon Curt Noll
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.2 $
21 * @(#) $Id: hash.h,v 30.2 2007/07/05 17:37:41 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/hash.h,v $
24 * Under source code control: 1995/11/14 23:57:45
25 * File existed as early as: 1995
27 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
28 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
32 #if !defined(__HASH_H__)
36 #if defined(CALC_SRC) /* if we are building from the calc source tree */
40 # include <calc/sha1.h>
41 # include <calc/zmath.h>
45 /* MAX_CHUNKSIZE is the largest chunksize of any hash */
46 #define MAX_CHUNKSIZE (SHA1_CHUNKSIZE)
48 /* max size of debugging strings in xyz_print() functions */
49 #define DEBUG_SIZE 127
53 * hashstate - state of a hash system
55 * Hashing some types of values requires a checkpoint (chkpt function call)
56 * to be performed, which pads buffered data with 0's and performs an
57 * update. The checkpoint thus causes the value to start on a new hash
58 * block boundary with no buffered data.
60 * Some data types (strings, BLOCKs and OCTETs) do not require a
61 * checkpoint as long as the previous value hashed was a string,
64 typedef struct hashstate HASH
;
66 int hashtype
; /* XYZ_HASH_TYPE debug value */
67 BOOL bytes
; /* TRUE => reading bytes rather than words */
68 void (*update
)(HASH
*, USB8
*, USB32
); /* update arbitrary length */
69 void (*chkpt
)(HASH
*); /* checkpoint a state */
70 void (*note
)(int, HASH
*); /* note a special value */
71 void (*type
)(int, HASH
*); /* note a VALUE type */
72 ZVALUE (*final
)(HASH
*); /* complete hash state */
73 int (*cmp
)(HASH
*,HASH
*); /* compare to states, TRUE => a!=b */
74 void (*print
)(HASH
*); /* print the value of a hash */
75 int base
; /* XYZ_BASE special hash value */
76 int chunksize
; /* XYZ_CHUNKSIZE input chunk size */
77 int unionsize
; /* h_union element size */
78 union { /* hash dependent states */
79 USB8 data
[1]; /* used by hash_value to hash below */
80 SHA1_INFO h_sha1
; /* new SHA-1 internal state */
86 * what to xor to digest value when hashing special values
88 * IMPORTANT: To avoid overlap due to the HASH_XYZ macros below, the
89 * XYZ_BASE values should be unique random hex values
90 * that end in 00 (i.e., 0 mod 256).
92 #define SHA_BASE 0x12face00 /* old SHA / SHA - no longer used */
93 #define SHA1_BASE 0x23cafe00 /* new SHA-1 / SHA-1 */
94 #define MD5_BASE 0x34feed00 /* MD5 - no longer used */
98 * XYZ_HASH_TYPE - hash types
100 * we support these hash types
102 #define SHA_HASH_TYPE 1 /* no longer used */
103 #define SHA1_HASH_TYPE 2
104 #define MD5_HASH_TYPE 3 /* no longer used */
108 * Note a special value given the base value
110 #define HASH_NEG(base) (1+base) /* note a negative value */
111 #define HASH_COMPLEX(base) (2+base) /* note a complex value */
112 #define HASH_DIV(base) (4+base) /* note a division by a value */
113 #define HASH_ZERO(base) (8+base) /* note a zero numeric value */
114 #define HASH_ZVALUE(base) (16+base) /* note a ZVALUE */
120 E_FUNC HASH
* hash_init(int, HASH
*);
121 E_FUNC
void hash_free(HASH
*);
122 E_FUNC HASH
* hash_copy(HASH
*);
123 E_FUNC
int hash_cmp(HASH
*, HASH
*);
124 E_FUNC
void hash_print(HASH
*);
125 E_FUNC ZVALUE
hash_final(HASH
*);
126 E_FUNC HASH
* hash_long(int, long, HASH
*);
127 E_FUNC HASH
* hash_zvalue(int, ZVALUE
, HASH
*);
128 E_FUNC HASH
* hash_number(int, void*, HASH
*);
129 E_FUNC HASH
* hash_complex(int, void*, HASH
*);
130 E_FUNC HASH
* hash_str(int, char*, HASH
*);
131 E_FUNC HASH
* hash_usb8(int, USB8
*, int, HASH
*);
132 E_FUNC HASH
* hash_value(int, void*, HASH
*);
135 #endif /* !__HASH_H__ */