Code cleanup - repetitive calls

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895031 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2021-11-14 12:44:07 +00:00
parent f1632f41e0
commit a94a46a2c5
4 changed files with 19 additions and 11 deletions

View File

@ -37,6 +37,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.Mac; import javax.crypto.Mac;
@ -93,11 +94,12 @@ public class AgileEncryptor extends Encryptor {
, newIntegritySalt = IOUtils.safelyAllocate(hashSize, maxLen); , newIntegritySalt = IOUtils.safelyAllocate(hashSize, maxLen);
// using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed). // using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed).
RandomSingleton.getInstance().nextBytes(newVerifierSalt); // blocksize SecureRandom r = RandomSingleton.getInstance();
RandomSingleton.getInstance().nextBytes(newVerifier); // blocksize r.nextBytes(newVerifierSalt); // blocksize
RandomSingleton.getInstance().nextBytes(newKeySalt); // blocksize r.nextBytes(newVerifier); // blocksize
RandomSingleton.getInstance().nextBytes(newKeySpec); // keysize r.nextBytes(newKeySalt); // blocksize
RandomSingleton.getInstance().nextBytes(newIntegritySalt); // hashsize r.nextBytes(newKeySpec); // keysize
r.nextBytes(newIntegritySalt); // hashsize
confirmPassword(password, newKeySpec, newKeySalt, newVerifierSalt, newVerifier, newIntegritySalt); confirmPassword(password, newKeySpec, newKeySalt, newVerifierSalt, newVerifier, newIntegritySalt);
} }

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
@ -51,12 +52,13 @@ public class BinaryRC4Encryptor extends Encryptor {
@Override @Override
public void confirmPassword(String password) { public void confirmPassword(String password) {
SecureRandom r = RandomSingleton.getInstance();
byte[] salt = new byte[16]; byte[] salt = new byte[16];
byte[] verifier = new byte[16]; byte[] verifier = new byte[16];
// using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed). // using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed).
RandomSingleton.getInstance().nextBytes(salt); r.nextBytes(salt);
RandomSingleton.getInstance().nextBytes(verifier); r.nextBytes(verifier);
confirmPassword(password, null, null, verifier, salt, null); confirmPassword(password, null, null, verifier, salt, null);
} }

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -57,11 +58,12 @@ public class CryptoAPIEncryptor extends Encryptor {
@Override @Override
public void confirmPassword(String password) { public void confirmPassword(String password) {
SecureRandom r = RandomSingleton.getInstance();
byte[] salt = new byte[16]; byte[] salt = new byte[16];
byte[] verifier = new byte[16]; byte[] verifier = new byte[16];
// using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed). // using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed).
RandomSingleton.getInstance().nextBytes(salt); r.nextBytes(salt);
RandomSingleton.getInstance().nextBytes(verifier); r.nextBytes(verifier);
confirmPassword(password, null, null, verifier, salt, null); confirmPassword(password, null, null, verifier, salt, null);
} }

View File

@ -28,6 +28,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
import javax.crypto.Cipher; import javax.crypto.Cipher;
@ -64,11 +65,12 @@ public class StandardEncryptor extends Encryptor {
@Override @Override
public void confirmPassword(String password) { public void confirmPassword(String password) {
// see [MS-OFFCRYPTO] - 2.3.3 EncryptionVerifier // see [MS-OFFCRYPTO] - 2.3.3 EncryptionVerifier
SecureRandom r = RandomSingleton.getInstance();
byte[] salt = new byte[16], verifier = new byte[16]; byte[] salt = new byte[16], verifier = new byte[16];
// using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed). // using a java.security.SecureRandom (and avoid allocating a new SecureRandom for each random number needed).
RandomSingleton.getInstance().nextBytes(salt); r.nextBytes(salt);
RandomSingleton.getInstance().nextBytes(verifier); r.nextBytes(verifier);
confirmPassword(password, null, null, salt, verifier, null); confirmPassword(password, null, null, salt, verifier, null);
} }