То, о чем вы говорите - это солить хеш.
Вот как я это делаю.
public static byte[] getSecure8ByteSalt(){
SecureRandom random = null;
try {
random = SecureRandom.getInstance("SHA1PRNG");
byte [] bSalt = new byte[8];
random.nextBytes(bSalt);
return bSalt;
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(),e);
}
return new byte[]{
(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x34, (byte)0xE3, (byte)0x03
};
}
Метод, который вызывает соль, называется хешем:
private void hash(String passwd, int hashType){
byte[] bSalt = new byte[8];
try {
if(this.salt == null){
bSalt = getSecure8ByteSalt();
}
else{
bSalt = base64ToByte(salt);
}
} catch (IOException e1) {
log.error(e1.getMessage(),e1);
return;
}
byte[] bDigest=null;
try {
bDigest = getHash(ITERATION_NUMBER,passwd,bSalt,hashType);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(),e);
}
String sDigest = byteToBase64(bDigest);
if(this.salt == null)
setSalt(byteToBase64(bSalt));
setPasswordHash(sDigest);
}
Метод от байта к основанию 64:
public static byte[] base64ToByte(String data) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(data);
}
public static String byteToBase64(byte[] data){
BASE64Encoder endecoder = new BASE64Encoder();
return endecoder.encode(data);
}
метод getHash:
private byte[] getHash(int iterationNb, String password, byte[] salt, int hashType) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance(HASH_TYPE[hashType]);
digest.reset();
digest.update(salt);
byte[] input = null;
try {
input = digest.digest(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(),e);
}
for (int i = 0; i < iterationNb; i++) {
digest.reset();
input = digest.digest(input);
}
return input;
}