[freenet-cvs] r18503 - in trunk/freenet: src/freenet/crypt test/freenet/crypt

nextgens at freenetproject.org nextgens at freenetproject.org
Thu Mar 13 03:51:28 UTC 2008


Author: nextgens
Date: 2008-03-13 03:51:27 +0000 (Thu, 13 Mar 2008)
New Revision: 18503

Modified:
   trunk/freenet/src/freenet/crypt/DSAGroupGenerator.java
   trunk/freenet/test/freenet/crypt/DSAGroupGeneratorTest.java
Log:
fix IsProbablyPrime() and its test

Modified: trunk/freenet/src/freenet/crypt/DSAGroupGenerator.java
===================================================================
--- trunk/freenet/src/freenet/crypt/DSAGroupGenerator.java	2008-03-13 03:29:50 UTC (rev 18502)
+++ trunk/freenet/src/freenet/crypt/DSAGroupGenerator.java	2008-03-13 03:51:27 UTC (rev 18503)
@@ -188,14 +188,14 @@
     	return obuf;
 	}
 
-	/**
-	 * WARNING: it won't work reliably for integers below 30
-	 */
 	public static boolean isPrime(BigInteger b) {
-        for (int i = 0; i < smallPrimes.length; i++) {
-            if (b.mod(smallPrimes[i]).equals(BigInteger.ZERO)) return false;
-        }
-        // FIPS 186-2 recommends 2^100:1 confidence
+		if(BigInteger.ONE.compareTo(b) > -1)
+			throw new IllegalArgumentException("Can't be a prime number!");
+		for(int i = 0; i < smallPrimes.length; i++) {
+			if(b.mod(smallPrimes[i]).equals(BigInteger.ZERO))
+				return false;
+		}
+		// FIPS 186-2 recommends 2^100:1 confidence
         return b.isProbablePrime(200);
     }
 

Modified: trunk/freenet/test/freenet/crypt/DSAGroupGeneratorTest.java
===================================================================
--- trunk/freenet/test/freenet/crypt/DSAGroupGeneratorTest.java	2008-03-13 03:29:50 UTC (rev 18502)
+++ trunk/freenet/test/freenet/crypt/DSAGroupGeneratorTest.java	2008-03-13 03:51:27 UTC (rev 18503)
@@ -18,19 +18,18 @@
 
 import java.math.BigInteger;
 
-import freenet.crypt.DSAGroupGenerator;
 import junit.framework.TestCase;
 
 /**
  * Test case for the {@link freenet.crypt.DSAGroupGeneratorTest} class.
  * 
- * @author Florent Daignière &lt;nextgens at freenetproject.org&gt;
+ * @author Florent Daigni�re &lt;nextgens at freenetproject.org&gt;
  */
 public class DSAGroupGeneratorTest extends TestCase {
 
 	public void testIsPrime() { // No need to test below 30 as it won't work anyway
-		assertFalse(DSAGroupGenerator.isPrime(BigInteger.ZERO));
-		assertFalse(DSAGroupGenerator.isPrime(BigInteger.ONE));
+		try { DSAGroupGenerator.isPrime(BigInteger.ZERO); fail(); } catch (IllegalArgumentException e) {}
+		try { DSAGroupGenerator.isPrime(BigInteger.ONE); fail(); } catch (IllegalArgumentException e) {}
 		
 		assertTrue(DSAGroupGenerator.isPrime(BigInteger.valueOf(2)));
 		assertTrue(DSAGroupGenerator.isPrime(BigInteger.valueOf(1021)));




More information about the cvs mailing list