Module: Digest::Instance

Included in:
Class
Defined in:
digest.c,
lib/digest.rb,
digest.c,
bubblebabble/bubblebabble.c

Overview

This module provides instance methods for a digest implementation object to calculate message digest values.

Instance Method Summary collapse

Instance Method Details

#update(string) ⇒ Object #<<(string) ⇒ Object

Updates the digest using a given string and returns self.

The update() method and the left-shift operator are overridden by each implementation subclass. (One should be an alias for the other)



171
172
173
174
175
176
177
# File 'digest.c', line 171

static VALUE
rb_digest_instance_update(VALUE self, VALUE str)
{
    rb_digest_instance_method_unimpl(self, "update");

    UNREACHABLE;
}

#==(another_digest_obj) ⇒ Boolean #==(string) ⇒ Boolean

If a string is given, checks whether it is equal to the hex-encoded hash value of the digest object. If another digest instance is given, checks whether they have the same hash value. Otherwise returns false.

Overloads:

  • #==(another_digest_obj) ⇒ Boolean

    Returns:

    • (Boolean)
  • #==(string) ⇒ Boolean

    Returns:

    • (Boolean)


367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'digest.c', line 367

static VALUE
rb_digest_instance_equal(VALUE self, VALUE other)
{
    VALUE str1, str2;

    if (rb_obj_is_kind_of(other, rb_mDigest_Instance) == Qtrue) {
        str1 = rb_digest_instance_digest(0, 0, self);
        str2 = rb_digest_instance_digest(0, 0, other);
    } else {
        str1 = rb_digest_instance_to_s(self);
        str2 = rb_check_string_type(other);
        if (NIL_P(str2)) return Qfalse;
    }

    /* never blindly assume that subclass methods return strings */
    StringValue(str1);
    StringValue(str2);

    if (RSTRING_LEN(str1) == RSTRING_LEN(str2) &&
	rb_str_cmp(str1, str2) == 0) {
	return Qtrue;
    }
    return Qfalse;
}

#base64digest(str = nil) ⇒ Object

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

If a string is given, returns the hash value for the given string in a base64 encoded form, resetting the digest to the initial state before and after the process.

In either case, the return value is properly padded with ‘=’ and contains no line feeds.



68
69
70
# File 'lib/digest.rb', line 68

def base64digest(str = nil)
  [str ? digest(str) : digest].pack('m0')
end

#base64digest!Object

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



74
75
76
# File 'lib/digest.rb', line 74

def base64digest!
  [digest!].pack('m0')
end

#block_lengthInteger

Returns the block length of the digest.

This method is overridden by each implementation subclass.

Returns:

  • (Integer)


433
434
435
436
437
438
439
# File 'digest.c', line 433

static VALUE
rb_digest_instance_block_length(VALUE self)
{
    rb_digest_instance_method_unimpl(self, "block_length");

    UNREACHABLE;
}

#bubblebabbleObject

call-seq:

digest_obj.bubblebabble -> hash_string

Returns the resulting hash value in a Bubblebabble encoded form.



114
115
116
117
118
# File 'bubblebabble/bubblebabble.c', line 114

static VALUE
rb_digest_instance_bubblebabble(VALUE self)
{
    return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
}

#digestString #digest(string) ⇒ String

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

If a string is given, returns the hash value for the given string, resetting the digest to the initial state before and after the process.

Overloads:

  • #digestString

    Returns:

    • (String)
  • #digest(string) ⇒ String

    Returns:

    • (String)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'digest.c', line 242

static VALUE
rb_digest_instance_digest(int argc, VALUE *argv, VALUE self)
{
    VALUE str, value;

    if (rb_scan_args(argc, argv, "01", &str) > 0) {
        rb_funcall(self, id_reset, 0);
        rb_funcall(self, id_update, 1, str);
        value = rb_funcall(self, id_finish, 0);
        rb_funcall(self, id_reset, 0);
    } else {
        value = rb_funcall(rb_obj_clone(self), id_finish, 0);
    }

    return value;
}

#digest!String

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

Returns:

  • (String)


266
267
268
269
270
271
272
273
# File 'digest.c', line 266

static VALUE
rb_digest_instance_digest_bang(VALUE self)
{
    VALUE value = rb_funcall(self, id_finish, 0);
    rb_funcall(self, id_reset, 0);

    return value;
}

#digest_lengthInteger

Returns the length of the hash value of the digest.

This method should be overridden by each implementation subclass. If not, digest_obj.digest().length() is returned.

Returns:

  • (Integer)


401
402
403
404
405
406
407
408
409
410
# File 'digest.c', line 401

static VALUE
rb_digest_instance_digest_length(VALUE self)
{
    /* subclasses really should redefine this method */
    VALUE digest = rb_digest_instance_digest(0, 0, self);

    /* never blindly assume that #digest() returns a string */
    StringValue(digest);
    return INT2NUM(RSTRING_LEN(digest));
}

#file(name) ⇒ Object

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



49
50
51
52
53
54
55
56
57
# File 'lib/digest.rb', line 49

def file(name)
  File.open(name, "rb") {|f|
    buf = ""
    while f.read(16384, buf)
      update buf
    end
  }
  self
end

#hexdigestString #hexdigest(string) ⇒ String

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

If a string is given, returns the hash value for the given string in a hex-encoded form, resetting the digest to the initial state before and after the process.

Overloads:

  • #hexdigestString

    Returns:

    • (String)
  • #hexdigest(string) ⇒ String

    Returns:

    • (String)


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'digest.c', line 287

static VALUE
rb_digest_instance_hexdigest(int argc, VALUE *argv, VALUE self)
{
    VALUE str, value;

    if (rb_scan_args(argc, argv, "01", &str) > 0) {
        rb_funcall(self, id_reset, 0);
        rb_funcall(self, id_update, 1, str);
        value = rb_funcall(self, id_finish, 0);
        rb_funcall(self, id_reset, 0);
    } else {
        value = rb_funcall(rb_obj_clone(self), id_finish, 0);
    }

    return hexencode_str_new(value);
}

#hexdigest!String

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

Returns:

  • (String)


311
312
313
314
315
316
317
318
# File 'digest.c', line 311

static VALUE
rb_digest_instance_hexdigest_bang(VALUE self)
{
    VALUE value = rb_funcall(self, id_finish, 0);
    rb_funcall(self, id_reset, 0);

    return hexencode_str_new(value);
}

#inspectString

Creates a printable version of the digest object.

Returns:

  • (String)


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'digest.c', line 338

static VALUE
rb_digest_instance_inspect(VALUE self)
{
    VALUE str;
    size_t digest_len = 32;	/* about this size at least */
    const char *cname;

    cname = rb_obj_classname(self);

    /* #<Digest::ClassName: xxxxx...xxxx> */
    str = rb_str_buf_new(2 + strlen(cname) + 2 + digest_len * 2 + 1);
    rb_str_buf_cat2(str, "#<");
    rb_str_buf_cat2(str, cname);
    rb_str_buf_cat2(str, ": ");
    rb_str_buf_append(str, rb_digest_instance_hexdigest(0, 0, self));
    rb_str_buf_cat2(str, ">");
    return str;
}

#lengthInteger #sizeInteger

Returns digest_obj.digest_length().

Overloads:

  • #lengthInteger

    Returns:

    • (Integer)
  • #sizeInteger

    Returns:

    • (Integer)


419
420
421
422
423
# File 'digest.c', line 419

static VALUE
rb_digest_instance_length(VALUE self)
{
    return rb_funcall(self, id_digest_length, 0);
}

#newObject

Returns a new, initialized copy of the digest object. Equivalent to digest_obj.clone().reset().



222
223
224
225
226
227
228
# File 'digest.c', line 222

static VALUE
rb_digest_instance_new(VALUE self)
{
    VALUE clone = rb_obj_clone(self);
    rb_funcall(clone, id_reset, 0);
    return clone;
}

#resetObject

Resets the digest to the initial state and returns self.

This method is overridden by each implementation subclass.



207
208
209
210
211
212
213
# File 'digest.c', line 207

static VALUE
rb_digest_instance_reset(VALUE self)
{
    rb_digest_instance_method_unimpl(self, "reset");

    UNREACHABLE;
}

#lengthInteger #sizeInteger

Returns digest_obj.digest_length().

Overloads:

  • #lengthInteger

    Returns:

    • (Integer)
  • #sizeInteger

    Returns:

    • (Integer)


419
420
421
422
423
# File 'digest.c', line 419

static VALUE
rb_digest_instance_length(VALUE self)
{
    return rb_funcall(self, id_digest_length, 0);
}

#to_sString

Returns digest_obj.hexdigest().

Returns:

  • (String)


326
327
328
329
330
# File 'digest.c', line 326

static VALUE
rb_digest_instance_to_s(VALUE self)
{
    return rb_funcall(self, id_hexdigest, 0);
}

#update(string) ⇒ Object #<<(string) ⇒ Object

Updates the digest using a given string and returns self.

The update() method and the left-shift operator are overridden by each implementation subclass. (One should be an alias for the other)



171
172
173
174
175
176
177
# File 'digest.c', line 171

static VALUE
rb_digest_instance_update(VALUE self, VALUE str)
{
    rb_digest_instance_method_unimpl(self, "update");

    UNREACHABLE;
}