Title: [686] trunk/jopenssl: Add some functionality to the Random class, making generation of bytes work.
Revision
686
Author
olabini
Date
2007-08-11 10:51:52 -0400 (Sat, 11 Aug 2007)

Log Message

Add some functionality to the Random class, making generation of bytes work. Bump version number to 0.0.4

Modified Paths

Diff

Modified: trunk/jopenssl/jruby-openssl.gemspec (685 => 686)


--- trunk/jopenssl/jruby-openssl.gemspec	2007-08-10 17:29:37 UTC (rev 685)
+++ trunk/jopenssl/jruby-openssl.gemspec	2007-08-11 14:51:52 UTC (rev 686)
@@ -2,12 +2,12 @@
 
 spec = Gem::Specification.new do |s|
   s.name = "jruby-openssl"
-  s.version = "0.0.3"
+  s.version = "0.0.4"
   s.author = "Ola Bini"
-  s.email = "[EMAIL PROTECTED]"
+  s.email = "[EMAIL PROTECTED]"
   s.homepage = "http://jruby-extras.rubyforge.org/"
   s.platform = Gem::Platform::RUBY #should be JAVA
-  s.summary = "JRuby Openssl"
+  s.summary = "JRuby OpenSSL"
   candidates = Dir["lib/bc*.jar"] + Dir["lib/jopenssl.jar"] + Dir["lib/**/*.rb"]
   s.files = candidates.delete_if do |item| item.include?(".svn") || item.include?("rdoc") end
   s.require_path = "lib"

Modified: trunk/jopenssl/lib/jopenssl.jar


(Binary files differ)

Modified: trunk/jopenssl/lib/jruby.jar


(Binary files differ)

Modified: trunk/jopenssl/src/java/org/jruby/ext/openssl/Random.java (685 => 686)


--- trunk/jopenssl/src/java/org/jruby/ext/openssl/Random.java	2007-08-10 17:29:37 UTC (rev 685)
+++ trunk/jopenssl/src/java/org/jruby/ext/openssl/Random.java	2007-08-11 14:51:52 UTC (rev 686)
@@ -27,47 +27,74 @@
  ***** END LICENSE BLOCK *****/
 package org.jruby.ext.openssl;
 
+import java.security.SecureRandom;
+
 import org.jruby.Ruby;
+import org.jruby.RubyClass;
 import org.jruby.RubyModule;
+import org.jruby.RubyNumeric;
+import org.jruby.RubyString;
 import org.jruby.runtime.CallbackFactory;
 import org.jruby.runtime.builtin.IRubyObject;
+import org.jruby.util.ByteList;
 
 /**
  * @author <a href="" PROTECTED]">Ola Bini</a>
  */
 public class Random {
+    private final static class RandomHolder {
+        public java.util.Random[] randomizers;
+    }
     public static void createRandom(Ruby runtime, RubyModule ossl) {
         RubyModule rand = ossl.defineModuleUnder("Random");
 
+        RubyClass osslError = (RubyClass)ossl.getConstant("OpenSSLError");
+        rand.defineClassUnder("RandomError",osslError,osslError.getAllocator());
+
         CallbackFactory randcb = runtime.callbackFactory(Random.class);
-        rand.getMetaClass().defineFastMethod("seed",randcb.getFastOptSingletonMethod("seed"));
-        rand.getMetaClass().defineFastMethod("load_random_file",randcb.getFastOptSingletonMethod("load_random_file"));
-        rand.getMetaClass().defineFastMethod("write_random_file",randcb.getFastOptSingletonMethod("write_random_file"));
-        rand.getMetaClass().defineFastMethod("random_bytes",randcb.getFastOptSingletonMethod("random_bytes"));
-        rand.getMetaClass().defineFastMethod("pseudo_bytes",randcb.getFastOptSingletonMethod("pseudo_bytes"));
-        rand.getMetaClass().defineFastMethod("egd",randcb.getFastOptSingletonMethod("egd"));
-        rand.getMetaClass().defineFastMethod("egd_bytes",randcb.getFastOptSingletonMethod("egd_bytes"));
+        rand.getMetaClass().defineFastMethod("seed",randcb.getFastSingletonMethod("seed", IRubyObject.class));
+        rand.getMetaClass().defineFastMethod("load_random_file",randcb.getFastSingletonMethod("load_random_file", IRubyObject.class));
+        rand.getMetaClass().defineFastMethod("write_random_file",randcb.getFastSingletonMethod("write_random_file", IRubyObject.class));
+        rand.getMetaClass().defineFastMethod("random_bytes",randcb.getFastSingletonMethod("random_bytes", IRubyObject.class));
+        rand.getMetaClass().defineFastMethod("pseudo_bytes",randcb.getFastSingletonMethod("pseudo_bytes", IRubyObject.class));
+        rand.getMetaClass().defineFastMethod("egd",randcb.getFastSingletonMethod("egd", IRubyObject.class));
+        rand.getMetaClass().defineFastMethod("egd_bytes",randcb.getFastSingletonMethod("egd_bytes", IRubyObject.class, IRubyObject.class));
+
+        RandomHolder holder = new RandomHolder();
+        holder.randomizers = new java.util.Random[]{new java.util.Random(), new SecureRandom()};
+        rand.dataWrapStruct(holder);
     }
 
-    public static IRubyObject seed(IRubyObject recv, IRubyObject[] args) {
+    public static IRubyObject seed(IRubyObject recv, IRubyObject arg) {
         return recv.getRuntime().getNil();
     }
-    public static IRubyObject load_random_file(IRubyObject recv, IRubyObject[] args) {
+    public static IRubyObject load_random_file(IRubyObject recv, IRubyObject arg) {
         return recv.getRuntime().getNil();
     }
-    public static IRubyObject write_random_file(IRubyObject recv, IRubyObject[] args) {
+    public static IRubyObject write_random_file(IRubyObject recv, IRubyObject arg) {
         return recv.getRuntime().getNil();
     }
-    public static IRubyObject random_bytes(IRubyObject recv, IRubyObject[] args) {
-        return recv.getRuntime().getNil();
+
+    public static IRubyObject random_bytes(IRubyObject recv, IRubyObject arg) {
+        return generate(recv, arg, 1);
     }
-    public static IRubyObject pseudo_bytes(IRubyObject recv, IRubyObject[] args) {
-        return recv.getRuntime().getNil();
+
+    public static IRubyObject pseudo_bytes(IRubyObject recv, IRubyObject arg) {
+        return generate(recv, arg, 0);
     }
-    public static IRubyObject egd(IRubyObject recv, IRubyObject[] args) {
+
+    private static RubyString generate(IRubyObject recv, IRubyObject arg, int ix) {
+        RandomHolder holder = (RandomHolder)recv.dataGetStruct();
+        int len = RubyNumeric.fix2int(arg);
+        byte[] buf = new byte[len];
+        holder.randomizers[ix].nextBytes(buf);
+        return RubyString.newString(recv.getRuntime(), new ByteList(buf,false));
+    }
+
+    public static IRubyObject egd(IRubyObject recv, IRubyObject arg) {
         return recv.getRuntime().getNil();
     }
-    public static IRubyObject egd_bytes(IRubyObject recv, IRubyObject[] args) {
+    public static IRubyObject egd_bytes(IRubyObject recv, IRubyObject arg1, IRubyObject arg2) {
         return recv.getRuntime().getNil();
     }
 }
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to