123456789_123456789_123456789_123456789_123456789_

Class: Digest::Base

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Class
Instance Chain:
self, Class, Instance
Inherits: Digest::Class
Defined in: ext/digest/digest.c,
ext/digest/digest.c

Overview

This abstract class provides a common interface to message digest implementation classes written in C.

Write a Digest subclass in C

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;
} ;

This structure must be set as an instance variable named metadata (without the @ in front of the name). By example:

 static const  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"),
            Data_Wrap_Struct(0, 0, 0, (void *)&sha1));

Class Method Summary

Class - Inherited

.base64digest

Returns the base64 encoded hash value of a given string.

.digest

Returns the hash value of a given string.

.file

Creates a digest object and reads a given file, name.

.hexdigest

Returns the hex-encoded hash value of a given string.

.new

Instance Method Summary

Instance - Included

#<<

Updates the digest using a given string and returns self.

#==

If a string is given, checks whether it is equal to the hex-encoded hash value of the digest object.

#base64digest

If none is given, returns the resulting hash value of the digest in a base64 encoded form, keeping the digest’s state.

#base64digest!

Returns the resulting hash value and resets the digest to the initial state.

#block_length

Returns the block length of the digest.

#bubblebabble

Returns the resulting hash value in a Bubblebabble encoded form.

#digest

If none is given, returns the resulting hash value of the digest, keeping the digest’s state.

#digest!

Returns the resulting hash value and resets the digest to the initial state.

#digest_length

Returns the length of the hash value of the digest.

#file

Updates the digest with the contents of a given file name and returns self.

#hexdigest

If none is given, returns the resulting hash value of the digest in a hex-encoded form, keeping the digest’s state.

#hexdigest!

Returns the resulting hash value in a hex-encoded form and resets the digest to the initial state.

#inspect

Creates a printable version of the digest object.

#length

Returns digest_obj.digest_length().

#new

Returns a new, initialized copy of the digest object.

#reset

Resets the digest to the initial state and returns self.

#size

Alias for Instance#length.

#to_s

Returns digest_obj.hexdigest().

#update

Alias for Instance#<<.

#finish

Finishes the digest and returns the resulting hash value.

Constructor Details

This class inherits a constructor from Digest::Class

Instance Method Details

#update(string) ⇒ Base #<<(string) ⇒ Base
Also known as: #update

Update the digest using given string and return self.

[ GitHub ]

  
# File 'ext/digest/digest.c', line 678

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_lengthInteger

Return the block length of the digest in bytes.

[ GitHub ]

  
# File 'ext/digest/digest.c', line 736

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_lengthInteger

Return the length of the hash value in bytes.

[ GitHub ]

  
# File 'ext/digest/digest.c', line 721

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);
}

#finish (private)

This method is for internal use only.
[ GitHub ]

  
# File 'ext/digest/digest.c', line 696

static VALUE
rb_digest_base_finish(VALUE self)
{
    rb_digest_metadata_t *algo;
    void *pctx;
    VALUE str;

    algo = get_digest_obj_metadata(self);

    TypedData_Get_Struct(self, void, &digest_type, pctx);

    str = rb_str_new(0, algo->digest_len);
    algo->finish_func(pctx, (unsigned char *)RSTRING_PTR(str));

    /* avoid potential coredump caused by use of a finished context */
    algo_init(algo, pctx);

    return str;
}

#initialize_copy(obj)

This method is for internal use only.
[ GitHub ]

  
# File 'ext/digest/digest.c', line 630

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;
}

#resetBase

Reset the digest to its initial state and return self.

[ GitHub ]

  
# File 'ext/digest/digest.c', line 656

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) ⇒ Base #<<(string) ⇒ Base

Alias for #<<.