Sha256: 4ce90a2883758cf2b72ccfcd3d7956ca186a7aed9a3a307214249326ee0184b9

Contents?: true

Size: 1.5 KB

Versions: 63

Compression:

Stored size: 1.5 KB

Contents

#include <ruby.h>
#include <ow-crypt.h>

static VALUE mBCrypt;
static VALUE cBCryptEngine;

/* Given a logarithmic cost parameter, generates a salt for use with +bc_crypt+.
*/
static VALUE bc_salt(VALUE self, VALUE prefix, VALUE count, VALUE input) {
    char * salt;
    VALUE str_salt;

    salt = crypt_gensalt_ra(
	    StringValuePtr(prefix),
	    NUM2ULONG(count),
	    NIL_P(input) ? NULL : StringValuePtr(input),
	    NIL_P(input) ? 0 : RSTRING_LEN(input));

    if(!salt) return Qnil;

    str_salt = rb_str_new2(salt);
    xfree(salt);

    return str_salt;
}

/* Given a secret and a salt, generates a salted hash (which you can then store safely).
*/
static VALUE bc_crypt(VALUE self, VALUE key, VALUE setting) {
    char * value;
    void * data;
    int size;
    VALUE out;

    data = NULL;
    size = 0xDEADBEEF;

    if(NIL_P(key) || NIL_P(setting)) return Qnil;

    value = crypt_ra(
	    NIL_P(key) ? NULL : StringValuePtr(key),
	    NIL_P(setting) ? NULL : StringValuePtr(setting),
	    &data,
	    &size);

    if(!value) return Qnil;

    out = rb_str_new(data, size - 1);

    xfree(data);

    return out;
}

/* Create the BCrypt and BCrypt::Engine modules, and populate them with methods. */
void Init_bcrypt_ext(){
    mBCrypt = rb_define_module("BCrypt");
    cBCryptEngine = rb_define_class_under(mBCrypt, "Engine", rb_cObject);

    rb_define_singleton_method(cBCryptEngine, "__bc_salt", bc_salt, 3);
    rb_define_singleton_method(cBCryptEngine, "__bc_crypt", bc_crypt, 2);
}

/* vim: set noet sws=4 sw=4: */

Version data entries

63 entries across 62 versions & 8 rubygems

Version Path
bcrypt4-4.1.0 ext/mri/bcrypt_ext.c
bcrypt4-4.0.2 ext/mri/bcrypt_ext.c
bcrypt4-4.0.1 ext/mri/bcrypt_ext.c
bcrypt4-4.0.0 ext/mri/bcrypt_ext.c
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/bcrypt-3.1.11/ext/mri/bcrypt_ext.c
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/bcrypt-ruby-3.1.1.rc1/ext/mri/bcrypt_ext.c
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/bcrypt-3.1.11/ext/mri/bcrypt_ext.c
bcrypt-3.1.11-x86-mingw32 ext/mri/bcrypt_ext.c
bcrypt-3.1.11-x64-mingw32 ext/mri/bcrypt_ext.c
bcrypt-3.1.11-java ext/mri/bcrypt_ext.c
bcrypt-3.1.11 ext/mri/bcrypt_ext.c
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/bcrypt-3.1.10/ext/mri/bcrypt_ext.c
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/bcrypt-3.1.10/ext/mri/bcrypt_ext.c
lookout-bcrypt-3.2.1-java ext/mri/bcrypt_ext.c
lookout-bcrypt-3.2.1 ext/mri/bcrypt_ext.c
bcrypt-3.1.10-x86-mingw32 ext/mri/bcrypt_ext.c
bcrypt-3.1.10-x64-mingw32 ext/mri/bcrypt_ext.c
bcrypt-3.1.10-java ext/mri/bcrypt_ext.c
bcrypt-3.1.10 ext/mri/bcrypt_ext.c
lookout-bcrypt-3.2.0-java ext/mri/bcrypt_ext.c