Ruby
1.9.3p484(2013-11-22revision43786)
Main Page
Modules
Data Structures
Files
File List
Globals
ext
openssl
ossl_rand.c
Go to the documentation of this file.
1
/*
2
* $Id: ossl_rand.c 31166 2011-03-24 07:29:21Z naruse $
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'.)
10
*/
11
#include "
ossl.h
"
12
13
/*
14
* Classes
15
*/
16
VALUE
mRandom
;
17
VALUE
eRandomError
;
18
19
/*
20
* Struct
21
*/
22
23
/*
24
* Public
25
*/
26
27
/*
28
* Private
29
*/
30
31
/*
32
* call-seq:
33
* seed(str) -> str
34
*
35
*/
36
static
VALUE
37
ossl_rand_seed
(
VALUE
self
,
VALUE
str)
38
{
39
StringValue
(str);
40
RAND_seed(
RSTRING_PTR
(str),
RSTRING_LENINT
(str));
41
42
return
str;
43
}
44
45
/*
46
* call-seq:
47
* add(str, entropy) -> self
48
*
49
*/
50
static
VALUE
51
ossl_rand_add
(
VALUE
self
,
VALUE
str,
VALUE
entropy)
52
{
53
StringValue
(str);
54
RAND_add(
RSTRING_PTR
(str),
RSTRING_LENINT
(str),
NUM2DBL
(entropy));
55
56
return
self
;
57
}
58
59
/*
60
* call-seq:
61
* load_random_file(filename) -> true
62
*
63
*/
64
static
VALUE
65
ossl_rand_load_file
(
VALUE
self
,
VALUE
filename)
66
{
67
SafeStringValue
(filename);
68
69
if
(!RAND_load_file(
RSTRING_PTR
(filename), -1)) {
70
ossl_raise
(
eRandomError
,
NULL
);
71
}
72
return
Qtrue
;
73
}
74
75
/*
76
* call-seq:
77
* write_random_file(filename) -> true
78
*
79
*/
80
static
VALUE
81
ossl_rand_write_file
(
VALUE
self
,
VALUE
filename)
82
{
83
SafeStringValue
(filename);
84
if
(RAND_write_file(
RSTRING_PTR
(filename)) == -1) {
85
ossl_raise
(
eRandomError
,
NULL
);
86
}
87
return
Qtrue
;
88
}
89
90
/*
91
* call-seq:
92
* random_bytes(length) -> aString
93
*
94
*/
95
static
VALUE
96
ossl_rand_bytes
(
VALUE
self
,
VALUE
len
)
97
{
98
VALUE
str;
99
int
n =
NUM2INT
(len);
100
101
str =
rb_str_new
(0, n);
102
if
(!RAND_bytes((
unsigned
char
*)
RSTRING_PTR
(str), n)) {
103
ossl_raise
(
eRandomError
,
NULL
);
104
}
105
106
return
str;
107
}
108
109
/*
110
* call-seq:
111
* pseudo_bytes(length) -> aString
112
*
113
*/
114
static
VALUE
115
ossl_rand_pseudo_bytes
(
VALUE
self
,
VALUE
len
)
116
{
117
VALUE
str;
118
int
n =
NUM2INT
(len);
119
120
str =
rb_str_new
(0, n);
121
if
(!RAND_pseudo_bytes((
unsigned
char
*)
RSTRING_PTR
(str), n)) {
122
ossl_raise
(
eRandomError
,
NULL
);
123
}
124
125
return
str;
126
}
127
128
/*
129
* call-seq:
130
* egd(filename) -> true
131
*
132
*/
133
static
VALUE
134
ossl_rand_egd
(
VALUE
self
,
VALUE
filename)
135
{
136
SafeStringValue
(filename);
137
138
if
(!RAND_egd(
RSTRING_PTR
(filename))) {
139
ossl_raise
(
eRandomError
,
NULL
);
140
}
141
return
Qtrue
;
142
}
143
144
/*
145
* call-seq:
146
* egd_bytes(filename, length) -> true
147
*
148
*/
149
static
VALUE
150
ossl_rand_egd_bytes
(
VALUE
self
,
VALUE
filename,
VALUE
len
)
151
{
152
int
n =
NUM2INT
(len);
153
154
SafeStringValue
(filename);
155
156
if
(!RAND_egd_bytes(
RSTRING_PTR
(filename), n)) {
157
ossl_raise
(
eRandomError
,
NULL
);
158
}
159
return
Qtrue
;
160
}
161
162
/*
163
* call-seq:
164
* status? => true | false
165
*
166
* Return true if the PRNG has been seeded with enough data, false otherwise.
167
*/
168
static
VALUE
169
ossl_rand_status
(
VALUE
self
)
170
{
171
return
RAND_status() ?
Qtrue
:
Qfalse
;
172
}
173
174
#define DEFMETH(class, name, func, argc) \
175
rb_define_method((class), (name), (func), (argc)); \
176
rb_define_singleton_method((class), (name), (func), (argc));
177
178
/*
179
* INIT
180
*/
181
void
182
Init_ossl_rand
()
183
{
184
#if 0
185
mOSSL
=
rb_define_module
(
"OpenSSL"
);
/* let rdoc know about mOSSL */
186
#endif
187
188
mRandom
=
rb_define_module_under
(
mOSSL
,
"Random"
);
189
190
eRandomError
=
rb_define_class_under
(
mRandom
,
"RandomError"
,
eOSSLError
);
191
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)
201
}
202
203
Generated on Fri Nov 22 2013 07:04:04 for Ruby by
1.8.3