2 * General routines for handling the various checksum supported.
4 * Written by Preben 'Peppe' Guldberg, December MMIV
7 Copyright (C) 2004-2005 Kern Sibbald
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 version 2 as amended with additional clauses defined in the
12 file LICENSE in the main source directory.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 the file LICENSE for additional details.
24 /* return 0 on success, otherwise some handler specific error code. */
25 int chksum_init(CHKSUM *chksum, int flags)
29 chksum->type = CHKSUM_NONE;
30 bstrncpy(chksum->name, "NONE", sizeof(chksum->name));
31 chksum->updated = false;
32 if (flags & CHKSUM_MD5) {
34 MD5Init(&chksum->context.md5);
35 chksum->type = CHKSUM_MD5;
36 bstrncpy(chksum->name, "MD5", sizeof(chksum->name));
37 } else if (flags & CHKSUM_SHA1) {
39 status = SHA1Init(&chksum->context.sha1);
41 chksum->type = CHKSUM_SHA1;
42 bstrncpy(chksum->name, "SHA1", sizeof(chksum->name));
48 /* return 0 on success, otherwise some handler specific error code. */
49 int chksum_update(CHKSUM *chksum, void *buf, unsigned len)
52 switch (chksum->type) {
56 MD5Update(&chksum->context.md5, (unsigned char *)buf, len);
57 chksum->updated = true;
60 status = SHA1Update(&chksum->context.sha1, (uint8_t *)buf, len);
62 chksum->updated = true;
70 /* return 0 on success, otherwise some handler specific error code. */
71 int chksum_final(CHKSUM *chksum)
73 switch (chksum->type) {
77 MD5Final(chksum->signature, &chksum->context.md5);
80 return SHA1Final(&chksum->context.sha1, chksum->signature);