Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
digest.c
Go to the documentation of this file.
1/************************************************
2
3 digest.c -
4
5 $Author$
6 created at: Fri May 25 08:57:27 JST 2001
7
8 Copyright (C) 1995-2001 Yukihiro Matsumoto
9 Copyright (C) 2001-2006 Akinori MUSHA
10
11 $RoughId: digest.c,v 1.16 2001/07/13 15:38:27 knu Exp $
12 $Id$
13
14************************************************/
15
16#include "digest.h"
17
18static VALUE rb_mDigest;
19static VALUE rb_mDigest_Instance;
20static VALUE rb_cDigest_Class;
21static VALUE rb_cDigest_Base;
22
23static ID id_reset, id_update, id_finish, id_digest, id_hexdigest, id_digest_length;
24static ID id_metadata;
25
27
28/*
29 * Document-module: Digest
30 *
31 * This module provides a framework for message digest libraries.
32 *
33 * You may want to look at OpenSSL::Digest as it supports more algorithms.
34 *
35 * A cryptographic hash function is a procedure that takes data and returns a
36 * fixed bit string: the hash value, also known as _digest_. Hash functions
37 * are also called one-way functions, it is easy to compute a digest from
38 * a message, but it is infeasible to generate a message from a digest.
39 *
40 * == Examples
41 *
42 * require 'digest'
43 *
44 * # Compute a complete digest
45 * Digest::SHA256.digest 'message' #=> "\xABS\n\x13\xE4Y..."
46 *
47 * sha256 = Digest::SHA256.new
48 * sha256.digest 'message' #=> "\xABS\n\x13\xE4Y..."
49 *
50 * # Other encoding formats
51 * Digest::SHA256.hexdigest 'message' #=> "ab530a13e459..."
52 * Digest::SHA256.base64digest 'message' #=> "q1MKE+RZFJgr..."
53 *
54 * # Compute digest by chunks
55 * md5 = Digest::MD5.new
56 * md5.update 'message1'
57 * md5 << 'message2' # << is an alias for update
58 *
59 * md5.hexdigest #=> "94af09c09bb9..."
60 *
61 * # Compute digest for a file
62 * sha256 = Digest::SHA256.file 'testfile'
63 * sha256.hexdigest
64 *
65 * Additionally digests can be encoded in "bubble babble" format as a sequence
66 * of consonants and vowels which is more recognizable and comparable than a
67 * hexadecimal digest.
68 *
69 * require 'digest/bubblebabble'
70 *
71 * Digest::SHA256.bubblebabble 'message' #=> "xopoh-fedac-fenyh-..."
72 *
73 * See the bubble babble specification at
74 * http://web.mit.edu/kenta/www/one/bubblebabble/spec/jrtrjwzi/draft-huima-01.txt.
75 *
76 * == Digest algorithms
77 *
78 * Different digest algorithms (or hash functions) are available:
79 *
80 * MD5::
81 * See RFC 1321 The MD5 Message-Digest Algorithm
82 * RIPEMD-160::
83 * As Digest::RMD160.
84 * See http://homes.esat.kuleuven.be/~bosselae/ripemd160.html.
85 * SHA1::
86 * See FIPS 180 Secure Hash Standard.
87 * SHA2 family::
88 * See FIPS 180 Secure Hash Standard which defines the following algorithms:
89 * * SHA512
90 * * SHA384
91 * * SHA256
92 *
93 * The latest versions of the FIPS publications can be found here:
94 * http://csrc.nist.gov/publications/PubsFIPS.html.
95 */
96
97static VALUE
98hexencode_str_new(VALUE str_digest)
99{
100 char *digest;
101 size_t digest_len;
102 size_t i;
103 VALUE str;
104 char *p;
105 static const char hex[] = {
106 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
107 'a', 'b', 'c', 'd', 'e', 'f'
108 };
109
110 StringValue(str_digest);
111 digest = RSTRING_PTR(str_digest);
112 digest_len = RSTRING_LEN(str_digest);
113
114 if (LONG_MAX / 2 < digest_len) {
115 rb_raise(rb_eRuntimeError, "digest string too long");
116 }
117
118 str = rb_usascii_str_new(0, digest_len * 2);
119
120 for (i = 0, p = RSTRING_PTR(str); i < digest_len; i++) {
121 unsigned char byte = digest[i];
122
123 p[i + i] = hex[byte >> 4];
124 p[i + i + 1] = hex[byte & 0x0f];
125 }
126
127 RB_GC_GUARD(str_digest);
128
129 return str;
130}
131
132/*
133 * call-seq:
134 * Digest.hexencode(string) -> hexencoded_string
135 *
136 * Generates a hex-encoded version of a given _string_.
137 */
138static VALUE
139rb_digest_s_hexencode(VALUE klass, VALUE str)
140{
141 return hexencode_str_new(str);
142}
143
144NORETURN(static void rb_digest_instance_method_unimpl(VALUE self, const char *method));
145
146/*
147 * Document-module: Digest::Instance
148 *
149 * This module provides instance methods for a digest implementation
150 * object to calculate message digest values.
151 */
152
153static void
154rb_digest_instance_method_unimpl(VALUE self, const char *method)
155{
156 rb_raise(rb_eRuntimeError, "%s does not implement %s()",
157 rb_obj_classname(self), method);
158}
159
160/*
161 * call-seq:
162 * digest_obj.update(string) -> digest_obj
163 * digest_obj << string -> digest_obj
164 *
165 * Updates the digest using a given _string_ and returns self.
166 *
167 * The update() method and the left-shift operator are overridden by
168 * each implementation subclass. (One should be an alias for the
169 * other)
170 */
171static VALUE
172rb_digest_instance_update(VALUE self, VALUE str)
173{
174 rb_digest_instance_method_unimpl(self, "update");
175
177}
178
179/*
180 * call-seq:
181 * digest_obj.instance_eval { finish } -> digest_obj
182 *
183 * Finishes the digest and returns the resulting hash value.
184 *
185 * This method is overridden by each implementation subclass and often
186 * made private, because some of those subclasses may leave internal
187 * data uninitialized. Do not call this method from outside. Use
188 * #digest!() instead, which ensures that internal data be reset for
189 * security reasons.
190 */
191static VALUE
192rb_digest_instance_finish(VALUE self)
193{
194 rb_digest_instance_method_unimpl(self, "finish");
195
197}
198
199/*
200 * call-seq:
201 * digest_obj.reset -> digest_obj
202 *
203 * Resets the digest to the initial state and returns self.
204 *
205 * This method is overridden by each implementation subclass.
206 */
207static VALUE
208rb_digest_instance_reset(VALUE self)
209{
210 rb_digest_instance_method_unimpl(self, "reset");
211
213}
214
215/*
216 * call-seq:
217 * digest_obj.new -> another_digest_obj
218 *
219 * Returns a new, initialized copy of the digest object. Equivalent
220 * to digest_obj.clone().reset().
221 */
222static VALUE
223rb_digest_instance_new(VALUE self)
224{
225 VALUE clone = rb_obj_clone(self);
226 rb_funcall(clone, id_reset, 0);
227 return clone;
228}
229
230/*
231 * call-seq:
232 * digest_obj.digest -> string
233 * digest_obj.digest(string) -> string
234 *
235 * If none is given, returns the resulting hash value of the digest,
236 * keeping the digest's state.
237 *
238 * If a _string_ is given, returns the hash value for the given
239 * _string_, resetting the digest to the initial state before and
240 * after the process.
241 */
242static VALUE
243rb_digest_instance_digest(int argc, VALUE *argv, VALUE self)
244{
245 VALUE str, value;
246
247 if (rb_scan_args(argc, argv, "01", &str) > 0) {
248 rb_funcall(self, id_reset, 0);
249 rb_funcall(self, id_update, 1, str);
250 value = rb_funcall(self, id_finish, 0);
251 rb_funcall(self, id_reset, 0);
252 } else {
253 value = rb_funcall(rb_obj_clone(self), id_finish, 0);
254 }
255
256 return value;
257}
258
259/*
260 * call-seq:
261 * digest_obj.digest! -> string
262 *
263 * Returns the resulting hash value and resets the digest to the
264 * initial state.
265 */
266static VALUE
267rb_digest_instance_digest_bang(VALUE self)
268{
269 VALUE value = rb_funcall(self, id_finish, 0);
270 rb_funcall(self, id_reset, 0);
271
272 return value;
273}
274
275/*
276 * call-seq:
277 * digest_obj.hexdigest -> string
278 * digest_obj.hexdigest(string) -> string
279 *
280 * If none is given, returns the resulting hash value of the digest in
281 * a hex-encoded form, keeping the digest's state.
282 *
283 * If a _string_ is given, returns the hash value for the given
284 * _string_ in a hex-encoded form, resetting the digest to the initial
285 * state before and after the process.
286 */
287static VALUE
288rb_digest_instance_hexdigest(int argc, VALUE *argv, VALUE self)
289{
290 VALUE str, value;
291
292 if (rb_scan_args(argc, argv, "01", &str) > 0) {
293 rb_funcall(self, id_reset, 0);
294 rb_funcall(self, id_update, 1, str);
295 value = rb_funcall(self, id_finish, 0);
296 rb_funcall(self, id_reset, 0);
297 } else {
298 value = rb_funcall(rb_obj_clone(self), id_finish, 0);
299 }
300
301 return hexencode_str_new(value);
302}
303
304/*
305 * call-seq:
306 * digest_obj.hexdigest! -> string
307 *
308 * Returns the resulting hash value in a hex-encoded form and resets
309 * the digest to the initial state.
310 */
311static VALUE
312rb_digest_instance_hexdigest_bang(VALUE self)
313{
314 VALUE value = rb_funcall(self, id_finish, 0);
315 rb_funcall(self, id_reset, 0);
316
317 return hexencode_str_new(value);
318}
319
320/*
321 * call-seq:
322 * digest_obj.to_s -> string
323 *
324 * Returns digest_obj.hexdigest().
325 */
326static VALUE
327rb_digest_instance_to_s(VALUE self)
328{
329 return rb_funcall(self, id_hexdigest, 0);
330}
331
332/*
333 * call-seq:
334 * digest_obj.inspect -> string
335 *
336 * Creates a printable version of the digest object.
337 */
338static VALUE
339rb_digest_instance_inspect(VALUE self)
340{
341 VALUE str;
342 size_t digest_len = 32; /* about this size at least */
343 const char *cname;
344
345 cname = rb_obj_classname(self);
346
347 /* #<Digest::ClassName: xxxxx...xxxx> */
348 str = rb_str_buf_new(2 + strlen(cname) + 2 + digest_len * 2 + 1);
349 rb_str_buf_cat2(str, "#<");
350 rb_str_buf_cat2(str, cname);
351 rb_str_buf_cat2(str, ": ");
352 rb_str_buf_append(str, rb_digest_instance_hexdigest(0, 0, self));
353 rb_str_buf_cat2(str, ">");
354 return str;
355}
356
357/*
358 * call-seq:
359 * digest_obj == another_digest_obj -> boolean
360 * digest_obj == string -> boolean
361 *
362 * If a string is given, checks whether it is equal to the hex-encoded
363 * hash value of the digest object. If another digest instance is
364 * given, checks whether they have the same hash value. Otherwise
365 * returns false.
366 */
367static VALUE
368rb_digest_instance_equal(VALUE self, VALUE other)
369{
370 VALUE str1, str2;
371
372 if (rb_obj_is_kind_of(other, rb_mDigest_Instance) == Qtrue) {
373 str1 = rb_digest_instance_digest(0, 0, self);
374 str2 = rb_digest_instance_digest(0, 0, other);
375 } else {
376 str1 = rb_digest_instance_to_s(self);
377 str2 = rb_check_string_type(other);
378 if (NIL_P(str2)) return Qfalse;
379 }
380
381 /* never blindly assume that subclass methods return strings */
382 StringValue(str1);
383 StringValue(str2);
384
385 if (RSTRING_LEN(str1) == RSTRING_LEN(str2) &&
386 rb_str_cmp(str1, str2) == 0) {
387 return Qtrue;
388 }
389 return Qfalse;
390}
391
392/*
393 * call-seq:
394 * digest_obj.digest_length -> integer
395 *
396 * Returns the length of the hash value of the digest.
397 *
398 * This method should be overridden by each implementation subclass.
399 * If not, digest_obj.digest().length() is returned.
400 */
401static VALUE
402rb_digest_instance_digest_length(VALUE self)
403{
404 /* subclasses really should redefine this method */
405 VALUE digest = rb_digest_instance_digest(0, 0, self);
406
407 /* never blindly assume that #digest() returns a string */
408 StringValue(digest);
409 return INT2NUM(RSTRING_LEN(digest));
410}
411
412/*
413 * call-seq:
414 * digest_obj.length -> integer
415 * digest_obj.size -> integer
416 *
417 * Returns digest_obj.digest_length().
418 */
419static VALUE
420rb_digest_instance_length(VALUE self)
421{
422 return rb_funcall(self, id_digest_length, 0);
423}
424
425/*
426 * call-seq:
427 * digest_obj.block_length -> integer
428 *
429 * Returns the block length of the digest.
430 *
431 * This method is overridden by each implementation subclass.
432 */
433static VALUE
434rb_digest_instance_block_length(VALUE self)
435{
436 rb_digest_instance_method_unimpl(self, "block_length");
437
439}
440
441/*
442 * Document-class: Digest::Class
443 *
444 * This module stands as a base class for digest implementation
445 * classes.
446 */
447
448/*
449 * call-seq:
450 * Digest::Class.digest(string, *parameters) -> hash_string
451 *
452 * Returns the hash value of a given _string_. This is equivalent to
453 * Digest::Class.new(*parameters).digest(string), where extra
454 * _parameters_, if any, are passed through to the constructor and the
455 * _string_ is passed to #digest().
456 */
457static VALUE
458rb_digest_class_s_digest(int argc, VALUE *argv, VALUE klass)
459{
460 VALUE str;
461 volatile VALUE obj;
462
463 if (argc < 1) {
464 rb_raise(rb_eArgError, "no data given");
465 }
466
467 str = *argv++;
468 argc--;
469
471
474
475 return rb_funcall(obj, id_digest, 1, str);
476}
477
478/*
479 * call-seq:
480 * Digest::Class.hexdigest(string[, ...]) -> hash_string
481 *
482 * Returns the hex-encoded hash value of a given _string_. This is
483 * almost equivalent to
484 * Digest.hexencode(Digest::Class.new(*parameters).digest(string)).
485 */
486static VALUE
487rb_digest_class_s_hexdigest(int argc, VALUE *argv, VALUE klass)
488{
489 return hexencode_str_new(rb_funcallv(klass, id_digest, argc, argv));
490}
491
492/* :nodoc: */
493static VALUE
494rb_digest_class_init(VALUE self)
495{
496 return self;
497}
498
499/*
500 * Document-class: Digest::Base
501 *
502 * This abstract class provides a common interface to message digest
503 * implementation classes written in C.
504 *
505 * ==Write a Digest subclass in C
506 * Digest::Base provides a common interface to message digest
507 * classes written in C. These classes must provide a struct
508 * of type rb_digest_metadata_t:
509 * typedef int (*rb_digest_hash_init_func_t)(void *);
510 * typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
511 * typedef int (*rb_digest_hash_finish_func_t)(void *, unsigned char *);
512 *
513 * typedef struct {
514 * int api_version;
515 * size_t digest_len;
516 * size_t block_len;
517 * size_t ctx_size;
518 * rb_digest_hash_init_func_t init_func;
519 * rb_digest_hash_update_func_t update_func;
520 * rb_digest_hash_finish_func_t finish_func;
521 * } rb_digest_metadata_t;
522 *
523 * This structure must be set as an instance variable named +metadata+
524 * (without the +@+ in front of the name). By example:
525 * static const rb_digest_metadata_t sha1 = {
526 * RUBY_DIGEST_API_VERSION,
527 * SHA1_DIGEST_LENGTH,
528 * SHA1_BLOCK_LENGTH,
529 * sizeof(SHA1_CTX),
530 * (rb_digest_hash_init_func_t)SHA1_Init,
531 * (rb_digest_hash_update_func_t)SHA1_Update,
532 * (rb_digest_hash_finish_func_t)SHA1_Finish,
533 * };
534 *
535 *
536 * rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
537 * Data_Wrap_Struct(0, 0, 0, (void *)&sha1));
538 */
539
541get_digest_base_metadata(VALUE klass)
542{
543 VALUE p;
544 VALUE obj;
546
547 for (p = klass; !NIL_P(p); p = rb_class_superclass(p)) {
548 if (rb_ivar_defined(p, id_metadata)) {
549 obj = rb_ivar_get(p, id_metadata);
550 break;
551 }
552 }
553
554 if (NIL_P(p))
555 rb_raise(rb_eRuntimeError, "Digest::Base cannot be directly inherited in Ruby");
556
557 if (!RB_TYPE_P(obj, T_DATA) || RTYPEDDATA_P(obj)) {
558 wrong:
559 if (p == klass)
560 rb_raise(rb_eTypeError, "%"PRIsVALUE"::metadata is not initialized properly",
561 klass);
562 else
563 rb_raise(rb_eTypeError, "%"PRIsVALUE"(%"PRIsVALUE")::metadata is not initialized properly",
564 klass, p);
565 }
566
567#undef RUBY_UNTYPED_DATA_WARNING
568#define RUBY_UNTYPED_DATA_WARNING 0
570
571 if (!algo) goto wrong;
572
573 switch (algo->api_version) {
574 case 3:
575 break;
576
577 /*
578 * put conversion here if possible when API is updated
579 */
580
581 default:
582 rb_raise(rb_eRuntimeError, "Incompatible digest API version");
583 }
584
585 return algo;
586}
587
589get_digest_obj_metadata(VALUE obj)
590{
591 return get_digest_base_metadata(rb_obj_class(obj));
592}
593
594static const rb_data_type_t digest_type = {
595 "digest",
597 0, 0,
599};
600
601static inline void
602algo_init(const rb_digest_metadata_t *algo, void *pctx)
603{
604 if (algo->init_func(pctx) != 1) {
605 rb_raise(rb_eRuntimeError, "Digest initialization failed.");
606 }
607}
608
609static VALUE
610rb_digest_base_alloc(VALUE klass)
611{
613 VALUE obj;
614 void *pctx;
615
616 if (klass == rb_cDigest_Base) {
617 rb_raise(rb_eNotImpError, "Digest::Base is an abstract class");
618 }
619
620 algo = get_digest_base_metadata(klass);
621
622 obj = rb_data_typed_object_zalloc(klass, algo->ctx_size, &digest_type);
623 pctx = RTYPEDDATA_DATA(obj);
624 algo_init(algo, pctx);
625
626 return obj;
627}
628
629/* :nodoc: */
630static VALUE
631rb_digest_base_copy(VALUE copy, VALUE obj)
632{
634 void *pctx1, *pctx2;
635
636 if (copy == obj) return copy;
637
638 rb_check_frozen(copy);
639
640 algo = get_digest_obj_metadata(copy);
641 if (algo != get_digest_obj_metadata(obj))
642 rb_raise(rb_eTypeError, "different algorithms");
643
644 TypedData_Get_Struct(obj, void, &digest_type, pctx1);
645 TypedData_Get_Struct(copy, void, &digest_type, pctx2);
646 memcpy(pctx2, pctx1, algo->ctx_size);
647
648 return copy;
649}
650
651/*
652 * call-seq: digest_base.reset -> digest_base
653 *
654 * Reset the digest to its initial state and return +self+.
655 */
656static VALUE
657rb_digest_base_reset(VALUE self)
658{
660 void *pctx;
661
662 algo = get_digest_obj_metadata(self);
663
664 TypedData_Get_Struct(self, void, &digest_type, pctx);
665
666 algo_init(algo, pctx);
667
668 return self;
669}
670
671/*
672 * call-seq:
673 * digest_base.update(string) -> digest_base
674 * digest_base << string -> digest_base
675 *
676 * Update the digest using given _string_ and return +self+.
677 */
678static VALUE
679rb_digest_base_update(VALUE self, VALUE str)
680{
682 void *pctx;
683
684 algo = get_digest_obj_metadata(self);
685
686 TypedData_Get_Struct(self, void, &digest_type, pctx);
687
689 algo->update_func(pctx, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
691
692 return self;
693}
694
695/* :nodoc: */
696static VALUE
697rb_digest_base_finish(VALUE self)
698{
700 void *pctx;
701 VALUE str;
702
703 algo = get_digest_obj_metadata(self);
704
705 TypedData_Get_Struct(self, void, &digest_type, pctx);
706
707 str = rb_str_new(0, algo->digest_len);
708 algo->finish_func(pctx, (unsigned char *)RSTRING_PTR(str));
709
710 /* avoid potential coredump caused by use of a finished context */
711 algo_init(algo, pctx);
712
713 return str;
714}
715
716/*
717 * call-seq: digest_base.digest_length -> Integer
718 *
719 * Return the length of the hash value in bytes.
720 */
721static VALUE
722rb_digest_base_digest_length(VALUE self)
723{
725
726 algo = get_digest_obj_metadata(self);
727
728 return INT2NUM(algo->digest_len);
729}
730
731/*
732 * call-seq: digest_base.block_length -> Integer
733 *
734 * Return the block length of the digest in bytes.
735 */
736static VALUE
737rb_digest_base_block_length(VALUE self)
738{
740
741 algo = get_digest_obj_metadata(self);
742
743 return INT2NUM(algo->block_len);
744}
745
746void
748{
749#undef rb_intern
750 id_reset = rb_intern("reset");
751 id_update = rb_intern("update");
752 id_finish = rb_intern("finish");
753 id_digest = rb_intern("digest");
754 id_hexdigest = rb_intern("hexdigest");
755 id_digest_length = rb_intern("digest_length");
756 id_metadata = rb_id_metadata();
757 InitVM(digest);
758}
759
760void
762{
763 /*
764 * module Digest
765 */
766 rb_mDigest = rb_define_module("Digest");
767
768 /* module functions */
769 rb_define_module_function(rb_mDigest, "hexencode", rb_digest_s_hexencode, 1);
770
771 /*
772 * module Digest::Instance
773 */
774 rb_mDigest_Instance = rb_define_module_under(rb_mDigest, "Instance");
775
776 /* instance methods that should be overridden */
777 rb_define_method(rb_mDigest_Instance, "update", rb_digest_instance_update, 1);
778 rb_define_method(rb_mDigest_Instance, "<<", rb_digest_instance_update, 1);
779 rb_define_private_method(rb_mDigest_Instance, "finish", rb_digest_instance_finish, 0);
780 rb_define_method(rb_mDigest_Instance, "reset", rb_digest_instance_reset, 0);
781 rb_define_method(rb_mDigest_Instance, "digest_length", rb_digest_instance_digest_length, 0);
782 rb_define_method(rb_mDigest_Instance, "block_length", rb_digest_instance_block_length, 0);
783
784 /* instance methods that may be overridden */
785 rb_define_method(rb_mDigest_Instance, "==", rb_digest_instance_equal, 1);
786 rb_define_method(rb_mDigest_Instance, "inspect", rb_digest_instance_inspect, 0);
787
788 /* instance methods that need not usually be overridden */
789 rb_define_method(rb_mDigest_Instance, "new", rb_digest_instance_new, 0);
790 rb_define_method(rb_mDigest_Instance, "digest", rb_digest_instance_digest, -1);
791 rb_define_method(rb_mDigest_Instance, "digest!", rb_digest_instance_digest_bang, 0);
792 rb_define_method(rb_mDigest_Instance, "hexdigest", rb_digest_instance_hexdigest, -1);
793 rb_define_method(rb_mDigest_Instance, "hexdigest!", rb_digest_instance_hexdigest_bang, 0);
794 rb_define_method(rb_mDigest_Instance, "to_s", rb_digest_instance_to_s, 0);
795 rb_define_method(rb_mDigest_Instance, "length", rb_digest_instance_length, 0);
796 rb_define_method(rb_mDigest_Instance, "size", rb_digest_instance_length, 0);
797
798 /*
799 * class Digest::Class
800 */
801 rb_cDigest_Class = rb_define_class_under(rb_mDigest, "Class", rb_cObject);
802 rb_define_method(rb_cDigest_Class, "initialize", rb_digest_class_init, 0);
803 rb_include_module(rb_cDigest_Class, rb_mDigest_Instance);
804
805 /* class methods */
806 rb_define_singleton_method(rb_cDigest_Class, "digest", rb_digest_class_s_digest, -1);
807 rb_define_singleton_method(rb_cDigest_Class, "hexdigest", rb_digest_class_s_hexdigest, -1);
808
809 /* class Digest::Base < Digest::Class */
810 rb_cDigest_Base = rb_define_class_under(rb_mDigest, "Base", rb_cDigest_Class);
811
812 rb_define_alloc_func(rb_cDigest_Base, rb_digest_base_alloc);
813
814 rb_define_method(rb_cDigest_Base, "initialize_copy", rb_digest_base_copy, 1);
815 rb_define_method(rb_cDigest_Base, "reset", rb_digest_base_reset, 0);
816 rb_define_method(rb_cDigest_Base, "update", rb_digest_base_update, 1);
817 rb_define_method(rb_cDigest_Base, "<<", rb_digest_base_update, 1);
818 rb_define_private_method(rb_cDigest_Base, "finish", rb_digest_base_finish, 0);
819 rb_define_method(rb_cDigest_Base, "digest_length", rb_digest_base_digest_length, 0);
820 rb_define_method(rb_cDigest_Base, "block_length", rb_digest_base_block_length, 0);
821}
RUBY_EXTERN void Init_digest_base(void)
NORETURN(static void rb_digest_instance_method_unimpl(VALUE self, const char *method))
void InitVM_digest(void)
Definition: digest.c:761
void Init_digest(void)
Definition: digest.c:747
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
void rb_include_module(VALUE, VALUE)
Definition: class.c:882
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
Definition: class.c:711
VALUE rb_define_module(const char *)
Definition: class.c:785
VALUE rb_define_module_under(VALUE, const char *)
Definition: class.c:810
VALUE rb_cObject
Object class.
Definition: ruby.h:2012
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2671
VALUE rb_eNotImpError
Definition: error.c:934
VALUE rb_eTypeError
Definition: error.c:924
VALUE rb_eRuntimeError
Definition: error.c:922
VALUE rb_eArgError
Definition: error.c:925
VALUE rb_class_superclass(VALUE)
Returns the superclass of klass.
Definition: object.c:1976
void rb_obj_call_init(VALUE, int, const VALUE *)
Calls #initialize method of obj with the given arguments.
Definition: eval.c:1681
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
Definition: object.c:1895
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
VALUE rb_obj_clone(VALUE)
Almost same as Object::clone.
Definition: object.c:410
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
Definition: object.c:692
#define rb_funcallv(recv, mid, argc, argv)
use StringValue() instead")))
#define RSTRING_LEN(str)
#define rb_str_buf_cat2
size_t strlen(const char *)
void rb_define_private_method(VALUE, const char *, VALUE(*)(), int)
#define RUBY_TYPED_WB_PROTECTED
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1070
const VALUE VALUE obj
#define rb_check_frozen(obj)
#define UNREACHABLE
#define RSTRING_PTR(str)
#define RTYPEDDATA_DATA(v)
#define rb_str_new(str, len)
#define NIL_P(v)
#define LONG_MAX
#define RUBY_TYPED_DEFAULT_FREE
#define rb_usascii_str_new(str, len)
#define T_DATA
VALUE rb_str_buf_new(long)
Definition: string.c:1315
VALUE rb_check_string_type(VALUE)
Definition: string.c:2314
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
uint32_t i
const char * rb_obj_classname(VALUE)
Definition: variable.c:289
#define INT2NUM(x)
void rb_define_module_function(VALUE, const char *, VALUE(*)(), int)
void rb_define_singleton_method(VALUE, const char *, VALUE(*)(), int)
#define RB_GC_GUARD(v)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define TypedData_Get_Struct(obj, type, data_type, sval)
#define PRIsVALUE
#define rb_funcall(recv, mid, argc,...)
#define rb_scan_args(argc, argvp, fmt,...)
VALUE rb_ivar_defined(VALUE, ID)
Definition: variable.c:1317
#define rb_intern(str)
int rb_str_cmp(VALUE, VALUE)
Definition: string.c:3228
#define Qtrue
#define Qfalse
void * memcpy(void *__restrict__, const void *__restrict__, size_t)
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2950
#define RTYPEDDATA_P(v)
#define RB_TYPE_P(obj, type)
const VALUE * argv
#define Data_Get_Struct(obj, type, sval)
#define RUBY_EXTERN
unsigned long ID
#define InitVM(ext)
void rb_define_method(VALUE, const char *, VALUE(*)(), int)
unsigned long VALUE
Definition: ruby.h:102
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
size_t block_len
Definition: digest.h:27
rb_digest_hash_init_func_t init_func
Definition: digest.h:29
size_t digest_len
Definition: digest.h:26
rb_digest_hash_finish_func_t finish_func
Definition: digest.h:31
rb_digest_hash_update_func_t update_func
Definition: digest.h:30