summaryrefslogtreecommitdiff
path: root/milkway/mw-checksum.c
blob: c3526e2d5f9b5945286dedff35b796a070cae598 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Milkway

 * Copyright (C) 2009- Luo Jinghua <sunmoon1997@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "milkwayint.h"

#include "md5.h"
#include "sha1.h"

#include <stdlib.h>
#include <string.h>

struct mw_checksum {
    enum mw_checksum_mode mode;
    union {
	struct MD5Context md5;
	SHA_CTX sha1;
    } ctx;
    mw_bool_t finalized;
    mw_uint8_t digest[32];
};

mw_checksum_t*
mw_checksum_new(enum mw_checksum_mode mode)
{
    mw_checksum_t* self = malloc(sizeof(struct mw_checksum));
    if (!self)
	return NULL;

    memset (self, 0, sizeof (*self));
    if (mw_checksum_reset(self, mode)) {
	free (self);
	return NULL;
    }
    return self;
}

void
mw_checksum_destroy(mw_checksum_t *self)
{
    free (self);
}

int
mw_checksum_update(mw_checksum_t* self, const void *data, size_t length)
{
    switch (self->mode) {
    case MW_CHECKSUM_MD5:
	MD5Update(&self->ctx.md5, data, length);
	break;
    case MW_CHECKSUM_SHA1:
	SHA1Update(&self->ctx.sha1, data, length);
	break;
    default:
	return MW_INVALID;
    }
    return MW_SUCCESS;
}

int
mw_checksum_final(mw_checksum_t* self)
{
    if (self->finalized)
	return MW_INVALID;

    switch (self->mode) {
    case MW_CHECKSUM_MD5:
	MD5Final(&self->digest[0], &self->ctx.md5);
	break;
    case MW_CHECKSUM_SHA1:
	SHA1Final(&self->digest[0], &self->ctx.sha1);
	break;
    default:
	return MW_INVALID;
    }

    self->finalized = MW_TRUE;
    return MW_SUCCESS;
}

int
mw_checksum_get_digest(mw_checksum_t* self,
		       void *digest,
		       size_t len)
{
    size_t digestlen = mw_checksum_get_digest_length(self);

    if (digestlen > len)
	return MW_TOO_SMALL;

    mw_checksum_final(self);
    memcpy(digest, self->digest, digestlen);
    return digestlen;
}

int
mw_checksum_get_hex_digest(mw_checksum_t* self,
			   char *digest,
			   size_t len)
{
    size_t digestlen = mw_checksum_get_digest_length(self);

    if (digestlen * 2 + 1 > len)
	return MW_TOO_SMALL;

    mw_checksum_final(self);
    mw_hexlify(self->digest, digestlen, digest);
    return digestlen * 2 + 1;
}

int
mw_checksum_get_digest_length(mw_checksum_t* self)
{
    switch (self->mode) {
    case MW_CHECKSUM_MD5:
	return 16;
    case MW_CHECKSUM_SHA1:
	return 20;
    default:
	break;
    }
    return -1;
}

int
mw_checksum_reset(mw_checksum_t* self,
		  enum mw_checksum_mode mode)
{
    switch (mode) {
    case MW_CHECKSUM_MD5:
	MD5Init(&self->ctx.md5);
	break;
    case MW_CHECKSUM_SHA1:
	SHA1Init(&self->ctx.sha1);
	break;
    default:
	return MW_INVALID;
    }

    memset (self->digest, 0, sizeof (self->digest));
    self->mode = mode;
    self->finalized = MW_FALSE;
    return MW_SUCCESS;
}