Class: Digest::Base
Overview
This abstract class provides a common interface to message digest implementation classes written in C.
Write a Digest subclass in C
Digest::Base provides a common interface to message digest classes written in C. These classes must provide a struct of type rb_digest_metadata_t:
typedef int (*rb_digest_hash_init_func_t)(void *);
typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
typedef int (*rb_digest_hash_finish_func_t)(void *, unsigned char *);
typedef struct {
int api_version;
size_t digest_len;
size_t block_len;
size_t ctx_size;
rb_digest_hash_init_func_t init_func;
rb_digest_hash_update_func_t update_func;
rb_digest_hash_finish_func_t finish_func;
} rb_digest_metadata_t;
This structure must be set as an instance variable named metadata
(without the @ in front of the name). By example:
static const rb_digest_metadata_t sha1 = {
RUBY_DIGEST_API_VERSION,
SHA1_DIGEST_LENGTH,
SHA1_BLOCK_LENGTH,
sizeof(SHA1_CTX),
(rb_digest_hash_init_func_t)SHA1_Init,
(rb_digest_hash_update_func_t)SHA1_Update,
(rb_digest_hash_finish_func_t)SHA1_Finish,
};
rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
rb_digest_make_metadata(&sha1));
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Update the digest using given string and return
self
. -
#block_length ⇒ Integer
Return the block length of the digest in bytes.
-
#digest_length ⇒ Integer
Return the length of the hash value in bytes.
-
#initialize_copy(obj) ⇒ Object
:nodoc:.
-
#reset ⇒ Object
Reset the digest to its initial state and return
self
. -
#update(str) ⇒ Object
Update the digest using given string and return
self
.
Methods inherited from Class
base64digest, bubblebabble, digest, file, hexdigest, #initialize
Methods included from Instance
#==, #base64digest, #base64digest!, #bubblebabble, #digest, #digest!, #file, #hexdigest, #hexdigest!, #inspect, #length, #new, #size, #to_s
Constructor Details
This class inherits a constructor from Digest::Class
Instance Method Details
#update(string) ⇒ Object #<<(string) ⇒ Object
Update the digest using given string and return self
.
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'digest.c', line 702
static VALUE
rb_digest_base_update(VALUE self, VALUE str)
{
rb_digest_metadata_t *algo;
void *pctx;
algo = get_digest_obj_metadata(self);
TypedData_Get_Struct(self, void, &digest_type, pctx);
StringValue(str);
algo->update_func(pctx, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
RB_GC_GUARD(str);
return self;
}
|
#block_length ⇒ Integer
Return the block length of the digest in bytes.
760 761 762 763 764 765 766 767 768 |
# File 'digest.c', line 760
static VALUE
rb_digest_base_block_length(VALUE self)
{
rb_digest_metadata_t *algo;
algo = get_digest_obj_metadata(self);
return SIZET2NUM(algo->block_len);
}
|
#digest_length ⇒ Integer
Return the length of the hash value in bytes.
745 746 747 748 749 750 751 752 753 |
# File 'digest.c', line 745
static VALUE
rb_digest_base_digest_length(VALUE self)
{
rb_digest_metadata_t *algo;
algo = get_digest_obj_metadata(self);
return SIZET2NUM(algo->digest_len);
}
|
#initialize_copy(obj) ⇒ Object
:nodoc:
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'digest.c', line 654
static VALUE
rb_digest_base_copy(VALUE copy, VALUE obj)
{
rb_digest_metadata_t *algo;
void *pctx1, *pctx2;
if (copy == obj) return copy;
rb_check_frozen(copy);
algo = get_digest_obj_metadata(copy);
if (algo != get_digest_obj_metadata(obj))
rb_raise(rb_eTypeError, "different algorithms");
TypedData_Get_Struct(obj, void, &digest_type, pctx1);
TypedData_Get_Struct(copy, void, &digest_type, pctx2);
memcpy(pctx2, pctx1, algo->ctx_size);
return copy;
}
|
#reset ⇒ Object
Reset the digest to its initial state and return self
.
680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
# File 'digest.c', line 680
static VALUE
rb_digest_base_reset(VALUE self)
{
rb_digest_metadata_t *algo;
void *pctx;
algo = get_digest_obj_metadata(self);
TypedData_Get_Struct(self, void, &digest_type, pctx);
algo_init(algo, pctx);
return self;
}
|
#update(string) ⇒ Object #<<(string) ⇒ Object
Update the digest using given string and return self
.
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'digest.c', line 702
static VALUE
rb_digest_base_update(VALUE self, VALUE str)
{
rb_digest_metadata_t *algo;
void *pctx;
algo = get_digest_obj_metadata(self);
TypedData_Get_Struct(self, void, &digest_type, pctx);
StringValue(str);
algo->update_func(pctx, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str));
RB_GC_GUARD(str);
return self;
}
|