From 231247c010acba191b78ed2d1310c935e63ad919 Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Wed, 23 Jul 2003 16:12:24 +0000 Subject: * ext/openssl: imported. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4128 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/ossl_rand.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 ext/openssl/ossl_rand.c (limited to 'ext/openssl/ossl_rand.c') diff --git a/ext/openssl/ossl_rand.c b/ext/openssl/ossl_rand.c new file mode 100644 index 0000000000..c30889221e --- /dev/null +++ b/ext/openssl/ossl_rand.c @@ -0,0 +1,142 @@ +/* + * $Id$ + * 'OpenSSL for Ruby' project + * Copyright (C) 2001-2002 Michal Rokos + * All rights reserved. + */ +/* + * This program is licenced under the same licence as Ruby. + * (See the file 'LICENCE'.) + */ +#include "ossl.h" + +/* + * Classes + */ +VALUE mRandom; +VALUE eRandomError; + +/* + * Struct + */ + +/* + * Public + */ + +/* + * Private + */ +static VALUE +ossl_rand_seed(VALUE self, VALUE str) +{ + StringValue(str); + RAND_seed(RSTRING(str)->ptr, RSTRING(str)->len); + + return str; +} + +static VALUE +ossl_rand_load_file(VALUE self, VALUE filename) +{ + SafeStringValue(filename); + + if(!RAND_load_file(RSTRING(filename)->ptr, -1)) { + ossl_raise(eRandomError, NULL); + } + return Qtrue; +} + +static VALUE +ossl_rand_write_file(VALUE self, VALUE filename) +{ + SafeStringValue(filename); + if (RAND_write_file(RSTRING(filename)->ptr) == -1) { + ossl_raise(eRandomError, NULL); + } + return Qtrue; +} + +static VALUE +ossl_rand_bytes(VALUE self, VALUE len) +{ + unsigned char *buffer = NULL; + VALUE str; + + if (!(buffer = OPENSSL_malloc(FIX2INT(len) + 1))) { + ossl_raise(eRandomError, NULL); + } + if (!RAND_bytes(buffer, FIX2INT(len))) { + OPENSSL_free(buffer); + ossl_raise(eRandomError, NULL); + } + str = rb_str_new(buffer, FIX2INT(len)); + OPENSSL_free(buffer); + + return str; +} + +static VALUE +ossl_rand_pseudo_bytes(VALUE self, VALUE len) +{ + unsigned char *buffer = NULL; + VALUE str; + + if (!(buffer = OPENSSL_malloc(FIX2INT(len) + 1))) { + ossl_raise(eRandomError, NULL); + } + if (!RAND_pseudo_bytes(buffer, FIX2INT(len))) { + OPENSSL_free(buffer); + ossl_raise(eRandomError, NULL); + } + str = rb_str_new(buffer, FIX2INT(len)); + OPENSSL_free(buffer); + + return str; +} + +static VALUE +ossl_rand_egd(VALUE self, VALUE filename) +{ + SafeStringValue(filename); + + if(!RAND_egd(RSTRING(filename)->ptr)) { + ossl_raise(eRandomError, NULL); + } + return Qtrue; +} + +static VALUE +ossl_rand_egd_bytes(VALUE self, VALUE filename, VALUE len) +{ + SafeStringValue(filename); + + if (!RAND_egd_bytes(RSTRING(filename)->ptr, FIX2INT(len))) { + ossl_raise(eRandomError, NULL); + } + return Qtrue; +} + +#define DEFMETH(class, name, func, argc) \ + rb_define_method(class, name, func, argc); \ + rb_define_singleton_method(class, name, func, argc); + +/* + * INIT + */ +void +Init_ossl_rand() +{ + mRandom = rb_define_module_under(mOSSL, "Random"); + + eRandomError = rb_define_class_under(mRandom, "RandomError", eOSSLError); + + DEFMETH(mRandom, "seed", ossl_rand_seed, 1); + DEFMETH(mRandom, "load_random_file", ossl_rand_load_file, 1); + DEFMETH(mRandom, "write_random_file", ossl_rand_write_file, 1); + DEFMETH(mRandom, "random_bytes", ossl_rand_bytes, 1); + DEFMETH(mRandom, "pseudo_bytes", ossl_rand_pseudo_bytes, 1); + DEFMETH(mRandom, "egd", ossl_rand_egd, 1); + DEFMETH(mRandom, "egd_bytes", ossl_rand_egd_bytes, 2); +} + -- cgit v1.2.3