summaryrefslogtreecommitdiff
path: root/ext/digest/digest.c
blob: 5355776119b67a937e14ad72c730266e89ae2ecf (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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/************************************************

  digest.c -

  $Author$
  created at: Fri May 25 08:57:27 JST 2001

  Copyright (C) 1995-2001 Yukihiro Matsumoto
  Copyright (C) 2001 Akinori MUSHA

  $RoughId: digest.c,v 1.16 2001/07/13 15:38:27 knu Exp $
  $Id$

************************************************/

/*
 * This module provides an interface to the following hash algorithms:
 *
 *   - the MD5 Message-Digest Algorithm by the RSA Data Security,
 *     Inc., described in RFC 1321
 *
 *   - the SHA-1 Secure Hash Algorithm by NIST (the US' National
 *     Institute of Standards and Technology), described in FIPS PUB
 *     180-1.
 *
 *   - the SHA-256/384/512 Secure Hash Algorithm by NIST (the US'
 *     National Institute of Standards and Technology), described in
 *     FIPS PUB 180-2.
 *
 *   - the RIPEMD-160 cryptographic hash function, designed by Hans
 *     Dobbertin, Antoon Bosselaers, and Bart Preneel.
 */

#include "digest.h"

static VALUE mDigest, cDigest_Base;
static ID id_metadata;

/*
 * Digest::Base
 */

static algo_t *
get_digest_base_metadata(class)
    VALUE class;
{
    VALUE obj;
    algo_t *algo;

    if (rb_cvar_defined(class, id_metadata) == Qfalse)
	rb_notimplement();

    obj = rb_cvar_get(class, id_metadata);

    Data_Get_Struct(obj, algo_t, algo);

    return algo;
}
	
static VALUE
rb_digest_base_s_new(argc, argv, class)
    int argc;
    VALUE* argv;
    VALUE class;
{
    algo_t *algo;
    VALUE obj;
    void *pctx;

    if (class == cDigest_Base)
	rb_raise(rb_eNotImpError, "Digest::Base is an abstract class");

    algo = get_digest_base_metadata(class);

    pctx = xmalloc(algo->ctx_size);
    algo->init_func(pctx);

    obj = Data_Wrap_Struct(class, 0, free, pctx);

    rb_obj_call_init(obj, argc, argv);

    return obj;
}

static VALUE
rb_digest_base_s_digest(class, str)
    VALUE class;
    VALUE str;
{
    algo_t *algo;
    void *pctx;
    size_t len;
    unsigned char *digest;
    VALUE obj;

    if (class == cDigest_Base)
	rb_raise(rb_eNotImpError, "Digest::Base is an abstract class");

#ifdef StringValue
    StringValue(str);
#else
    Check_Type(str, T_STRING);
#endif

    algo = get_digest_base_metadata(class);

    pctx = xmalloc(algo->ctx_size);
    algo->init_func(pctx);
    algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);

    len = algo->digest_len;

    digest = xmalloc(len);
    algo->final_func(digest, pctx);

    obj = rb_str_new(digest, len);

    free(digest);
    free(pctx);

    return obj;
}

static VALUE
rb_digest_base_s_hexdigest(class, str)
    VALUE class;
    VALUE str;
{
    algo_t *algo;
    void *pctx;
    size_t len;
    unsigned char *hexdigest;
    VALUE obj;

    if (class == cDigest_Base)
	rb_raise(rb_eNotImpError, "Digest::Base is an abstract class");

#ifdef StringValue
    StringValue(str);
#else
    Check_Type(str, T_STRING);
#endif

    algo = get_digest_base_metadata(class);

    pctx = xmalloc(algo->ctx_size);
    algo->init_func(pctx);
    algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);

    len = algo->digest_len * 2;

    hexdigest = xmalloc(len + 1); /* +1 is for '\0' */
    algo->end_func(pctx, hexdigest);
    puts(hexdigest);
    obj = rb_str_new(hexdigest, len);

    free(hexdigest);
    free(pctx);

    return obj;
}

static VALUE
rb_digest_base_clone(self)
    VALUE self;
{
    algo_t *algo;
    void *pctx1, *pctx2;
    VALUE class;

    class = CLASS_OF(self);
    algo = get_digest_base_metadata(class);
    Data_Get_Struct(self, void, pctx1);

    pctx2 = xmalloc(algo->ctx_size);
    memcpy(pctx2, pctx1, algo->ctx_size);

    return Data_Wrap_Struct(class, 0, free, pctx2);
}

static VALUE
rb_digest_base_update(self, str)
    VALUE self, str;
{
    algo_t *algo;
    void *pctx;

#ifdef StringValue
    StringValue(str);
#else
    Check_Type(str, T_STRING);
#endif

    algo = get_digest_base_metadata(CLASS_OF(self));
    Data_Get_Struct(self, void, pctx);

    algo->update_func(pctx, RSTRING(str)->ptr, RSTRING(str)->len);

    return self;
}

static VALUE
rb_digest_base_init(argc, argv, self)
    int argc;
    VALUE* argv;
    VALUE self;
{
    VALUE arg;

    rb_scan_args(argc, argv, "01", &arg);

    if (!NIL_P(arg)) rb_digest_base_update(self, arg);

    return self;
}

static VALUE
rb_digest_base_digest(self)
    VALUE self;
{
    algo_t *algo;
    void *pctx1, *pctx2;
    unsigned char *digest;
    size_t len;
    VALUE str;

    algo = get_digest_base_metadata(CLASS_OF(self));
    Data_Get_Struct(self, void, pctx1);

    len = algo->ctx_size;

    pctx2 = xmalloc(len);
    memcpy(pctx2, pctx1, len);

    len = algo->digest_len;

    digest = xmalloc(len);
    algo->final_func(digest, pctx2);

    str = rb_str_new(digest, len);

    free(digest);
    free(pctx2);

    return str;
}

static VALUE
rb_digest_base_hexdigest(self)
    VALUE self;
{
    algo_t *algo;
    void *pctx1, *pctx2;
    unsigned char *hexdigest;
    size_t len;
    VALUE str;

    algo = get_digest_base_metadata(CLASS_OF(self));
    Data_Get_Struct(self, void, pctx1);

    len = algo->ctx_size;

    pctx2 = xmalloc(len);
    memcpy(pctx2, pctx1, len);

    len = algo->digest_len * 2;

    hexdigest = xmalloc(len + 1); /* +1 is for '\0' */
    algo->end_func(pctx2, hexdigest);

    str = rb_str_new(hexdigest, len);

    free(hexdigest);
    free(pctx2);

    return str;
}

static VALUE
rb_digest_base_equal(self, other)
    VALUE self, other;
{
    algo_t *algo;
    VALUE class;
    VALUE str1, str2;

    class = CLASS_OF(self);
    algo = get_digest_base_metadata(class);

    if (CLASS_OF(other) == class) {
	void *pctx1, *pctx2;

	Data_Get_Struct(self, void, pctx1);
	Data_Get_Struct(other, void, pctx2);

	return algo->equal_func(pctx1, pctx2) ? Qtrue : Qfalse;
    }

#ifdef StringValue
    StringValue(other);
#else
    Check_Type(other, T_STRING);
#endif
    str2 = other;

    if (RSTRING(str2)->len == algo->digest_len)
	str1 = rb_digest_base_digest(self);
    else
	str1 = rb_digest_base_hexdigest(self);

    if (RSTRING(str1)->len == RSTRING(str2)->len
      && rb_str_cmp(str1, str2) == 0)
	return Qtrue;

    return Qfalse;
}

/*
 * Init
 */

void
Init_digest()
{
    mDigest = rb_define_module("Digest");

    cDigest_Base = rb_define_class_under(mDigest, "Base", rb_cObject);

    rb_define_singleton_method(cDigest_Base, "new", rb_digest_base_s_new, -1);
    rb_define_singleton_method(cDigest_Base, "digest", rb_digest_base_s_digest, 1);
    rb_define_singleton_method(cDigest_Base, "hexdigest", rb_digest_base_s_hexdigest, 1);

    rb_define_method(cDigest_Base, "initialize", rb_digest_base_init, -1);
    rb_define_method(cDigest_Base, "clone",  rb_digest_base_clone, 0);
    rb_define_method(cDigest_Base, "update", rb_digest_base_update, 1);
    rb_define_method(cDigest_Base, "<<", rb_digest_base_update, 1);
    rb_define_method(cDigest_Base, "digest", rb_digest_base_digest, 0);
    rb_define_method(cDigest_Base, "hexdigest", rb_digest_base_hexdigest, 0);
    rb_define_method(cDigest_Base, "to_s", rb_digest_base_hexdigest, 0);
    rb_define_method(cDigest_Base, "==", rb_digest_base_equal, 1);

    id_metadata = rb_intern("metadata");
}