git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Tree: 03c07302c3f65146490bf60e9a39bd16fe1dcf67

Files: 03c07302c3f65146490bf60e9a39bd16fe1dcf67 / ext / bcrypt_ext.c

990 bytesRaw
1#include "ruby.h"
2
3char *bcrypt_gensalt(u_int8_t, u_int8_t *);
4char *bcrypt(const char *, const char *);
5
6VALUE mBCrypt;
7VALUE cBCryptEngine;
8
9/* Given a logarithmic cost parameter, generates a salt for use with +bc_crypt+.
10 */
11static VALUE bc_salt(VALUE self, VALUE cost, VALUE seed) {
12 return rb_str_new2((char *)bcrypt_gensalt(NUM2INT(cost), (u_int8_t *)RSTRING(seed)->ptr));
13}
14
15/* Given a secret and a salt, generates a salted hash (which you can then store safely).
16 */
17static VALUE bc_crypt(VALUE self, VALUE key, VALUE salt) {
18 return rb_str_new2((char *)bcrypt(RSTRING(key)->ptr, (char *)RSTRING(salt)->ptr));
19}
20
21/* Create the BCrypt and BCrypt::Internals modules, and populate them with methods. */
22void Init_bcrypt_ext(){
23 mBCrypt = rb_define_module("BCrypt");
24 cBCryptEngine = rb_define_class_under(mBCrypt, "Engine", rb_cObject);
25
26 rb_define_singleton_method(cBCryptEngine, "__bc_salt", bc_salt, 2);
27 rb_define_singleton_method(cBCryptEngine, "__bc_crypt", bc_crypt, 2);
28}
29

Built with git-ssb-web