Class: CryptoPP::Digest

Inherits:
Object
  • Object
show all
Defined in:
ext/cryptopp.cpp

Direct Known Subclasses

HMAC

Instance Method Summary collapse

Instance Method Details

#==(compare) ⇒ Object

Compares a Digest directly to a String. We’ll attempt to detect whether or not the String is in binary or hex based on the number of characters in it – if it’s exactly double the expected number of bytes, then we’ll assume we’ve got a hex String.



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'ext/digests.cpp', line 552

VALUE rb_digest_equals(VALUE self, VALUE compare)
{
  JHash *hash = NULL;
  VALUE str1, str2;
  Check_Type(compare, T_STRING);
  Data_Get_Struct(self, JHash, hash);
  if (RSTRING_LEN(compare) == ((long) hash->getDigestSize() / 2)) {
    str1 = rb_str_new(hash->getHashtext(false).data(), hash->getHashtext(false).length());
    str2 = compare;
  }
  else if (RSTRING_LEN(compare) == ((long) hash->getDigestSize())) {
    str1 = rb_str_new(hash->getHashtext(true).data(), hash->getHashtext(true).length());
    str2 = rb_funcall(compare, rb_intern("downcase"), 0);
  }
  else {
    rb_raise(rb_eCryptoPP_Error, "expected %d bytes, got %ld", hash->getDigestSize() / 2, RSTRING_LEN(compare));
  }

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

#algorithm_nameString

Returns the name of the algorithm being used.

Returns:

  • (String)


753
754
755
756
757
758
# File 'ext/digests.cpp', line 753

VALUE rb_digest_algorithm_name(VALUE self)
{
  JHash *hash = NULL;
  Data_Get_Struct(self, JHash, hash);
  return rb_module_digest_name(self, INT2NUM(hash->getHashType()));
}

#calculateString

Calculates the digest and returns the result in binary.

Returns:

  • (String)


475
476
477
478
479
# File 'ext/digests.cpp', line 475

VALUE rb_digest_calculate(VALUE self)
{
  string retval = digest_calculate(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#calculate_hexString

Calculates the digest and returns the result in hex.

Returns:

  • (String)


487
488
489
490
491
# File 'ext/digests.cpp', line 487

VALUE rb_digest_calculate_hex(VALUE self)
{
  string retval = digest_calculate(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#clearObject

Clears a Digest’s plaintext and hashtext.



764
765
766
767
768
769
770
# File 'ext/digests.cpp', line 764

VALUE rb_digest_clear(VALUE self)
{
  JHash *hash = NULL;
  Data_Get_Struct(self, JHash, hash);
  hash->clear();
  return Qnil;
}

#digestString

Returns the digested text in binary.

Returns:

  • (String)


373
374
375
376
377
# File 'ext/digests.cpp', line 373

VALUE rb_digest_digest(VALUE self)
{
  string retval = digest_digest(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#digest=(bin) ⇒ Object

Sets the digest text on a Digest in binary.



510
511
512
513
514
# File 'ext/digests.cpp', line 510

VALUE rb_digest_digest_eq(VALUE self, VALUE digest)
{
  digest_digest_eq(self, digest, false);
  return digest;
}

#digest_hexString Also known as: hexdigest

Returns the digested text in hex.

Returns:

  • (String)


385
386
387
388
389
# File 'ext/digests.cpp', line 385

VALUE rb_digest_digest_hex(VALUE self)
{
  string retval = digest_digest(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#digest_hex=(hex) ⇒ Object Also known as: hexdigest=

Sets the digest text on a Digest in hex.



522
523
524
525
526
# File 'ext/digests.cpp', line 522

VALUE rb_digest_digest_hex_eq(VALUE self, VALUE digest)
{
  digest_digest_eq(self, digest, true);
  return digest;
}

#digest_ioString

Instance version of CryptoPP#digest_io.

Returns:

  • (String)


811
812
813
814
815
# File 'ext/digests.cpp', line 811

VALUE rb_digest_digest_io(VALUE self, VALUE io)
{
  string retval = digest_digest_io(self, io, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#digest_io_hexString

Instance version of CryptoPP#digest_io_hex.

Returns:

  • (String)


823
824
825
826
827
# File 'ext/digests.cpp', line 823

VALUE rb_digest_digest_io_hex(VALUE self, VALUE io)
{
  string retval = digest_digest_io(self, io, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#inspectString

Inspect method.

Returns:

  • (String)


535
536
537
538
539
540
541
542
543
# File 'ext/digests.cpp', line 535

VALUE rb_digest_inspect(VALUE self)
{
  JHash* hash = NULL;
  string retval;
  string cname = rb_obj_classname(self);
  Data_Get_Struct(self, JHash, hash);
  retval = "#<" + cname + ": " + hash->getHashtext(true) + ">";
  return rb_str_new(retval.c_str(), retval.length());
}

#plaintextString

Returns the plaintext used to generate the digest in binary.

Returns:

  • (String)


406
407
408
409
410
# File 'ext/digests.cpp', line 406

VALUE rb_digest_plaintext(VALUE self)
{
  string retval = digest_plaintext(self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#plaintext=(plaintext) ⇒ Object

Sets the plaintext on a Digest in binary.



441
442
443
444
445
# File 'ext/digests.cpp', line 441

VALUE rb_digest_plaintext_eq(VALUE self, VALUE plaintext)
{
  digest_plaintext_eq(self, plaintext, false);
  return plaintext;
}

#plaintext_hexString

Returns the plaintext used to generate the digest in hex.

Returns:

  • (String)


418
419
420
421
422
# File 'ext/digests.cpp', line 418

VALUE rb_digest_plaintext_hex(VALUE self)
{
  string retval = digest_plaintext(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#plaintext=(plaintext) ⇒ Object

Sets the plaintext on a Digest in hex.



453
454
455
456
457
# File 'ext/digests.cpp', line 453

VALUE rb_digest_plaintext_hex_eq(VALUE self, VALUE plaintext)
{
  digest_plaintext_eq(self, plaintext, true);
  return plaintext;
}

#digest_hexString

Returns the digested text in hex.

Returns:

  • (String)


385
386
387
388
389
# File 'ext/digests.cpp', line 385

VALUE rb_digest_digest_hex(VALUE self)
{
  string retval = digest_digest(self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

#update(plaintext) ⇒ String Also known as: <<

Updates the plaintext on a Digest and returns the new digested text.

Returns:

  • (String)


348
349
350
351
352
353
354
355
356
# File 'ext/digests.cpp', line 348

VALUE rb_digest_update(VALUE self, VALUE plaintext)
{
  JHash *hash = NULL;
  Check_Type(plaintext, T_STRING);
  Data_Get_Struct(self, JHash, hash);
  hash->updatePlaintext(string(StringValuePtr(plaintext), RSTRING_LEN(plaintext)));
  hash->hash();
  return rb_tainted_str_new(hash->getHashtext().data(), hash->getHashtext().length());
}

#validateBoolean Also known as: valid?

Validates if the digest text is a valid digest for plaintext.

Returns:

  • (Boolean)


779
780
781
782
783
784
785
786
787
788
789
# File 'ext/digests.cpp', line 779

VALUE rb_digest_validate(VALUE self)
{
  JHash *hash = NULL;
  Data_Get_Struct(self, JHash, hash);
  if (hash->validate()) {
    return Qtrue;
  }
  else {
    return Qfalse;
  }
}