diff options
author | Mattias Andrée <maandree@operamail.com> | 2013-08-10 20:44:39 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2013-08-10 20:44:39 +0200 |
commit | 12f9869bb8796f6f261b748890d839aee3cfc0b4 (patch) | |
tree | cf0623a001adf01f88da861b988ae968735185e6 | |
parent | pad10star1 returns actual length of message (diff) | |
download | sha3sum-12f9869bb8796f6f261b748890d839aee3cfc0b4.tar.gz sha3sum-12f9869bb8796f6f261b748890d839aee3cfc0b4.tar.bz2 sha3sum-12f9869bb8796f6f261b748890d839aee3cfc0b4.tar.xz |
improved memoery management in java version
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | java/ConcurrentSHA3.java | 10 | ||||
-rw-r--r-- | java/SHA3.java | 21 |
2 files changed, 23 insertions, 8 deletions
diff --git a/java/ConcurrentSHA3.java b/java/ConcurrentSHA3.java index 8deae20..00a9082 100644 --- a/java/ConcurrentSHA3.java +++ b/java/ConcurrentSHA3.java @@ -106,7 +106,7 @@ public class ConcurrentSHA3 /** * Message chunk that is being processed */ - private byte[] message; + private byte[] message = null; /** * The current state @@ -373,8 +373,11 @@ public class ConcurrentSHA3 this.nr = 12 + (this.l << 1); this.wmod = w == 64 ? -1L : (1L << this.w) - 1L; this.S = new long[25]; - this.M = new byte[(this.r * this.b) >> 2]; + if ((this.M == null) || ((this.r * this.b) >> 2 != this.M.length)) + this.M = new byte[(this.r * this.b) >> 2]; this.mptr = 0; + if (this.message == null) + this.message = new byte[8 << 10]; } @@ -405,7 +408,7 @@ public class ConcurrentSHA3 System.arraycopy(msg, 0, this.M, this.mptr, msglen); int len = this.mptr += msglen; len -= len % ((this.r * this.b) >> 3); - System.arraycopy(this.M, 0, this.message = new byte[len], 0, len); + System.arraycopy(this.M, 0, (this.message.length < len) ? (this.message = new byte[len]) : this.message, 0, len); System.arraycopy(this.M, len, this.M, 0, this.mptr -= len); int n = Math.min(len, rr); @@ -556,7 +559,6 @@ public class ConcurrentSHA3 System.arraycopy(msg, 0, this.M, this.mptr, msglen); len = this.pad10star1(this.M, this.mptr + msglen, this.r); } - this.M = null; int rr = this.r >> 3; int nn = (this.n + 7) >> 3; diff --git a/java/SHA3.java b/java/SHA3.java index 3fc84e2..151b0fa 100644 --- a/java/SHA3.java +++ b/java/SHA3.java @@ -91,7 +91,7 @@ public class SHA3 /** * Message chunk that is being processes */ - private static byte[] message; + private static byte[] message = null; /** * The current state @@ -368,8 +368,22 @@ public class SHA3 SHA3.nr = 12 + (SHA3.l << 1); SHA3.wmod = w == 64 ? -1L : (1L << SHA3.w) - 1L; SHA3.S = new long[25]; - SHA3.M = new byte[(SHA3.r * SHA3.b) >> 2]; + if ((SHA3.M == null) || ((SHA3.r * SHA3.b) >> 2 != SHA3.M.length)) + SHA3.M = new byte[(SHA3.r * SHA3.b) >> 2]; SHA3.mptr = 0; + if (SHA3.message == null) + SHA3.message = new byte[8 << 10]; + } + + + /** + * Free up static resources + */ + public static void dispose() + { + SHA3.S = null; + SHA3.M = null; + SHA3.message = null; } @@ -400,7 +414,7 @@ public class SHA3 System.arraycopy(msg, 0, SHA3.M, SHA3.mptr, msglen); int len = SHA3.mptr += msglen; len -= len % ((SHA3.r * SHA3.b) >> 3); - System.arraycopy(SHA3.M, 0, SHA3.message = new byte[len], 0, len); + System.arraycopy(SHA3.M, 0, (SHA3.message.length < len) ? (SHA3.message = new byte[len]) : SHA3.message, 0, len); System.arraycopy(SHA3.M, len, SHA3.M, 0, SHA3.mptr -= len); int n = Math.min(len, rr); @@ -551,7 +565,6 @@ public class SHA3 System.arraycopy(msg, 0, SHA3.M, SHA3.mptr, msglen); len = SHA3.pad10star1(SHA3.M, SHA3.mptr + msglen, SHA3.r); } - SHA3.M = null; int rr = SHA3.r >> 3; int nn = (SHA3.n + 7) >> 3; |