[kaffe] CVS kaffe (kaz): test/regression/SecureRandomTest.java:

Kaffe CVS cvs-commits at kaffe.org
Wed Jul 27 15:52:12 PDT 2005


PatchSet 6769 
Date: 2005/07/27 22:47:07
Author: kaz
Branch: HEAD
Tag: (none) 
Log:
2005-07-28 Ito Kazumitsu <kaz at maczuka.gcd.org>

        * test/regression/SecureRandomTest.java:
        Use kaffe's security provider explicitly.

Members: 
	ChangeLog:1.4294->1.4295 
	test/regression/SecureRandomTest.java:INITIAL->1.3 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.4294 kaffe/ChangeLog:1.4295
--- kaffe/ChangeLog:1.4294	Tue Jul 26 16:21:58 2005
+++ kaffe/ChangeLog	Wed Jul 27 22:47:07 2005
@@ -1,3 +1,8 @@
+2005-07-28 Ito Kazumitsu <kaz at maczuka.gcd.org>
+
+	* test/regression/SecureRandomTest.java:
+	Use kaffe's security provider explicitly.
+
 2005-07-26 Dalibor Topic  <robilad at kaffe.org>
 
 	Resynced with GNU Classpath.
===================================================================
Checking out kaffe/test/regression/SecureRandomTest.java
RCS:  /home/cvs/kaffe/kaffe/test/regression/SecureRandomTest.java,v
VERS: 1.3
***************
--- /dev/null	Sun Aug  4 19:57:58 2002
+++ kaffe/test/regression/SecureRandomTest.java	Wed Jul 27 22:52:12 2005
@@ -0,0 +1,101 @@
+
+import java.security.*;
+
+class SecureRandomTest
+{
+    private static final int HISTORY_SIZE = 20;
+    private static final int ITERATIONS = 10;
+    
+    public static final String toPlainString(byte bytes[], int maxBytes)
+    {
+	String retval = "";
+
+	if( bytes != null )
+	{
+	    int lpc;
+	    
+	    if( maxBytes > bytes.length )
+		maxBytes = bytes.length;
+	    for( lpc = 0; lpc < maxBytes; lpc++ )
+	    {
+		if( (bytes[lpc] & 0xf0) == 0 )
+		    retval += "0" + Integer.toHexString(bytes[lpc] & 0xff);
+		else
+		    retval += Integer.toHexString(bytes[lpc] & 0xff);
+	    }
+	}
+	else
+	{
+	    retval = "(null)";
+	}
+	return retval;
+    }
+    
+    private static void checkHistory(byte history[][], byte data[])
+    {
+	int lpc;
+
+	for( lpc = 0; lpc < history.length; lpc++ )
+	{
+	    boolean match = true;
+	    int lpc2;
+
+	    if( history[lpc] == null )
+		continue;
+	    for( lpc2 = 0; lpc2 < data.length; lpc2++ )
+	    {
+		match = match && (history[lpc][lpc2] == data[lpc2]);
+	    }
+	    if( match )
+	    {
+		throw new Error("The \"secure\" random isn't! : lpc=  " + lpc 
+				+ " lpc2 = " + lpc2 
+				+ " data = " + toPlainString(data, Integer.MAX_VALUE));
+	    }
+	}
+    }
+    
+    public static void main(String args[])
+	throws Throwable
+    {
+	byte history[][];
+	SecureRandom sr;
+	int lpc, hist;
+	byte data[];
+
+	Security.addProvider(new kaffe.security.provider.Kaffe());
+	
+	/*
+	 * Make sure the SecureRandom's produce different sequences after
+	 * initialization.
+	 */
+	history = new byte[HISTORY_SIZE][];
+	/*
+	 * Now that we have added new kaffe.security.provider.Kaffe()
+	 * to the list of security providers, we may well explicitly
+	 * request Kaffe's SHA1PRNG to be used.
+	 * Otherwise, in some poor environment, GNU classpath's default
+	 * provider may be used, which, at the moment of this writing,
+	 * is not strong enough to pass this test. (Thu Jul 28, 2005)
+	 */
+	sr = SecureRandom.getInstance("SHA1PRNG", "KAFFE");
+	for( lpc = 0, hist = 0; lpc < ITERATIONS; lpc++, hist++ )
+	{
+	    data = new byte[20];
+	    sr.nextBytes(data);
+	    history[hist % HISTORY_SIZE] = data;
+	}
+	sr = SecureRandom.getInstance("SHA1PRNG", "KAFFE");
+	for( lpc = 0, hist = 0; lpc < ITERATIONS; lpc++, hist++ )
+	{
+	    data = new byte[20];
+	    sr.nextBytes(data);
+	    checkHistory(history, data);
+	}
+	System.out.println("Two SecureRandoms produce different output.");
+    }
+}
+
+/* Expected Output:
+Two SecureRandoms produce different output.
+*/



More information about the kaffe mailing list