数字摘要
public static void main(String[] args) {
String old ="";
try {
for (int i = 0; i < 10; i++) {
String beforeDegist = "asdf";
System.out.println("摘要前:" + beforeDegist);
// 初始信息要转换成字节流的形式
byte[] plainText;
plainText = beforeDegist.getBytes("UTF-8");
// 使用getInstance("算法")来获得消息摘要,这里使用SHA-1的160位算法
// MessageDigest messageDigest =
// MessageDigest.getInstance("SHA-1");
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
// System.out.println("\n" +
// messageDigest.getProvider().getInfo());
// 开始使用算法
messageDigest.update(plainText);
String afterDegist = new String(messageDigest.digest(),"UTF8");
System.out.println("摘要后:" + afterDegist.equals(old));
old = afterDegist;
// 输出算法运算结果
// StringBuffer afterDegist = new StringBuffer();
// afterDegist.append(messageDigest.digest());//, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}