lookout-bcrypt 3.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
1
+ #include <ruby.h>
2
+ #include <ow-crypt.h>
3
+
4
+ static VALUE mBCrypt;
5
+ static VALUE cBCryptEngine;
6
+
7
+ /* Given a logarithmic cost parameter, generates a salt for use with +bc_crypt+.
8
+ */
9
+ static VALUE bc_salt(VALUE self, VALUE prefix, VALUE count, VALUE input) {
10
+ char * salt;
11
+ VALUE str_salt;
12
+
13
+ salt = crypt_gensalt_ra(
14
+ StringValuePtr(prefix),
15
+ NUM2ULONG(count),
16
+ NIL_P(input) ? NULL : StringValuePtr(input),
17
+ NIL_P(input) ? 0 : RSTRING_LEN(input));
18
+
19
+ if(!salt) return Qnil;
20
+
21
+ str_salt = rb_str_new2(salt);
22
+ xfree(salt);
23
+
24
+ return str_salt;
25
+ }
26
+
27
+ /* Given a secret and a salt, generates a salted hash (which you can then store safely).
28
+ */
29
+ static VALUE bc_crypt(VALUE self, VALUE key, VALUE setting) {
30
+ char * value;
31
+ void * data;
32
+ int size;
33
+ VALUE out;
34
+
35
+ data = NULL;
36
+ size = 0xDEADBEEF;
37
+
38
+ if(NIL_P(key) || NIL_P(setting)) return Qnil;
39
+
40
+ value = crypt_ra(
41
+ NIL_P(key) ? NULL : StringValuePtr(key),
42
+ NIL_P(setting) ? NULL : StringValuePtr(setting),
43
+ &data,
44
+ &size);
45
+
46
+ if(!value) return Qnil;
47
+
48
+ out = rb_str_new(data, size - 1);
49
+
50
+ xfree(data);
51
+
52
+ return out;
53
+ }
54
+
55
+ /* Create the BCrypt and BCrypt::Engine modules, and populate them with methods. */
56
+ void Init_bcrypt_ext(){
57
+ mBCrypt = rb_define_module("BCrypt");
58
+ cBCryptEngine = rb_define_class_under(mBCrypt, "Engine", rb_cObject);
59
+
60
+ rb_define_singleton_method(cBCryptEngine, "__bc_salt", bc_salt, 3);
61
+ rb_define_singleton_method(cBCryptEngine, "__bc_crypt", bc_crypt, 2);
62
+ }
63
+
64
+ /* vim: set noet sws=4 sw=4: */
data/ext/mri/crypt.c ADDED
@@ -0,0 +1,57 @@
1
+ #include <ruby.h>
2
+ #include <ow-crypt.h>
3
+
4
+ VALUE mCrypt;
5
+
6
+ static VALUE crypt_salt(VALUE self, VALUE prefix, VALUE count, VALUE input)
7
+ {
8
+ char * salt;
9
+ VALUE str_salt;
10
+
11
+ salt = crypt_gensalt_ra(
12
+ StringValuePtr(prefix),
13
+ NUM2ULONG(count),
14
+ NIL_P(input) ? NULL : StringValuePtr(input),
15
+ NIL_P(input) ? 0 : RSTRING_LEN(input));
16
+
17
+ if(!salt) return Qnil;
18
+
19
+ str_salt = rb_str_new2(salt);
20
+ free(salt);
21
+
22
+ return str_salt;
23
+ }
24
+
25
+ static VALUE ra(VALUE self, VALUE key, VALUE setting)
26
+ {
27
+ char * value;
28
+ void * data;
29
+ int size;
30
+ VALUE out;
31
+
32
+ data = NULL;
33
+ size = 0xDEADBEEF;
34
+
35
+ if(NIL_P(key) || NIL_P(setting)) return Qnil;
36
+
37
+ value = crypt_ra(
38
+ NIL_P(key) ? NULL : StringValuePtr(key),
39
+ NIL_P(setting) ? NULL : StringValuePtr(setting),
40
+ &data,
41
+ &size);
42
+
43
+ if(!value) return Qnil;
44
+
45
+ out = rb_str_new(data, size - 1);
46
+
47
+ free(data);
48
+
49
+ return out;
50
+ }
51
+
52
+ void Init_crypt()
53
+ {
54
+ mCrypt = rb_define_module("Crypt");
55
+ rb_define_singleton_method(mCrypt, "salt", crypt_salt, 3);
56
+ rb_define_singleton_method(mCrypt, "crypt", ra, 2);
57
+ }
data/ext/mri/crypt.h ADDED
@@ -0,0 +1,13 @@
1
+ /*
2
+ * Written by Solar Designer and placed in the public domain.
3
+ * See crypt_blowfish.c for more information.
4
+ */
5
+
6
+ #include <gnu-crypt.h>
7
+
8
+ #if defined(_OW_SOURCE) || defined(__USE_OW)
9
+ #define __SKIP_GNU
10
+ #undef __SKIP_OW
11
+ #include <ow-crypt.h>
12
+ #undef __SKIP_GNU
13
+ #endif