3 * 'OpenSSL for Ruby' project
4 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
37 ossl_rand_seed(VALUE self
, VALUE str
)
40 RAND_seed(RSTRING_PTR(str
), RSTRING_LEN(str
));
47 * add(str, entropy) -> self
51 ossl_rand_add(VALUE self
, VALUE str
, VALUE entropy
)
54 RAND_add(RSTRING_PTR(str
), RSTRING_LEN(str
), NUM2DBL(entropy
));
61 * load_random_file(filename) -> true
65 ossl_rand_load_file(VALUE self
, VALUE filename
)
67 SafeStringValue(filename
);
69 if(!RAND_load_file(RSTRING_PTR(filename
), -1)) {
70 ossl_raise(eRandomError
, NULL
);
77 * write_random_file(filename) -> true
81 ossl_rand_write_file(VALUE self
, VALUE filename
)
83 SafeStringValue(filename
);
84 if (RAND_write_file(RSTRING_PTR(filename
)) == -1) {
85 ossl_raise(eRandomError
, NULL
);
92 * random_bytes(length) -> aString
96 ossl_rand_bytes(VALUE self
, VALUE len
)
101 str
= rb_str_new(0, n
);
102 if (!RAND_bytes((unsigned char *)RSTRING_PTR(str
), n
)) {
103 ossl_raise(eRandomError
, NULL
);
111 * pseudo_bytes(length) -> aString
115 ossl_rand_pseudo_bytes(VALUE self
, VALUE len
)
118 int n
= NUM2INT(len
);
120 str
= rb_str_new(0, n
);
121 if (!RAND_pseudo_bytes((unsigned char *)RSTRING_PTR(str
), n
)) {
122 ossl_raise(eRandomError
, NULL
);
130 * egd(filename) -> true
134 ossl_rand_egd(VALUE self
, VALUE filename
)
136 SafeStringValue(filename
);
138 if(!RAND_egd(RSTRING_PTR(filename
))) {
139 ossl_raise(eRandomError
, NULL
);
146 * egd_bytes(filename, length) -> true
150 ossl_rand_egd_bytes(VALUE self
, VALUE filename
, VALUE len
)
152 long n
= NUM2INT(len
);
154 SafeStringValue(filename
);
156 if (!RAND_egd_bytes(RSTRING_PTR(filename
), n
)) {
157 ossl_raise(eRandomError
, NULL
);
164 * status? => true | false
166 * Return true if the PRNG has been seeded with enough data, false otherwise.
169 ossl_rand_status(VALUE self
)
171 return RAND_status() ? Qtrue
: Qfalse
;
174 #define DEFMETH(class, name, func, argc) \
175 rb_define_method(class, name, func, argc); \
176 rb_define_singleton_method(class, name, func, argc);
184 #if 0 /* let rdoc know about mOSSL */
185 mOSSL
= rb_define_module("OpenSSL");
188 mRandom
= rb_define_module_under(mOSSL
, "Random");
190 eRandomError
= rb_define_class_under(mRandom
, "RandomError", eOSSLError
);
192 DEFMETH(mRandom
, "seed", ossl_rand_seed
, 1);
193 DEFMETH(mRandom
, "random_add", ossl_rand_add
, 2);
194 DEFMETH(mRandom
, "load_random_file", ossl_rand_load_file
, 1);
195 DEFMETH(mRandom
, "write_random_file", ossl_rand_write_file
, 1);
196 DEFMETH(mRandom
, "random_bytes", ossl_rand_bytes
, 1);
197 DEFMETH(mRandom
, "pseudo_bytes", ossl_rand_pseudo_bytes
, 1);
198 DEFMETH(mRandom
, "egd", ossl_rand_egd
, 1);
199 DEFMETH(mRandom
, "egd_bytes", ossl_rand_egd_bytes
, 2);
200 DEFMETH(mRandom
, "status?", ossl_rand_status
, 0)