2 * General routines for handling the various checksum supported.
4 * Written by Preben 'Peppe' Guldberg, December MMIV
8 Copyright (C) 2004 Kern Sibbald
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /* return 0 on success, otherwise some handler specific error code. */
31 int chksum_init(CHKSUM *chksum, int flags)
35 chksum->type = CHKSUM_NONE;
36 bstrncpy(chksum->name, "NONE", sizeof(chksum->name));
37 chksum->updated = false;
38 if (flags & CHKSUM_MD5) {
40 MD5Init(&chksum->context.md5);
41 chksum->type = CHKSUM_MD5;
42 bstrncpy(chksum->name, "MD5", sizeof(chksum->name));
43 } else if (flags * CHKSUM_SHA1) {
45 status = SHA1Init(&chksum->context.sha1);
47 chksum->type = CHKSUM_SHA1;
48 bstrncpy(chksum->name, "SHA1", sizeof(chksum->name));
54 /* return 0 on success, otherwise some handler specific error code. */
55 int chksum_update(CHKSUM *chksum, void *buf, unsigned len)
58 switch (chksum->type) {
62 MD5Update(&chksum->context.md5, (unsigned char *)buf, len);
63 chksum->updated = true;
66 status = SHA1Update(&chksum->context.sha1, (uint8_t *)buf, len);
68 chksum->updated = true;
76 /* return 0 on success, otherwise some handler specific error code. */
77 int chksum_final(CHKSUM *chksum)
79 switch (chksum->type) {
83 MD5Final(chksum->signature, &chksum->context.md5);
86 return SHA1Final(&chksum->context.sha1, chksum->signature);