JCA的message digest介紹:
1.使用SHA-256
2.加入salt
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;
/**
*
* @author andy
*/
public class MessageDigestExample {
public static void main(String args[]) {
// put code here to create a MD5 hash with a salt
MessageDigest md=null;
byte password[]={'p','a','s','s','w','o','r','d'};
try {
md= MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
md.update(password);
byte[] salt=new byte[8];
SecureRandom random=null;
try {
random= SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
random.nextBytes(salt);
md.update(salt);
byte[] hash=md.digest();
String encodingString=new String(Base64.encodeBase64(hash));
System.out.println("salt please:"+encodingString);
}
}
沒有留言:
張貼留言