...
A good practice is to always append a salt to the password being hashed. A salt is a unique (often sequential) or randomly generated piece of data that is stored along with the hash value. The use of a salt helps prevent brute-force attacks against the hash value, provided that the salt is long enough to generate sufficient entropy (shorter salt values cannot significantly slow down a brute-force attack). Each password should have its own salt associated with it. If a single salt were used for more than one password, two users would be able to see whether their passwords are the same.
The choice of hash function and salt length presents a trade-off between security and performance. Increasing the effort required for effective brute-force attacks by choosing a stronger hash function can also increase the time required to validate a password. Increasing the length of the salt makes brute-force attacks more difficult , but requires additional storage space.
...
This noncompliant code example encrypts and decrypts the password stored in password.bin
using a symmetric key algorithm.:
Code Block | ||
---|---|---|
| ||
public final class Password { private void setPassword(byte[] pass) throws Exception { // Arbitrary encryption scheme bytes[] encrypted = encrypt(pass); clearArray(pass); // Encrypted password to password.bin saveBytes(encrypted,"password.bin"); clearArray(encrypted); } boolean checkPassword(byte[] pass) throws Exception { // Load the encrypted password byte[] encrypted = loadBytes("password.bin"); byte[] decrypted = decrypt(encrypted); boolean arraysEqual = Arrays.equal(decrypted, pass); clearArray(decrypted); clearArray(pass); return arraysEqual; } private void clearArray(byte[] a) { for (int i = 0; i < a.length; i++) { a[i] = 0; } } } |
...
Although this approach solves the decryption problem from the previous noncompliant code example, this program may inadvertently store the passwords as cleartext in memory. Java String
objects are immutable , and can be copied and internally stored by the Java Virtual Machine. Consequently, Java lacks a mechanism to securely erase a password once it has been stored in a String
. See 01. Limit the lifetime of sensitive data for more information.
...