Module: CryptoPP

Defined in:
ext/cryptopp.cpp

Defined Under Namespace

Classes: Cipher, CryptoPPError, Digest, HMAC

Constant Summary collapse

VERSION =
rb_str_new2(CRYPTOPP_EXT_VERSION(EXT_VERSION_CODE))
CRYPTOPP_VERSION =
INT2NUM(CRYPTOPP_VERSION)

Class Method Summary collapse

Class Method Details

.block_mode_name(block_mode) ⇒ String

Singleton method to return the name of a block cipher mode.

Returns:

  • (String)


1231
1232
1233
1234
# File 'ext/ciphers.cpp', line 1231

VALUE rb_module_block_mode_name(VALUE self, VALUE m)
{
  return rb_tainted_str_new2(JCipher::getModeName(mode_sym_to_const(m)).c_str());
}

.cipher_enabled?(algorithm) ⇒ Boolean

Singleton method to check for the availability of a cipher algorithm.

Returns:

  • (Boolean)


1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
# File 'ext/ciphers.cpp', line 1343

VALUE rb_module_cipher_enabled(VALUE self, VALUE c)
{
  switch (cipher_sym_to_const(c)) {
    default:
      return Qfalse;

#  define CIPHER_ALGORITHM_X(klass, r, c, s) \
    case r ## _CIPHER:
#  include "defs/ciphers.def"
      return Qtrue;
  }
}

.cipher_factory(algorithm) ⇒ Cipher .cipher_factory(algorithm, options) ⇒ Cipher

Creates a new Cipher object.

See the Cipher class for available options.

Overloads:

  • .cipher_factory(algorithm) ⇒ Cipher

    Returns:

  • .cipher_factory(algorithm, options) ⇒ Cipher

    Returns:



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/ciphers.cpp', line 327

VALUE rb_module_cipher_factory(int argc, VALUE *argv, VALUE self)
{
  VALUE algorithm, options, retval;
  rb_scan_args(argc, argv, "11", &algorithm, &options);
  try {
    retval = wrap_cipher_in_ruby(cipher_factory(algorithm));
  }
  catch (Exception& e) {
    rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
  }
  if (!NIL_P(options)) {
    cipher_options(retval, options);
  }
  return retval;
}

.cipher_listArray

Returns an Array of available ciphers.

Returns:

  • (Array)


1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
# File 'ext/ciphers.cpp', line 1393

VALUE rb_module_cipher_list(VALUE self)
{
  VALUE ary = rb_ary_new();

#  define CIPHER_ALGORITHM_X(klass, r, c, s) \
    rb_ary_push(ary, ID2SYM(rb_intern(# s)));
#  include "defs/ciphers.def"

  return ary;
}

.cipher_name(algorithm) ⇒ String

Returns the name of a Cipher.

Returns:

  • (String)


1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
# File 'ext/ciphers.cpp', line 1196

VALUE rb_module_cipher_name(VALUE self, VALUE c)
{
  switch (cipher_sym_to_const(c)) {
    default:
      rb_raise(rb_eCryptoPP_Error, "could not find a valid cipher type");
    break;

#    define CIPHER_ALGORITHM_X(klass, r, c, s) \
      case r ## _CIPHER: \
        return rb_tainted_str_new2(c::getStaticCipherName().c_str());
#    include "defs/ciphers.def"
  }
}

.digest(algorithm, plaintext) ⇒ String

Digest the plaintext and returns the result in binary.

Returns:

  • (String)


625
626
627
628
629
# File 'ext/digests.cpp', line 625

VALUE rb_module_digest(int argc, VALUE *argv, VALUE self)
{
  string retval = module_digest(argc, argv, self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

.digest_enabled?Boolean

Is a Digest/HMAC algorithm available?

Returns:

  • (Boolean)


710
711
712
713
714
715
716
717
718
# File 'ext/digests.cpp', line 710

VALUE rb_module_digest_enabled(VALUE self, VALUE d)
{
  if (digest_enabled(digest_sym_to_const(d))) {
    return Qtrue;
  }
  else {
    return Qfalse;
  }
}

.digest_factory(algorithm) ⇒ CryptoPP::Digest .digest_factory(algorithm, plaintext) ⇒ CryptoPP::Digest .digest_factory(algorithm, options) ⇒ CryptoPP::Digest

Creates a new Digest object.

See the Digest class for available options.

Overloads:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'ext/digests.cpp', line 247

VALUE rb_module_digest_factory(int argc, VALUE *argv, VALUE self)
{
  JHash* hash = NULL;
  VALUE algorithm, options, retval;

  rb_scan_args(argc, argv, "11", &algorithm, &options);
  {
    HashEnum a = digest_sym_to_const(algorithm);
    if (!digest_is_non_hmac(a)) {
      rb_raise(rb_eCryptoPP_Error, "invalid digest algorithm");
    }
    else {
      try {
        hash = digest_factory(algorithm);
        retval = wrap_digest_in_ruby(hash);
      }
      catch (Exception& e) {
        if (hash != NULL) {
          delete hash;
        }
        rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
      }
      if (argc == 2) {
        if (TYPE(options) == T_STRING) {
          rb_digest_plaintext_eq(retval, options);
          hash->hash();
        }
        else {
          digest_options(retval, options);
        }
      }
      return retval;
    }
  }
}

.digest_hex(algorithm, plaintext) ⇒ String

Digest the plaintext and returns the result in hex.

Returns:

  • (String)


637
638
639
640
641
# File 'ext/digests.cpp', line 637

VALUE rb_module_digest_hex(int argc, VALUE *argv, VALUE self)
{
  string retval = module_digest(argc, argv, self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

.digest(algorithm, plaintext) ⇒ String .digest(algorithm, plaintext, key) ⇒ String

Singleton method for digesting with a HMAC. The plaintext and key values are in binary and the return value is in binary.

Overloads:

  • .digest(algorithm, plaintext) ⇒ String

    Returns:

    • (String)
  • .digest(algorithm, plaintext, key) ⇒ String

    Returns:

    • (String)


1117
1118
1119
1120
1121
# File 'ext/digests.cpp', line 1117

VALUE rb_module_hmac_digest(int argc, VALUE *argv, VALUE self)
{
  string retval = module_hmac_digest(argc, argv, self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

.digest_hex(algorithm, plaintext) ⇒ String .digest_hex(algorithm, plaintext, key) ⇒ String

Singleton method for digesting with a HMAC. The plaintext and key values are in binary and the return value is in hex.

Overloads:

  • .digest_hex(algorithm, plaintext) ⇒ String

    Returns:

    • (String)
  • .digest_hex(algorithm, plaintext, key) ⇒ String

    Returns:

    • (String)


1131
1132
1133
1134
1135
# File 'ext/digests.cpp', line 1131

VALUE rb_module_hmac_digest_hex(int argc, VALUE *argv, VALUE self)
{
  string retval = module_hmac_digest(argc, argv, self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

.digest_io(io) ⇒ String

Digests a Ruby IO object and spits out the result in binary. You can use any sort of Ruby object as long as it implements eof?, read, write and flush.

Example:

cipher.digest_io(File.open("http://example.com/"))

Returns:

  • (String)


679
680
681
682
683
# File 'ext/digests.cpp', line 679

VALUE rb_module_digest_io(int argc, VALUE *argv, VALUE self)
{
  string retval = module_digest_io(argc, argv, self, false);
  return rb_tainted_str_new(retval.data(), retval.length());
}

.digest_io_hex(io) ⇒ String

Digests a Ruby IO object and spits out the result in hex. You can use any sort of Ruby object as long as it implements eof?, read, write and flush.

Example:

cipher.digest_io_hex(File.open("http://example.com/"))

Returns:

  • (String)


697
698
699
700
701
# File 'ext/digests.cpp', line 697

VALUE rb_module_digest_io_hex(int argc, VALUE *argv, VALUE self)
{
  string retval = module_digest_io(argc, argv, self, true);
  return rb_tainted_str_new(retval.data(), retval.length());
}

.digest_listArray

Returns an Array of available Digest algorithms.

Returns:

  • (Array)


836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'ext/digests.cpp', line 836

VALUE rb_module_digest_list(VALUE self)
{
  VALUE ary;
  ary = rb_ary_new();

#  define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
    rb_ary_push(ary, INT2NUM(r ##_CHECKSUM));
#  include "defs/checksums.def"

#  define HASH_ALGORITHM_X(klass, r, c, s) \
    rb_ary_push(ary, INT2NUM(r ##_HASH));
#  include "defs/hashes.def"

  return ary;
}

.digest_name(h) ⇒ Object

Returns the name of a hash algorithm.



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'ext/digests.cpp', line 722

VALUE rb_module_digest_name(VALUE self, VALUE h)
{
  switch ((enum HashEnum) NUM2INT(h)) {
    default:
      rb_raise(rb_eCryptoPP_Error, "could not find a valid digest type");
    break;

#    define CHECKSUM_ALGORITHM_X(klass, r, c, s) \
      case r ## _CHECKSUM: \
        return rb_tainted_str_new2(c::getHashName().c_str());
#    include "defs/checksums.def"

#    define HASH_ALGORITHM_X(klass, r, c, s) \
      case r ## _HASH: \
        return rb_tainted_str_new2(c::getHashName().c_str());
#    include "defs/hashes.def"

#     define HMAC_ALGORITHM_X(klass, r, c, s) \
      case r ## _HMAC: \
        return rb_tainted_str_new2(c::getHashName().c_str());
#    include "defs/hmacs.def"
  }
}

.hmac_factory(algorithm) ⇒ CryptoPP::HMAC .hmac_factory(algorithm, plaintext, key) ⇒ CryptoPP::HMAC .hmac_factory(algorithm, options) ⇒ CryptoPP::HMAC

Creates a new HMAC object.

Overloads:



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'ext/digests.cpp', line 889

VALUE rb_module_hmac_factory(int argc, VALUE *argv, VALUE self)
{
  if (argc < 1 || argc > 3) {
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1-3)", argc);
  }
  else {
    JHash* hash = NULL;
    VALUE algorithm = argv[0];
    VALUE retval;

    if (!digest_is_hmac(digest_sym_to_const(algorithm))) {
      rb_raise(rb_eCryptoPP_Error, "invalid HMAC algorithm");
    }
    else {
      try {
        hash = digest_factory(algorithm);
        retval = wrap_digest_in_ruby(hash);
      }
      catch (Exception& e) {
        if (hash != NULL) {
          delete hash;
        }
        rb_raise(rb_eCryptoPP_Error, "%s", e.GetWhat().c_str());
      }
      if (argc >= 2) {
        if (TYPE(argv[1]) == T_STRING) {
          digest_plaintext_eq(retval, argv[1], false);
          if (argc == 3) {
            Check_Type(argv[2], T_STRING);
            digest_hmac_key_eq(retval, argv[2], false);
          }
          hash->hash();
        }
        else if (argc > 2) {
          rb_raise(rb_eArgError, "wrong argument types (expected a String or a Hash");
        }
        else {
          digest_hmac_options(retval, argv[1]);
        }
      }
      return retval;
    }
  }
}

.hmac_listArray

Returns an Array of available HMAC algorithms.

Returns:

  • (Array)


1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'ext/digests.cpp', line 1144

VALUE rb_module_hmac_list(VALUE self)
{
  VALUE ary;
  ary = rb_ary_new();

#  define HMAC_ALGORITHM_X(klass, r, c, s) \
    rb_ary_push(ary, ID2SYM(rb_intern(# s)));
#  include "defs/hmacs.def"

  return ary;
}

.padding_name(padding) ⇒ String

Singleton method to return the name of the a block cipher padding mode.

Returns:

  • (String)


1262
1263
1264
1265
# File 'ext/ciphers.cpp', line 1262

VALUE rb_module_padding_name(VALUE self, VALUE p)
{
  return rb_tainted_str_new2(JCipher::getPaddingName(padding_sym_to_const(p)).c_str());
}

.rng_available?(rng) ⇒ Boolean

Singleton method to check for the availability of a random number generator.

Returns:

  • (Boolean)


1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
# File 'ext/ciphers.cpp', line 1363

VALUE rb_module_rng_available(VALUE self, VALUE r)
{
  ID id = SYM2ID(r);
  if (id == rb_intern("rand")) {
    return Qtrue;
  }
#  ifdef NONBLOCKING_RNG_AVAILABLE
    else if (id == rb_intern("non_blocking")) {
      return Qtrue;
    }
#  endif

#  ifdef BLOCKING_RNG_AVAILABLE
    else if (id == rb_intern("blocking")) {
      return Qtrue;
    }
#  endif

  else {
    return Qfalse;
  }
}

.rng_name(rng) ⇒ String

Singleton method to return the name of the random number generator being used.

Returns:

  • (String)


1294
1295
1296
1297
# File 'ext/ciphers.cpp', line 1294

VALUE rb_module_rng_name(VALUE self, VALUE r)
{
  return rb_tainted_str_new2(JCipher::getRNGName(rng_sym_to_const(r)).c_str());
}