File: digest.c

package info (click to toggle)
fwknop 2.0.0rc2-2%2Bdeb7u2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,656 kB
  • sloc: ansic: 12,916; sh: 10,743; perl: 545; makefile: 266
file content (228 lines) | stat: -rw-r--r-- 5,435 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* $Id$
 *****************************************************************************
 *
 * File:    digest.c
 *
 * Author:  Damien S. Stuart
 *
 * Purpose: Roll-up of the digests used by fwknop.
 *
 * Copyright 2009-2010 Damien Stuart (dstuart@dstuart.org)
 *
 *  License (GNU Public License):
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *****************************************************************************
*/
#include "digest.h"
#include "base64.h"

/* Convert a raw digest into its hex string representation.
*/
void
digest_to_hex(char *out, unsigned char *in, size_t size)
{
    size_t i;

    for(i=0; i<size; i++)
    {
        sprintf(out, "%02x", in[i]);
        out += 2;
    }
}

/* Compute MD5 hash on in and store result in out.
*/
void
md5(unsigned char *out, unsigned char *in, size_t size)
{
    MD5Context ctx;

    MD5Init(&ctx);
    MD5Update(&ctx, (unsigned char*)in, size);
    MD5Final(out, &ctx);
}

/* Compute MD5 hash on in and store the hex string result in out.
*/
void
md5_hex(char *out, unsigned char *in, size_t size)
{
    uint8_t      md[MD5_DIGEST_LENGTH];

    md5(md, in, size);
    digest_to_hex(out, md, MD5_DIGEST_LENGTH);
}

/* Compute MD5 hash on in and store the base64 string result in out.
*/
void
md5_base64(char *out, unsigned char *in, size_t size)
{
    uint8_t      md[MD5_DIGEST_LENGTH];

    md5(md, in, size);
    b64_encode(md, out, MD5_DIGEST_LENGTH);

    strip_b64_eq(out);
}

/* Compute SHA1 hash on in and store result in out.
*/
void
sha1(unsigned char *out, unsigned char *in, size_t size)
{
    SHA1_INFO    sha1_info;

    sha1_init(&sha1_info);
    sha1_update(&sha1_info, (uint8_t*)in, size);
    sha1_final(out, &sha1_info);
}

/* Compute SHA1 hash on in and store the hex string result in out.
*/
void
sha1_hex(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA1_DIGEST_LENGTH];

    sha1(md, in, size);
    digest_to_hex(out, md, SHA1_DIGEST_LENGTH);
}

/* Compute SHA1 hash on in and store the base64 string result in out.
*/
void
sha1_base64(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA1_DIGEST_LENGTH];

    sha1(md, in, size);
    b64_encode(md, out, SHA1_DIGEST_LENGTH);

    strip_b64_eq(out);
}

/* Compute SHA256 hash on in and store the hex string result in out.
*/
void
sha256(unsigned char *out, unsigned char *in, size_t size)
{
    SHA256_CTX    sha256_ctx;

    SHA256_Init(&sha256_ctx);
    SHA256_Update(&sha256_ctx, (const uint8_t*)in, size);
    SHA256_Final(out, &sha256_ctx);
}

/* Compute SHA256 hash on in and store the hex string result in out.
*/
void
sha256_hex(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA256_DIGEST_LENGTH];

    sha256(md, in, size);
    digest_to_hex(out, md, SHA256_DIGEST_LENGTH);
}

/* Compute SHA256 hash on in and store the base64 string result in out.
*/
void
sha256_base64(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA256_DIGEST_LENGTH];

    sha256(md, in, size);
    b64_encode(md, out, SHA256_DIGEST_LENGTH);

    strip_b64_eq(out);
}

/* Compute SHA384 hash on in and store the hex string result in out.
*/
void
sha384(unsigned char *out, unsigned char *in, size_t size)
{
    SHA384_CTX    sha384_ctx;

    SHA384_Init(&sha384_ctx);
    SHA384_Update(&sha384_ctx, (const uint8_t*)in, size);
    SHA384_Final(out, &sha384_ctx);
}

/* Compute SHA384 hash on in and store the hex string result in out.
*/
void
sha384_hex(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA384_DIGEST_LENGTH];

    sha384(md, in, size);
    digest_to_hex(out, md, SHA384_DIGEST_LENGTH);
}

/* Compute SHA384 hash on in and store the base64 string result in out.
*/
void
sha384_base64(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA384_DIGEST_LENGTH];

    sha384(md, in, size);
    b64_encode(md, out, SHA384_DIGEST_LENGTH);

    strip_b64_eq(out);
}

/* Compute SHA512 hash on in and store the hex string result in out.
*/
void
sha512(unsigned char *out, unsigned char *in, size_t size)
{
    SHA512_CTX    sha512_ctx;

    SHA512_Init(&sha512_ctx);
    SHA512_Update(&sha512_ctx, (const uint8_t*)in, size);
    SHA512_Final(out, &sha512_ctx);
}

/* Compute SHA512 hash on in and store the hex string result in out.
*/
void
sha512_hex(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA512_DIGEST_LENGTH];

    sha512(md, in, size);
    digest_to_hex(out, md, SHA512_DIGEST_LENGTH);
}

/* Compute SHA512 hash on in and store the base64 string result in out.
*/
void
sha512_base64(char *out, unsigned char *in, size_t size)
{
    uint8_t       md[SHA512_DIGEST_LENGTH];

    sha512(md, in, size);
    b64_encode(md, out, SHA512_DIGEST_LENGTH);

    strip_b64_eq(out);
}

/***EOF***/