package com.imooc.security.md;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
/**
* 消息摘要加密算法
* @author Bingo_Ge
*
*/
public class ImoocMD {
private static String src = "imooc security md";
public static void main(String[] args) {
jdkMD5();
jdkMD2();
bcMD4();
bcMD5();
ccMD5();
ccMD2();
}
private static void ccMD2() {
System.out.println("ccMD2:" + DigestUtils.md2Hex(src.getBytes()));
}
private static void ccMD5() {
System.out.println("ccMD5:" + DigestUtils.md5Hex(src.getBytes()));
}
private static void bcMD5() {
Digest digest = new MD5Digest();
digest.update(src.getBytes(), 0, src.getBytes().length);
byte[] md5Bytes = new byte[digest.getDigestSize()];
digest.doFinal(md5Bytes, 0);
System.out.println("bcMD5:" + org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes));
}
private static void bcMD4() {
Digest digest = new MD4Digest();
digest.update(src.getBytes(), 0, src.getBytes().length);
byte[] md4Bytes = new byte[digest.getDigestSize()];
digest.doFinal(md4Bytes, 0);
System.out.println("bcMD4:" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
}
private static void jdkMD2() {
try {
MessageDigest md = MessageDigest.getInstance("MD2");
byte[] md2Bytes = md.digest(src.getBytes());
System.out.println("jkdMD2:" + Hex.encodeHexString(md2Bytes));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static void jdkMD5() {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5Bytes = md.digest(src.getBytes());
System.out.println("jkdMD5:" + Hex.encodeHexString(md5Bytes));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
消息摘要加密算法MD
最新推荐文章于 2023-01-11 10:07:58 发布