diff options
author | Florian Zschocke <florian.zschocke@devolo.de> | 2016-12-10 01:00:27 +0100 |
---|---|---|
committer | Florian Zschocke <florian.zschocke@devolo.de> | 2016-12-12 14:25:41 +0100 |
commit | 2be2c2c95c9a3747fd200e3ea3623607053d5299 (patch) | |
tree | b3e2e6ee26565a351933c91bb8f31a752a3980fc /src/test | |
parent | a1fc7e7228d7b8de05bc2cf074f112af757401d0 (diff) | |
download | gitblit-2be2c2c95c9a3747fd200e3ea3623607053d5299.tar.gz gitblit-2be2c2c95c9a3747fd200e3ea3623607053d5299.zip |
Introduce SecureRandom wrapper for properly seeded static instances
Introduce our own wrapper `SecureRandom` around `java.security.SecureRandom`.
This a) makes sure that the PRNG is seeded on creation and not when
random bytes are retrieved, and
b) uses a static instance in the `UserModel` so that lags do not occur
during operation due to potentially seeding getting blocked on Unix
when reading from the system's entropy pool. To keep the random data
still secure, the static instance will reseed all 24 hours, also a
functionality of the wrapper class.
This fixes #1063 and extends and closes PR #1116
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/gitblit/utils/SecureRandomTest.java | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/test/java/com/gitblit/utils/SecureRandomTest.java b/src/test/java/com/gitblit/utils/SecureRandomTest.java new file mode 100644 index 00000000..c4098c2f --- /dev/null +++ b/src/test/java/com/gitblit/utils/SecureRandomTest.java @@ -0,0 +1,33 @@ +package com.gitblit.utils; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +public class SecureRandomTest { + + @Test + public void testRandomBytes() { + SecureRandom sr = new SecureRandom(); + byte[] bytes1 = sr.randomBytes(10); + assertEquals(10, bytes1.length); + byte[] bytes2 = sr.randomBytes(10); + assertEquals(10, bytes2.length); + assertFalse(Arrays.equals(bytes1, bytes2)); + + assertEquals(0, sr.randomBytes(0).length); + assertEquals(200, sr.randomBytes(200).length); + } + + @Test + public void testNextBytes() { + SecureRandom sr = new SecureRandom(); + byte[] bytes1 = new byte[32]; + sr.nextBytes(bytes1); + byte[] bytes2 = new byte[32]; + sr.nextBytes(bytes2); + assertFalse(Arrays.equals(bytes1, bytes2)); + } +} |