Skip to content

Commit acad163

Browse files
committed
Reduce code duplication
Signed-off-by: Jeroen van Erp <[email protected]>
1 parent 8e9e644 commit acad163

File tree

4 files changed

+102
-128
lines changed

4 files changed

+102
-128
lines changed

src/main/java/net/schmizz/sshj/transport/random/BouncyCastleFipsRandom.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,11 @@
1515
*/
1616
package net.schmizz.sshj.transport.random;
1717

18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
20-
21-
import java.security.NoSuchProviderException;
22-
import java.security.SecureRandom;
23-
2418
/**
2519
* BouncyCastle <code>Random</code>. This pseudo random number generator uses BouncyCastle fips.
2620
* The JRE random will be used when creating a new generator to add some random data to the seed.
2721
*/
28-
public class BouncyCastleFipsRandom
29-
implements Random {
30-
31-
private static final Logger logger = LoggerFactory.getLogger(BouncyCastleFipsRandom.class);
22+
public class BouncyCastleFipsRandom extends SecureRandomProvider {
3223

3324
/** Named factory for the BouncyCastle <code>Random</code> */
3425
public static class Factory
@@ -40,39 +31,9 @@ public Random create() {
4031
}
4132

4233
}
43-
private byte[] tmp = new byte[16];
44-
private final SecureRandom random;
4534

46-
public BouncyCastleFipsRandom() {
47-
logger.info("Generating random seed from SecureRandom of BCFIPS.");
48-
long t = System.currentTimeMillis();
49-
try {
50-
// Use SecureRandom with the BCFIPS provider
51-
random = SecureRandom.getInstance("DEFAULT", "BCFIPS");
52-
} catch (NoSuchProviderException e) {
53-
throw new RuntimeException("BCFIPS provider is not available", e);
54-
} catch (Exception e) {
55-
throw new RuntimeException("Failed to initialize SecureRandom with BCFIPS provider", e);
56-
}
57-
logger.debug("Creating random seed took {} ms", System.currentTimeMillis() - t);
58-
}
5935

60-
@Override
61-
public synchronized void fill(byte[] bytes, int start, int len) {
62-
if (start == 0 && len == bytes.length) {
63-
random.nextBytes(bytes);
64-
} else {
65-
synchronized (this) {
66-
if (len > tmp.length) tmp = new byte[len];
67-
random.nextBytes(tmp);
68-
System.arraycopy(tmp, 0, bytes, start, len);
69-
}
70-
}
71-
}
72-
73-
@Override
74-
public void fill(byte[] bytes) {
75-
random.nextBytes(bytes);
36+
public BouncyCastleFipsRandom() {
37+
super("DEFAULT", "BCFIPS");
7638
}
77-
7839
}

src/main/java/net/schmizz/sshj/transport/random/BouncyCastleRandom.java

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,41 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
/*
17+
* Copyright (C)2009 - SSHJ Contributors
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
1631
package net.schmizz.sshj.transport.random;
1732

18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
20-
21-
import java.security.NoSuchProviderException;
22-
import java.security.SecureRandom;
23-
2433
/**
25-
* BouncyCastle <code>Random</code>. This pseudo random number generator uses BouncyCastle non fips.
26-
* The JRE random will be used when creating a new generator to add some random data to the seed.
27-
*/
28-
public class BouncyCastleRandom
29-
implements Random {
30-
31-
private static final Logger logger = LoggerFactory.getLogger(BouncyCastleRandom.class);
34+
* BouncyCastle <code>Random</code>. This pseudo random number generator uses BouncyCastle non fips.
35+
* The JRE random will be used when creating a new generator to add some random data to the seed.
36+
*/
37+
public class BouncyCastleRandom extends SecureRandomProvider {
3238

3339
/** Named factory for the BouncyCastle <code>Random</code> */
3440
public static class Factory
35-
implements net.schmizz.sshj.common.Factory<Random> {
41+
implements net.schmizz.sshj.common.Factory<Random> {
3642

3743
@Override
3844
public Random create() {
3945
return new BouncyCastleRandom();
4046
}
4147

4248
}
43-
private byte[] tmp = new byte[16];
44-
private final SecureRandom random;
4549

46-
public BouncyCastleRandom() {
47-
logger.info("Generating random seed from SecureRandom of BC.");
48-
long t = System.currentTimeMillis();
49-
try {
50-
// Use SecureRandom with the BC provider
51-
random = SecureRandom.getInstance("DEFAULT", "BC");
52-
} catch (NoSuchProviderException e) {
53-
throw new RuntimeException("BC provider is not in the classpath", e);
54-
} catch (Exception e) {
55-
throw new RuntimeException("Failed to initialize SecureRandom with BC provider", e);
50+
public BouncyCastleRandom() {
51+
super("DEFAULT", "BC");
5652
}
57-
logger.debug("Creating random seed took {} ms", System.currentTimeMillis() - t);
58-
}
59-
60-
@Override
61-
public synchronized void fill(byte[] bytes, int start, int len) {
62-
if (start == 0 && len == bytes.length) {
63-
random.nextBytes(bytes);
64-
} else {
65-
synchronized (this) {
66-
if (len > tmp.length) tmp = new byte[len];
67-
random.nextBytes(tmp);
68-
System.arraycopy(tmp, 0, bytes, start, len);
69-
}
70-
}
71-
}
72-
73-
@Override
74-
public void fill(byte[] bytes) {
75-
random.nextBytes(bytes);
76-
}
77-
7853
}

src/main/java/net/schmizz/sshj/transport/random/JCERandom.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515
*/
1616
package net.schmizz.sshj.transport.random;
1717

18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
2018

2119
import java.security.SecureRandom;
2220

2321
/** A {@link Random} implementation using the built-in {@link SecureRandom} PRNG. */
24-
public class JCERandom
25-
implements Random {
26-
private static final Logger logger = LoggerFactory.getLogger(JCERandom.class);
27-
22+
public class JCERandom extends SecureRandomProvider {
2823
/** Named factory for the JCE {@link Random} */
2924
public static class Factory
3025
implements net.schmizz.sshj.common.Factory.Named<Random> {
@@ -41,39 +36,7 @@ public String getName() {
4136

4237
}
4338

44-
private byte[] tmp = new byte[16];
45-
private final SecureRandom random;
46-
4739
JCERandom() {
48-
logger.info("Creating new SecureRandom.");
49-
long t = System.currentTimeMillis();
50-
random = new SecureRandom();
51-
logger.debug("Random creation took {} ms", System.currentTimeMillis() - t);
52-
}
53-
54-
/**
55-
* Fill the given byte-array with random bytes from this PRNG.
56-
*
57-
* @param foo the byte-array
58-
* @param start the offset to start at
59-
* @param len the number of bytes to fill
60-
*/
61-
@Override
62-
public synchronized void fill(byte[] foo, int start, int len) {
63-
if (start == 0 && len == foo.length) {
64-
random.nextBytes(foo);
65-
} else {
66-
synchronized (this) {
67-
if (len > tmp.length)
68-
tmp = new byte[len];
69-
random.nextBytes(tmp);
70-
System.arraycopy(tmp, 0, foo, start, len);
71-
}
72-
}
73-
}
74-
75-
@Override
76-
public void fill(final byte[] bytes) {
77-
random.nextBytes(bytes);
40+
super();
7841
}
7942
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.schmizz.sshj.transport.random;
17+
18+
import java.security.NoSuchProviderException;
19+
import java.security.SecureRandom;
20+
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
public class SecureRandomProvider implements Random{
25+
private static final Logger logger = LoggerFactory.getLogger(SecureRandomProvider.class);
26+
27+
private byte[] tmp = new byte[16];
28+
private SecureRandom random;
29+
30+
protected SecureRandomProvider() {
31+
this.random = newRandom();
32+
}
33+
34+
protected SecureRandomProvider(String algorithm, String provider) {
35+
this.random = newRandom(algorithm, provider);
36+
}
37+
38+
private static SecureRandom newRandom() {
39+
return new SecureRandom();
40+
}
41+
42+
private static SecureRandom newRandom(String algorithm, String provider) {
43+
logger.info("Generating random seed from SecureRandom of {}.", provider);
44+
long t = System.currentTimeMillis();
45+
try {
46+
// Use SecureRandom with the provider
47+
return SecureRandom.getInstance(algorithm, provider);
48+
} catch (NoSuchProviderException e) {
49+
throw new RuntimeException(String.format("%s provider is not in the classpath", provider), e);
50+
} catch (Exception e) {
51+
throw new RuntimeException(String.format("Failed to initialize SecureRandom with %s provider", provider), e);
52+
} finally {
53+
logger.debug("Creating random seed took {} ms", System.currentTimeMillis() - t);
54+
}
55+
}
56+
57+
@Override
58+
public synchronized void fill(byte[] bytes, int start, int len) {
59+
if (start == 0 && len == bytes.length) {
60+
random.nextBytes(bytes);
61+
} else {
62+
synchronized (this) {
63+
if (len > tmp.length) tmp = new byte[len];
64+
random.nextBytes(tmp);
65+
System.arraycopy(tmp, 0, bytes, start, len);
66+
}
67+
}
68+
}
69+
70+
@Override
71+
public void fill(byte[] bytes) {
72+
random.nextBytes(bytes);
73+
}
74+
75+
}

0 commit comments

Comments
 (0)