Skip to content

Commit 6fe135a

Browse files
committed
Added constants and key factory for key derivation
1 parent 66148ff commit 6fe135a

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

‎src/main/java/CryptoResource.java‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import javax.crypto.Cipher;
22
import javax.crypto.NoSuchPaddingException;
3+
import javax.crypto.SecretKeyFactory;
34
import javax.crypto.spec.IvParameterSpec;
45
import java.security.NoSuchAlgorithmException;
56

67
/* Class that manages setting up cryptographic resources */
78
public abstract class CryptoResource {
89

910
protected static final int INIT_VECTOR_SIZE = 16;
11+
protected static final int SALT_SIZE = 8;
12+
protected static final int ITERATION_COUNT = 65536;
13+
protected static final int KEY_LENGTH = 256;
14+
1015
private static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5PADDING";
16+
private static final String KEY_FACTORY_ALGORITHM = "PBKDF2WithHmacSHA256";
17+
protected static final String KEY_SPEC_ALGORITHM = "AES";
18+
protected static final String RANDOM_SECURE_ALGORITHM = "SHA1PRNG";
19+
1120
protected final Cipher cipher;
21+
protected final SecretKeyFactory secretKeyFactory;
1222

1323
/* Initialises cryptographic resources */
1424
public CryptoResource() throws CryptoException {
1525
try {
1626
cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
27+
secretKeyFactory = SecretKeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
1728
} catch (NoSuchAlgorithmException e) {
1829
throw new CryptoException("Algorithm not found for " + CIPHER_TRANSFORMATION);
1930
} catch (NoSuchPaddingException e) {
2031
throw new CryptoException("Padding scheme not found for " + CIPHER_TRANSFORMATION);
2132
}
2233
}
2334

24-
/* Pads a password to 16 bytes with spaces */
25-
protected String padPasswordTo16Bytes(String password) {
26-
StringBuilder keyBuilder = new StringBuilder(password);
27-
while (keyBuilder.length() < 16)
28-
keyBuilder.append(" ");
29-
return keyBuilder.toString();
30-
}
31-
3235
}

0 commit comments

Comments
 (0)