Author: jflesch
Date: 2007-04-22 16:03:22 +0000 (Sun, 22 Apr 2007)
New Revision: 12869

Added:
   trunk/apps/Thaw/src/thaw/plugins/signatures/RandomSource.java
Modified:
   trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
Log:
Fix random source and SHA256 utilizations

Modified: trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java   2007-04-22 
15:17:42 UTC (rev 12868)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/Identity.java   2007-04-22 
16:03:22 UTC (rev 12869)
@@ -18,7 +18,6 @@
 import freenet.crypt.DSAGroup;
 import freenet.crypt.DSAPublicKey;
 import freenet.crypt.DSASignature;
-import freenet.crypt.Yarrow;
 import freenet.crypt.Global;

 import thaw.core.Logger;
@@ -93,13 +92,7 @@
                this.isDup = isDup;
                this.trustLevel = trustLevel;

-               MessageDigest md = SHA256.getMessageDigest();
-               md.reset();
-               md.update(y);
-
-               hash = Base64.encode(md.digest());
-
-               SHA256.returnMessageDigest(md);
+               hash = Base64.encode(SHA256.digest(y));
        }


@@ -112,7 +105,7 @@
        public static Identity generate(Hsqldb db, String nick) {
                Logger.info(nick, "thaw.plugins.signatures.Identity : 
Generating new identity ...");

-               Yarrow randomSource = new Yarrow();
+               freenet.crypt.RandomSource randomSource = 
thaw.plugins.signatures.RandomSource.getRandomSource();

                DSAPrivateKey privateKey = new 
DSAPrivateKey(Global.DSAgroupBigA, randomSource);
                DSAPublicKey publicKey = new DSAPublicKey(Global.DSAgroupBigA, 
privateKey);
@@ -178,26 +171,24 @@


        public static DSASignature sign(String text, byte[] x) {
-               Yarrow randomSource = new Yarrow();
+               freenet.crypt.RandomSource randomSource = 
thaw.plugins.signatures.RandomSource.getRandomSource();

-               MessageDigest md = SHA256.getMessageDigest();
+               BigInteger m;

-               md.reset();

                try {
-                       md.update(text.getBytes("UTF-8"));
+                       m = new 
BigInteger(SHA256.digest(text.getBytes("UTF-8")));
                } catch(java.io.UnsupportedEncodingException e) {
-                       md.update(text.getBytes());
+                       Logger.warning(new Identity(), "sign() : 
UnsupportedEncodingException ? => Falling back on default charset");
+                       m = new BigInteger(SHA256.digest(text.getBytes()));
                }

-               BigInteger m = new BigInteger(md.digest());

                DSASignature sign = DSA.sign(Global.DSAgroupBigA,
                                             new DSAPrivateKey(new 
BigInteger(x)),
                                             m,
                                             randomSource);

-               SHA256.returnMessageDigest(md);

                return sign;
        }
@@ -213,24 +204,19 @@
                                    byte[] s, /* sig */
                                    byte[] y) /* publicKey */ {

-               MessageDigest md = SHA256.getMessageDigest();
+               BigInteger m;

-               md.reset();
-
                try {
-                       md.update(text.getBytes("UTF-8"));
+                       m = new 
BigInteger(SHA256.digest(text.getBytes("UTF-8")));
                } catch(java.io.UnsupportedEncodingException e) {
-                       md.update(text.getBytes());
+                       /* no logging because if it happens once, it will 
happen often */
+                       m = new BigInteger(SHA256.digest(text.getBytes()));
                }

-               BigInteger m = new BigInteger(md.digest());
-
                boolean ret = DSA.verify(new DSAPublicKey(Global.DSAgroupBigA, 
new BigInteger(y)),
                                         new DSASignature(new BigInteger(r), 
new BigInteger(s)),
                                         m, false);

-               SHA256.returnMessageDigest(md);
-
                return ret;
        }


Added: trunk/apps/Thaw/src/thaw/plugins/signatures/RandomSource.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/signatures/RandomSource.java               
                (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/signatures/RandomSource.java       
2007-04-22 16:03:22 UTC (rev 12869)
@@ -0,0 +1,23 @@
+package thaw.plugins.signatures;
+
+import freenet.crypt.Yarrow;
+
+
+/**
+ * Not really usefull at the moment.
+ * Later it will store the last
+ * Note for later: it was really really stupid from my part to call it 
'RandomSource' ....
+ */
+public class RandomSource {
+
+       private static freenet.crypt.RandomSource randomSource = null;
+
+       public static freenet.crypt.RandomSource getRandomSource() {
+               if (randomSource == null)
+                       randomSource = new Yarrow();
+
+               return randomSource;
+       }
+
+
+}


Reply via email to