Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# ext/openssl/ossl_rand.c

Go to the documentation of this file.

    00001 /*
    00002  * $Id: ossl_rand.c 27440 2010-04-22 08:21:01Z nobu $
    00003  * 'OpenSSL for Ruby' project
    00004  * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
    00005  * All rights reserved.
    00006  */
    00007 /*
    00008  * This program is licenced under the same licence as Ruby.
    00009  * (See the file 'LICENCE'.)
    00010  */
    00011 #include "ossl.h"
    00012
    00013 /*
    00014  * Classes
    00015  */
    00016 VALUE mRandom;
    00017 VALUE eRandomError;
    00018
    00019 /*
    00020  * Struct
    00021  */
    00022
    00023 /*
    00024  * Public
    00025  */
    00026
    00027 /*
    00028  * Private
    00029  */
    00030
    00031 /*
    00032  *  call-seq:
    00033  *     seed(str) -> str
    00034  *
    00035  */
    00036 static VALUE
    00037 ossl_rand_seed(VALUE self, VALUE str)
    00038 {
    00039     StringValue(str);
    00040     RAND_seed(RSTRING_PTR(str), RSTRING_LEN(str));
    00041
    00042     return str;
    00043 }
    00044
    00045 /*
    00046  *  call-seq:
    00047  *     add(str, entropy) -> self
    00048  *
    00049  */
    00050 static VALUE
    00051 ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
    00052 {
    00053     StringValue(str);
    00054     RAND_add(RSTRING_PTR(str), RSTRING_LEN(str), NUM2DBL(entropy));
    00055
    00056     return self;
    00057 }
    00058
    00059 /*
    00060  *  call-seq:
    00061  *     load_random_file(filename) -> true
    00062  *
    00063  */
    00064 static VALUE
    00065 ossl_rand_load_file(VALUE self, VALUE filename)
    00066 {
    00067     SafeStringValue(filename);
    00068
    00069     if(!RAND_load_file(RSTRING_PTR(filename), -1)) {
    00070         ossl_raise(eRandomError, NULL);
    00071     }
    00072     return Qtrue;
    00073 }
    00074
    00075 /*
    00076  *  call-seq:
    00077  *     write_random_file(filename) -> true
    00078  *
    00079  */
    00080 static VALUE
    00081 ossl_rand_write_file(VALUE self, VALUE filename)
    00082 {
    00083     SafeStringValue(filename);
    00084     if (RAND_write_file(RSTRING_PTR(filename)) == -1) {
    00085         ossl_raise(eRandomError, NULL);
    00086     }
    00087     return Qtrue;
    00088 }
    00089
    00090 /*
    00091  *  call-seq:
    00092  *     random_bytes(length) -> aString
    00093  *
    00094  */
    00095 static VALUE
    00096 ossl_rand_bytes(VALUE self, VALUE len)
    00097 {
    00098     VALUE str;
    00099     int n = NUM2INT(len);
    00100
    00101     str = rb_str_new(0, n);
    00102     if (!RAND_bytes((unsigned char *)RSTRING_PTR(str), n)) {
    00103         ossl_raise(eRandomError, NULL);
    00104     }
    00105
    00106     return str;
    00107 }
    00108
    00109 /*
    00110  *  call-seq:
    00111  *     pseudo_bytes(length) -> aString
    00112  *
    00113  */
    00114 static VALUE
    00115 ossl_rand_pseudo_bytes(VALUE self, VALUE len)
    00116 {
    00117     VALUE str;
    00118     int n = NUM2INT(len);
    00119
    00120     str = rb_str_new(0, n);
    00121     if (!RAND_pseudo_bytes((unsigned char *)RSTRING_PTR(str), n)) {
    00122         ossl_raise(eRandomError, NULL);
    00123     }
    00124
    00125     return str;
    00126 }
    00127
    00128 /*
    00129  *  call-seq:
    00130  *     egd(filename) -> true
    00131  *
    00132  */
    00133 static VALUE
    00134 ossl_rand_egd(VALUE self, VALUE filename)
    00135 {
    00136     SafeStringValue(filename);
    00137
    00138     if(!RAND_egd(RSTRING_PTR(filename))) {
    00139         ossl_raise(eRandomError, NULL);
    00140     }
    00141     return Qtrue;
    00142 }
    00143
    00144 /*
    00145  *  call-seq:
    00146  *     egd_bytes(filename, length) -> true
    00147  *
    00148  */
    00149 static VALUE
    00150 ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len)
    00151 {
    00152     long n = NUM2INT(len);
    00153
    00154     SafeStringValue(filename);
    00155
    00156     if (!RAND_egd_bytes(RSTRING_PTR(filename), n)) {
    00157         ossl_raise(eRandomError, NULL);
    00158     }
    00159     return Qtrue;
    00160 }
    00161
    00162 /*
    00163  *  call-seq:
    00164  *     status? => true | false
    00165  *
    00166  * Return true if the PRNG has been seeded with enough data, false otherwise.
    00167  */
    00168 static VALUE
    00169 ossl_rand_status(VALUE self)
    00170 {
    00171     return RAND_status() ? Qtrue : Qfalse;
    00172 }
    00173
    00174 #define DEFMETH(class, name, func, argc) \
    00175         rb_define_method(class, name, func, argc); \
    00176         rb_define_singleton_method(class, name, func, argc);
    00177
    00178 /*
    00179  * INIT
    00180  */
    00181 void
    00182 Init_ossl_rand()
    00183 {
    00184 #if 0 /* let rdoc know about mOSSL */
    00185     mOSSL = rb_define_module("OpenSSL");
    00186 #endif
    00187
    00188     mRandom = rb_define_module_under(mOSSL, "Random");
    00189
    00190     eRandomError = rb_define_class_under(mRandom, "RandomError", eOSSLError);
    00191
    00192     DEFMETH(mRandom, "seed", ossl_rand_seed, 1);
    00193     DEFMETH(mRandom, "random_add", ossl_rand_add, 2);
    00194     DEFMETH(mRandom, "load_random_file", ossl_rand_load_file, 1);
    00195     DEFMETH(mRandom, "write_random_file", ossl_rand_write_file, 1);
    00196     DEFMETH(mRandom, "random_bytes", ossl_rand_bytes, 1);
    00197     DEFMETH(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1);
    00198     DEFMETH(mRandom, "egd", ossl_rand_egd, 1);
    00199     DEFMETH(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2);
    00200     DEFMETH(mRandom, "status?", ossl_rand_status, 0)
    00201 }
    00202
    00203