* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / ext / openssl / ossl_rand.c
blobaf61fe33a0387f1b7caedfabdb5039d95a55139d
1 /*
2 * $Id$
3 * 'OpenSSL for Ruby' project
4 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5 * All rights reserved.
6 */
7 /*
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
11 #include "ossl.h"
14 * Classes
16 VALUE mRandom;
17 VALUE eRandomError;
20 * Struct
24 * Public
28 * Private
32 * call-seq:
33 * seed(str) -> str
36 static VALUE
37 ossl_rand_seed(VALUE self, VALUE str)
39 StringValue(str);
40 RAND_seed(RSTRING_PTR(str), RSTRING_LEN(str));
42 return str;
46 * call-seq:
47 * add(str, entropy) -> self
50 static VALUE
51 ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
53 StringValue(str);
54 RAND_add(RSTRING_PTR(str), RSTRING_LEN(str), NUM2DBL(entropy));
56 return self;
60 * call-seq:
61 * load_random_file(filename) -> true
64 static VALUE
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);
72 return Qtrue;
76 * call-seq:
77 * write_random_file(filename) -> true
80 static VALUE
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);
87 return Qtrue;
91 * call-seq:
92 * random_bytes(length) -> aString
95 static VALUE
96 ossl_rand_bytes(VALUE self, VALUE len)
98 VALUE str;
99 int n = NUM2INT(len);
101 str = rb_str_new(0, n);
102 if (!RAND_bytes((unsigned char *)RSTRING_PTR(str), n)) {
103 ossl_raise(eRandomError, NULL);
106 return str;
110 * call-seq:
111 * pseudo_bytes(length) -> aString
114 static VALUE
115 ossl_rand_pseudo_bytes(VALUE self, VALUE len)
117 VALUE str;
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);
125 return str;
129 * call-seq:
130 * egd(filename) -> true
133 static VALUE
134 ossl_rand_egd(VALUE self, VALUE filename)
136 SafeStringValue(filename);
138 if(!RAND_egd(RSTRING_PTR(filename))) {
139 ossl_raise(eRandomError, NULL);
141 return Qtrue;
145 * call-seq:
146 * egd_bytes(filename, length) -> true
149 static VALUE
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);
159 return Qtrue;
163 * call-seq:
164 * status? => true | false
166 * Return true if the PRNG has been seeded with enough data, false otherwise.
168 static VALUE
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);
179 * INIT
181 void
182 Init_ossl_rand()
184 #if 0 /* let rdoc know about mOSSL */
185 mOSSL = rb_define_module("OpenSSL");
186 #endif
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)