Module: Digest::Instance
- Included in:
- Class
- Defined in:
- digest.c,
lib/digest.rb,
digest.c
Overview
This module provides instance methods for a digest implementation object to calculate message digest values.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Updates the digest using a given string and returns self.
-
#==(other) ⇒ Object
If a string is given, checks whether it is equal to the hex-encoded hash value of the digest object.
-
#block_length ⇒ Integer
Returns the block length of the digest.
-
#bubblebabble ⇒ Object
Returns the resulting hash value in a Bubblebabble encoded form.
-
#digest(*args) ⇒ Object
If none is given, returns the resulting hash value of the digest, keeping the digest’s state.
-
#digest! ⇒ String
Returns the resulting hash value and resets the digest to the initial state.
-
#digest_length ⇒ Integer
Returns the length of the hash value of the digest.
-
#file(name) ⇒ Object
updates the digest with the contents of a given file name and returns self.
-
#hexdigest(*args) ⇒ Object
If none is given, returns the resulting hash value of the digest in a hex-encoded form, keeping the digest’s state.
-
#hexdigest! ⇒ String
Returns the resulting hash value and resets the digest to the initial state.
-
#inspect ⇒ String
Creates a printable version of the digest object.
-
#length ⇒ Object
Returns digest_obj.digest_length().
-
#new ⇒ Object
Returns a new, initialized copy of the digest object.
-
#reset ⇒ Object
Resets the digest to the initial state and returns self.
-
#size ⇒ Object
Returns digest_obj.digest_length().
-
#to_s ⇒ String
Returns digest_obj.hexdigest().
-
#update(str) ⇒ Object
Updates the digest using a given string and returns self.
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)
97 98 99 100 101 |
# File 'digest.c', line 97
static VALUE
rb_digest_instance_update(VALUE self, VALUE str)
{
rb_raise(rb_eRuntimeError, "%s does not implement update()", RSTRING_PTR(rb_inspect(self)));
}
|
#==(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.
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'digest.c', line 293
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 = other;
}
/* 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;
}
|
#block_length ⇒ Integer
Returns the block length of the digest.
This method is overridden by each implementation subclass.
358 359 360 361 362 |
# File 'digest.c', line 358
static VALUE
rb_digest_instance_block_length(VALUE self)
{
rb_raise(rb_eRuntimeError, "%s does not implement block_length()", RSTRING_PTR(rb_inspect(self)));
}
|
#bubblebabble ⇒ Object
Returns the resulting hash value in a Bubblebabble encoded form.
111 112 113 114 115 |
# File 'bubblebabble/bubblebabble.c', line 111
static VALUE
rb_digest_instance_bubblebabble(VALUE self)
{
return bubblebabble_str_new(rb_funcall(self, id_digest, 0));
}
|
#digest ⇒ String #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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'digest.c', line 162
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 clone = rb_obj_clone(self);
value = rb_funcall(clone, id_finish, 0);
rb_funcall(clone, id_reset, 0);
}
return value;
}
|
#digest! ⇒ String
Returns the resulting hash value and resets the digest to the initial state.
189 190 191 192 193 194 195 196 |
# File 'digest.c', line 189
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_length ⇒ Integer
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.
326 327 328 329 330 331 332 333 334 335 |
# File 'digest.c', line 326
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.
36 37 38 39 40 41 42 43 44 |
# File 'lib/digest.rb', line 36 def file(name) File.open(name, "rb") {|f| buf = "" while f.read(16384, buf) update buf end } self end |
#hexdigest ⇒ String #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.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'digest.c', line 210
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 clone = rb_obj_clone(self);
value = rb_funcall(clone, id_finish, 0);
rb_funcall(clone, id_reset, 0);
}
return hexencode_str_new(value);
}
|
#hexdigest! ⇒ String
Returns the resulting hash value and resets the digest to the initial state.
237 238 239 240 241 242 243 244 |
# File 'digest.c', line 237
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);
}
|
#inspect ⇒ String
Creates a printable version of the digest object.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'digest.c', line 264
static VALUE
rb_digest_instance_inspect(VALUE self)
{
VALUE str;
size_t digest_len = 32; /* about this size at least */
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;
}
|
#length ⇒ Integer #size ⇒ Integer
Returns digest_obj.digest_length().
344 345 346 347 348 |
# File 'digest.c', line 344
static VALUE
rb_digest_instance_length(VALUE self)
{
return rb_funcall(self, id_digest_length, 0);
}
|
#new ⇒ Object
Returns a new, initialized copy of the digest object. Equivalent to digest_obj.clone().reset().
142 143 144 145 146 147 148 |
# File 'digest.c', line 142
static VALUE
rb_digest_instance_new(VALUE self)
{
VALUE clone = rb_obj_clone(self);
rb_funcall(clone, id_reset, 0);
return clone;
}
|
#reset ⇒ Object
Resets the digest to the initial state and returns self.
This method is overridden by each implementation subclass.
129 130 131 132 133 |
# File 'digest.c', line 129
static VALUE
rb_digest_instance_reset(VALUE self)
{
rb_raise(rb_eRuntimeError, "%s does not implement reset()", RSTRING_PTR(rb_inspect(self)));
}
|
#length ⇒ Integer #size ⇒ Integer
Returns digest_obj.digest_length().
344 345 346 347 348 |
# File 'digest.c', line 344
static VALUE
rb_digest_instance_length(VALUE self)
{
return rb_funcall(self, id_digest_length, 0);
}
|
#to_s ⇒ String
Returns digest_obj.hexdigest().
252 253 254 255 256 |
# File 'digest.c', line 252
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)
97 98 99 100 101 |
# File 'digest.c', line 97
static VALUE
rb_digest_instance_update(VALUE self, VALUE str)
{
rb_raise(rb_eRuntimeError, "%s does not implement update()", RSTRING_PTR(rb_inspect(self)));
}
|