消息摘要算法
package com.kaishengit.security; import java.security.MessageDigest; import java.security.Security; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.digests.SHA1Digest; import org.bouncycastle.jce.provider.BouncyCastleProvider; /** * 消息摘要算法 * @author kdj * 创建时间:2018年4月11日 */ public class MessageDigestTest { private static String securityStr = "测试"; public static void main(String[] args) { //包含三种:MD5,SHA,MAC //消息摘要算法是数字签名的核心算法 // MD5Encode(); // MD4Encode(); // MD5ccEncode(); // SHAJdkEncode(); // SHABCEncode(); // SHACCEncode(); MACJdkEncode(); } //MD是128位摘要信息,MD2(JDK),MD4(bouncy),MD5(JDK) //单向加密算法 public static String MD5Encode(){ try { MessageDigest dig = MessageDigest.getInstance("MD5"); byte[] encode = dig.digest(securityStr.getBytes()); String encodeStr = Hex.encodeHexString(encode); System.out.println("md5 jdk encode:"+encodeStr); return encodeStr; } catch (Exception e) { } return ""; } /** * 因为md4在jdk中没有实现,所有可以往安全类中加入bouncy,使jdk也可以使用MD4 * @return */ public static String MD4Encode(){ try { Security.addProvider(new BouncyCastleProvider()); MessageDigest dig = MessageDigest.getInstance("MD4"); byte[] encode = dig.digest(securityStr.getBytes()); String encodeStr = Hex.encodeHexString(encode); System.out.println("md4 encode:"+encodeStr); return encodeStr; } catch (Exception e) { } return ""; } /** * 简化了调用的复杂性,利用common * @return */ public static String MD5ccEncode(){ //调用的是 System.out.println("md5 cc encode:"+DigestUtils.md5Hex(securityStr.getBytes())); return ""; } //----------------------------------------------------------------------------- //SHA安全散列 public static String SHAJdkEncode(){ try { MessageDigest dig = MessageDigest.getInstance("SHA"); byte[] arr = dig.digest(securityStr.getBytes()); //转换16进制为字符串 String encode = Hex.encodeHexString(arr); System.out.println("SHA JDK Encode:"+encode); return encode; } catch (Exception e) { } return ""; } public static String SHABCEncode(){ try { Digest digest = new SHA1Digest(); digest.update(securityStr.getBytes(), 0, securityStr.getBytes().length); byte[] sha1Byte = new byte[digest.getDigestSize()]; digest.doFinal(sha1Byte, 0); String shaStr = org.bouncycastle.util.encoders.Hex.toHexString(sha1Byte); System.out.println("SHA BC Encode:"+shaStr); return shaStr; } catch (Exception e) { } return ""; } public static String SHACCEncode(){ byte[] arr = DigestUtils.sha1(securityStr.getBytes()); String encode = Hex.encodeHexString(arr); System.out.println("SHA CC Encode:"+encode); return encode; } //---------------------------------------------------------------------------- //MAC public static String MACJdkEncode(){ try { KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5"); //产生秘钥 SecretKey secretKey = keyGenerator.generateKey(); //获取秘钥 byte[] key = secretKey.getEncoded(); //还原秘钥 SecretKey restSecretKey = new SecretKeySpec(key, "HmacMD5"); //实例化MAC Mac mac = Mac.getInstance(restSecretKey.getAlgorithm()); mac.init(restSecretKey); byte[] hmacMD5Byte = mac.doFinal(securityStr.getBytes()); String encode = Hex.encodeHexString(hmacMD5Byte); System.out.println("MAC JDK Encode:"+encode); return encode; } catch (Exception e) { } return ""; } public static String MACCCEncode(){ return ""; } }