crypt.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <ruby.h>
  2. #include <ow-crypt.h>
  3. VALUE mCrypt;
  4. static VALUE crypt_salt(VALUE self, VALUE prefix, VALUE count, VALUE input)
  5. {
  6. char * salt;
  7. VALUE str_salt;
  8. salt = crypt_gensalt_ra(
  9. StringValuePtr(prefix),
  10. NUM2ULONG(count),
  11. NIL_P(input) ? NULL : StringValuePtr(input),
  12. NIL_P(input) ? 0 : RSTRING_LEN(input));
  13. if(!salt) return Qnil;
  14. str_salt = rb_str_new2(salt);
  15. free(salt);
  16. return str_salt;
  17. }
  18. static VALUE ra(VALUE self, VALUE key, VALUE setting)
  19. {
  20. char * value;
  21. void * data;
  22. int size;
  23. VALUE out;
  24. data = NULL;
  25. size = 0xDEADBEEF;
  26. if(NIL_P(key) || NIL_P(setting)) return Qnil;
  27. value = crypt_ra(
  28. NIL_P(key) ? NULL : StringValuePtr(key),
  29. NIL_P(setting) ? NULL : StringValuePtr(setting),
  30. &data,
  31. &size);
  32. if(!value) return Qnil;
  33. out = rb_str_new(data, size - 1);
  34. free(data);
  35. return out;
  36. }
  37. void Init_crypt()
  38. {
  39. mCrypt = rb_define_module("Crypt");
  40. rb_define_singleton_method(mCrypt, "salt", crypt_salt, 3);
  41. rb_define_singleton_method(mCrypt, "crypt", ra, 2);
  42. }