Apparently I'm behind the times on this constructor

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-05-03 14:37:08 -07:00
parent 6fce69bbac
commit 54f25358eb
9 changed files with 26 additions and 46 deletions

View File

@ -38,12 +38,8 @@ public class AuthenticationCredentials {
} }
public AuthenticationCredentials(String authenticationToken) { public AuthenticationCredentials(String authenticationToken) {
try { this.salt = Math.abs(new SecureRandom().nextInt()) + "";
this.salt = Math.abs(SecureRandom.getInstance("SHA1PRNG").nextInt()) + "";
this.hashedAuthenticationToken = getHashedValue(salt, authenticationToken); this.hashedAuthenticationToken = getHashedValue(salt, authenticationToken);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
} }
public String getHashedAuthenticationToken() { public String getHashedAuthenticationToken() {

View File

@ -25,7 +25,7 @@ public class TurnTokenGenerator {
try { try {
Mac mac = Mac.getInstance("HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1");
long validUntilSeconds = (System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1)) / 1000; long validUntilSeconds = (System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1)) / 1000;
long user = Math.abs(SecureRandom.getInstance("SHA1PRNG").nextInt()); long user = Math.abs(new SecureRandom().nextInt());
String userTime = validUntilSeconds + ":" + user; String userTime = validUntilSeconds + ":" + user;
mac.init(new SecretKeySpec(key, "HmacSHA1")); mac.init(new SecretKeySpec(key, "HmacSHA1"));

View File

@ -360,16 +360,12 @@ public class AccountController {
} }
@VisibleForTesting protected VerificationCode generateVerificationCode(String number) { @VisibleForTesting protected VerificationCode generateVerificationCode(String number) {
try {
if (testDevices.containsKey(number)) { if (testDevices.containsKey(number)) {
return new VerificationCode(testDevices.get(number)); return new VerificationCode(testDevices.get(number));
} }
SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); SecureRandom random = new SecureRandom();
int randomInt = 100000 + random.nextInt(900000); int randomInt = 100000 + random.nextInt(900000);
return new VerificationCode(randomInt); return new VerificationCode(randomInt);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
} }
} }

View File

@ -103,14 +103,10 @@ public class AttachmentController {
} }
private long generateAttachmentId() { private long generateAttachmentId() {
try {
byte[] attachmentBytes = new byte[8]; byte[] attachmentBytes = new byte[8];
SecureRandom.getInstance("SHA1PRNG").nextBytes(attachmentBytes); new SecureRandom().nextBytes(attachmentBytes);
attachmentBytes[0] = (byte)(attachmentBytes[0] & 0x7F); attachmentBytes[0] = (byte)(attachmentBytes[0] & 0x7F);
return Conversions.byteArrayToLong(attachmentBytes); return Conversions.byteArrayToLong(attachmentBytes);
} catch (NoSuchAlgorithmException nsae) {
throw new AssertionError(nsae);
}
} }
} }

View File

@ -205,12 +205,8 @@ public class DeviceController {
} }
@VisibleForTesting protected VerificationCode generateVerificationCode() { @VisibleForTesting protected VerificationCode generateVerificationCode() {
try { SecureRandom random = new SecureRandom();
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
int randomInt = 100000 + random.nextInt(900000); int randomInt = 100000 + random.nextInt(900000);
return new VerificationCode(randomInt); return new VerificationCode(randomInt);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
} }
} }

View File

@ -222,7 +222,7 @@ public class FederatedClient {
trustManagerFactory.init(initializeTrustStore(peer.getName(), peer.getCertificate())); trustManagerFactory.init(initializeTrustStore(peer.getName(), peer.getCertificate()));
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG")); sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier()); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory).build(); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory).build();

View File

@ -22,11 +22,11 @@ public class ProvisioningAddress extends WebsocketAddress {
public static ProvisioningAddress generate() { public static ProvisioningAddress generate() {
try { try {
byte[] random = new byte[16]; byte[] random = new byte[16];
SecureRandom.getInstance("SHA1PRNG").nextBytes(random); new SecureRandom().nextBytes(random);
return new ProvisioningAddress(Base64.encodeBytesWithoutPadding(random) return new ProvisioningAddress(Base64.encodeBytesWithoutPadding(random)
.replace('+', '-').replace('/', '_'), 0); .replace('+', '-').replace('/', '_'), 0);
} catch (NoSuchAlgorithmException | InvalidWebsocketAddressException e) { } catch (InvalidWebsocketAddressException e) {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }

View File

@ -86,7 +86,7 @@ public class DeleteUserCommand extends EnvironmentCommand<WhisperServerConfigura
if (device.isPresent()) { if (device.isPresent()) {
byte[] random = new byte[16]; byte[] random = new byte[16];
SecureRandom.getInstance("SHA1PRNG").nextBytes(random); new SecureRandom().nextBytes(random);
device.get().setGcmId(null); device.get().setGcmId(null);
device.get().setFetchesMessages(false); device.get().setFetchesMessages(false);

View File

@ -244,18 +244,14 @@ public class PubSubConnectionTest {
} }
public int read(byte[] input, int offset, int length) { public int read(byte[] input, int offset, int length) {
try {
int maxCopy = Math.min(data.length - index, length); int maxCopy = Math.min(data.length - index, length);
int randomCopy = SecureRandom.getInstance("SHA1PRNG").nextInt(maxCopy) + 1; int randomCopy = new SecureRandom().nextInt(maxCopy) + 1;
int copyAmount = Math.min(maxCopy, randomCopy); int copyAmount = Math.min(maxCopy, randomCopy);
System.arraycopy(data, index, input, offset, copyAmount); System.arraycopy(data, index, input, offset, copyAmount);
index += copyAmount; index += copyAmount;
return copyAmount; return copyAmount;
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
} }
} }