forked from markfasheh/duperemove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csum-mhash.c
85 lines (66 loc) · 1.68 KB
/
csum-mhash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* csum.c
*
* Copyright (C) 2013 SUSE. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <mhash.h>
#include "csum.h"
static MHASH td;
#define HASH_FUNC MHASH_SHA256
unsigned int digest_len = 0;
void checksum_block(char *buf, int len, unsigned char *digest)
{
td = mhash_init(HASH_FUNC);
if (td == MHASH_FAILED)
abort();
mhash(td, buf, len);
mhash_deinit(td, digest);
}
int init_hash(void)
{
digest_len = mhash_get_block_size(HASH_FUNC);
if (!digest_len)
return 1;
if (digest_len == 0 || digest_len > DIGEST_LEN_MAX)
abort();
return 0;
}
void debug_print_digest(FILE *stream, unsigned char *digest)
{
int i;
for (i = 0; i < digest_len; i++)
fprintf(stream, "%.2x", digest[i]);
}
struct running_checksum {
MHASH td;
unsigned char digest[DIGEST_LEN_MAX];
};
struct running_checksum *start_running_checksum(void)
{
struct running_checksum *c = calloc(1, sizeof(struct running_checksum));
if (c)
c->td = mhash_init(HASH_FUNC);
return c;
}
void add_to_running_checksum(struct running_checksum *c,
unsigned int len, unsigned char *buf)
{
mhash(c->td, buf, len);
}
void finish_running_checksum(struct running_checksum *c, unsigned char *digest)
{
mhash_deinit(c->td, digest);
free(c);
}