From 39343ce957c517cd8d532ab9995fef40a58796b6 Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Sun, 17 Apr 2005 01:04:06 +0000
Subject: [PATCH] 2005-04-16  jrandom     * Migrated to Bouncycastle's SHA256
 and HMAC implementations for efficiency

---
 .../test/net/i2p/crypto/HMACSHA256Bench.java  | 18 +++----
 .../java/test/net/i2p/crypto/SHA256Bench.java | 51 +------------------
 2 files changed, 9 insertions(+), 60 deletions(-)

diff --git a/core/java/test/net/i2p/crypto/HMACSHA256Bench.java b/core/java/test/net/i2p/crypto/HMACSHA256Bench.java
index 715c45ec0b..453bea7866 100644
--- a/core/java/test/net/i2p/crypto/HMACSHA256Bench.java
+++ b/core/java/test/net/i2p/crypto/HMACSHA256Bench.java
@@ -71,33 +71,29 @@ public class HMACSHA256Bench {
 			buf.append("a");
 		}
 		byte[] lmess = buf.toString().getBytes();
-        
-        HMACSHA256Generator.Buffer sbuf = ctx.hmac().createBuffer(smess.length);
-        HMACSHA256Generator.Buffer mbuf = ctx.hmac().createBuffer(mmess.length);
-        HMACSHA256Generator.Buffer lbuf = ctx.hmac().createBuffer(lmess.length);
-        
+
 		// warm up the engines
-        ctx.hmac().calculate(key, smess, sbuf);
-        ctx.hmac().calculate(key, mmess, mbuf);
-        ctx.hmac().calculate(key, lmess, lbuf);
+        ctx.hmac().calculate(key, smess);
+        ctx.hmac().calculate(key, mmess);
+        ctx.hmac().calculate(key, lmess);
         
         long before = System.currentTimeMillis();
         for (int x = 0; x < times; x++)
-            ctx.hmac().calculate(key, smess, sbuf);
+            ctx.hmac().calculate(key, smess);
         long after = System.currentTimeMillis();
         
         display(times, before, after, smess.length, "3 byte");
         
         before = System.currentTimeMillis();
         for (int x = 0; x < times; x++)
-            ctx.hmac().calculate(key, mmess, mbuf);
+            ctx.hmac().calculate(key, mmess);
         after = System.currentTimeMillis();
 
         display(times, before, after, mmess.length, "2KB");
         
         before = System.currentTimeMillis();
         for (int x = 0; x < times; x++)
-            ctx.hmac().calculate(key, lmess, lbuf);
+            ctx.hmac().calculate(key, lmess);
         after = System.currentTimeMillis();
 
         display(times, before, after, lmess.length, "10KB");
diff --git a/core/java/test/net/i2p/crypto/SHA256Bench.java b/core/java/test/net/i2p/crypto/SHA256Bench.java
index 7c1fce418c..dc5f14163c 100644
--- a/core/java/test/net/i2p/crypto/SHA256Bench.java
+++ b/core/java/test/net/i2p/crypto/SHA256Bench.java
@@ -68,11 +68,6 @@ public class SHA256Bench {
 		}
 		byte[] lmess = buf.toString().getBytes();
         
-        SHA256EntryCache cache = new SHA256EntryCache();
-        SHA256EntryCache.CacheEntry scache = cache.acquire(smess.length);
-        SHA256EntryCache.CacheEntry mcache = cache.acquire(mmess.length);
-        SHA256EntryCache.CacheEntry lcache = cache.acquire(lmess.length);
-        
 		// warm up the engines
 		SHA256Generator.getInstance().calculateHash(smess);
 		SHA256Generator.getInstance().calculateHash(mmess);
@@ -88,35 +83,11 @@ public class SHA256Bench {
 			long endmedstartlong = System.currentTimeMillis();
 			Hash l = cacheOnly ? null : SHA256Generator.getInstance().calculateHash(lmess);
 			long endlong = System.currentTimeMillis();
-            
-            // now do it with caching
-			Hash s1 = SHA256Generator.getInstance().calculateHash(smess, 0, smess.length, scache);
-			long endshortstartmed1 = System.currentTimeMillis();
-			Hash m1 = SHA256Generator.getInstance().calculateHash(mmess, 0, mmess.length, mcache);
-			long endmedstartlong1 = System.currentTimeMillis();
-			Hash l1 = SHA256Generator.getInstance().calculateHash(lmess, 0, lmess.length, lcache);
-			long endlong1 = System.currentTimeMillis();
-            
-            if (cacheOnly) {
-                // dont verify
-            } else {
-                if (!s.equals(s1) || !m.equals(m1) || !l.equals(l1)) {
-                    System.err.println("*ERR* match failed on " + x 
-                                       + "s="+ s.equals(s1) + " m=" + m.equals(m1)
-                                       + " l=" + l.equals(l1));
-                    return;
-                }
-            }
-            
-			//System.out.print(".");
-			shorttime += endshortstartmed - startshort;
+
+            shorttime += endshortstartmed - startshort;
 			medtime += endmedstartlong - endshortstartmed;
 			longtime += endlong - endmedstartlong;
             
-			shorttime1 += endshortstartmed1 - endlong;
-			medtime1 += endmedstartlong1 - endshortstartmed1;
-			longtime1 += endlong1 - endmedstartlong1;
-			
 			if ((minShort == 0) && (minMed == 0) && (minLong == 0) ) {
 			    minShort = endshortstartmed - startshort;
 			    maxShort = endshortstartmed - startshort;
@@ -124,13 +95,6 @@ public class SHA256Bench {
 			    maxMed = endmedstartlong - endshortstartmed;
 			    minLong = endlong - endmedstartlong;
 			    maxLong = endlong - endmedstartlong;
-                
-			    minShort1 = endshortstartmed1 - endlong;
-			    maxShort1 = endshortstartmed1 - endlong;
-			    minMed1 = endmedstartlong1 - endshortstartmed1;
-			    maxMed1 = endmedstartlong1 - endshortstartmed1;
-			    minLong1 = endlong1 - endmedstartlong1;
-			    maxLong1 = endlong1 - endmedstartlong1;
 			} else {
 			    if (minShort > endshortstartmed - startshort) minShort = endshortstartmed - startshort;
 			    if (maxShort < endshortstartmed - startshort) maxShort = endshortstartmed - startshort;
@@ -138,23 +102,12 @@ public class SHA256Bench {
 			    if (maxMed < endmedstartlong - endshortstartmed) maxMed = endmedstartlong - endshortstartmed;
 			    if (minLong > endlong - endmedstartlong) minLong = endlong - endmedstartlong;
 			    if (maxLong < endlong - endmedstartlong) maxLong = endlong - endmedstartlong;
-                
-			    if (minShort1 > endshortstartmed1 - endlong) minShort1 = endshortstartmed1 - endlong;
-			    if (maxShort1 < endshortstartmed1 - endlong) maxShort1 = endshortstartmed1 - endlong;
-			    if (minMed1 > endmedstartlong1 - endshortstartmed1) minMed1 = endmedstartlong - endshortstartmed;
-			    if (maxMed1 < endmedstartlong1 - endshortstartmed1) maxMed1 = endmedstartlong - endshortstartmed;
-			    if (minLong1 > endlong1 - endmedstartlong1) minLong1 = endlong1 - endmedstartlong1;
-			    if (maxLong1 < endlong1 - endmedstartlong1) maxLong1 = endlong1 - endmedstartlong1;
 			}
 		}
 		System.out.println();
 		System.out.println("Short Message Time Average  : " + (shorttime/times) + "\ttotal: " + shorttime + "\tmin: " + minShort + "\tmax: " + maxShort + "\tBps: " + (shorttime == 0 ? "NaN" : ""+(times*smess.length)/shorttime));
 		System.out.println("Medium Message Time Average : " + (medtime/times) + "\ttotal: " + medtime + "\tmin: " + minMed + "\tmax: " + maxMed + "\tBps: " + (medtime == 0 ? "NaN" : ""+(times*mmess.length*1000)/medtime));
 		System.out.println("Long Message Time Average   : " + (longtime/times) + "\ttotal: " + longtime + "\tmin: " + minLong + "\tmax: " + maxLong + "\tBps: " + (longtime == 0 ? "NaN" : "" + (times*lmess.length*1000)/longtime));
-        
-		System.out.println("Short Message Time (cache)  : " + (shorttime1/times) + "\ttotal: " + shorttime1 + "\tmin: " + minShort1 + "\tmax: " + maxShort1 + "\tBps: " + (shorttime1 == 0 ? "NaN" : ""+(times*smess.length)/shorttime1));
-		System.out.println("Medium Message Time (cache) : " + (medtime1/times) + "\ttotal: " + medtime1 + "\tmin: " + minMed1 + "\tmax: " + maxMed1 + "\tBps: " + (times*mmess.length*1000)/medtime1);
-		System.out.println("Long Message Time (cache)   : " + (longtime1/times) + "\ttotal: " + longtime1 + "\tmin: " + minLong1 + "\tmax: " + maxLong1 + "\tBps: " + (times*lmess.length*1000)/longtime1);
 	}
 }
 	
-- 
GitLab