erlang java 加解密(2)消息摘要算法(MD、SHA、MAC)

java加解密相关资料和代码参考《java加密与解密的艺》http://snowolf.iteye.com/blog/379860

 

消息摘要算法包含MD、SHA、MAC共3大系列,常用于验证数据的完整性,是数字签名的核心算法

1、MD

MD(message digest)消息摘要算法,前身有MD2,MD3,MD4,不论哪一种MD算法,他们都需要或得一个随机长度的信息并产生一个128位的信息摘要,把128位二进制换算成十六进制,可以得到一个32位的字符串,所以我们见到的MD5算法的数字指纹都是32位的。消息摘要的主要特点就是对同一数据做多次摘要,其摘要值完全一致

 

 

MD java相关代码

 

import java.security.MessageDigest;
import java.security.Security;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
/**
 * MD(Message Digest 消息摘要算法)
 * 产生128位的二进制信息摘要,转换为16进制位32位
 * @author zhangzhiqiang
 */
public class MDCoder {
	
//	java6实现MD2、MD5	
	public static byte[] encodeMD2(byte[] data)throws Exception{
		//初始化MeddageDigest
		MessageDigest md = MessageDigest.getInstance("MD2");
		//执行消息摘要
		return md.digest(data);
	}
	
	public static byte[] encodeMD5(byte[] data)throws Exception{
		MessageDigest md = MessageDigest.getInstance("MD5");
		return md.digest(data);
	}

//---------------------------bouncy castle--------------------------------------------------	
//	bouncy castle实现MD4  是对java6 的补充
	public static byte[] encodeMD4(byte[] data)throws Exception{
		//加入Bouncy castle支持
		Security.addProvider(new BouncyCastleProvider());
		//初始化MeddageDigest
		MessageDigest md = MessageDigest.getInstance("MD4");
		return md.digest(data);
	}

	public static String encodeMD4Hex(byte[] data)throws Exception{
		byte[] b = encodeMD4(data);
		return new String(Hex.encode(b));
	}
	
//---------------------------commons codec--------------------------------------------------	
//commons codec仅实现MD5
	public static byte[] ccEncodeMD5(String str)throws Exception{
		return DigestUtils.md5(str);
	}
	
	public static String ccEncodeMD5Hex(String str)throws Exception{
		return DigestUtils.md5Hex(str);
	}
}

 md java测试类

 

import org.junit.*;
import static org.junit.Assert.*;
import org.bouncycastle.util.encoders.Hex;
import utils.Utils;
/**
 * @author zhiqiang
 *
 */
public class MDCoderTest {
	
	@Test
	public void testEncodeMD2()throws Exception{
		System.err.println("MD2消息摘要");
		String str="MD2消息摘要";
		byte[] data1 = MDCoder.encodeMD2(str.getBytes());
		byte[] data2 = MDCoder.encodeMD2(str.getBytes());
		assertArrayEquals(data1,data2);
		}
	@Test
	public void testEncodeMD5()throws Exception{
		System.err.println("MD5消息摘要");
		String str="MD5test";
		byte[] data1 = MDCoder.encodeMD5(str.getBytes());
		byte[] data2 = MDCoder.encodeMD5(str.getBytes());
		System.err.println(Utils.bytesToHexString(data1));
		System.err.println(new String(Hex.encode(data1)));
		Utils.bytesToHexString(data1);
		System.err.println();
		for(byte x:data1){
			System.err.print(x);
		}
		System.err.println();
		assertArrayEquals(data1,data2);
		}
	
	@Test
	public void testEncodeMD4()throws Exception{
		System.err.println("MD4消息摘要");
		String str="MD4test";
		byte[] data1 = MDCoder.encodeMD4(str.getBytes());
		byte[] data2 = MDCoder.encodeMD4(str.getBytes());
		System.err.println(Utils.bytesToHexString(data1));
		for(byte x:data1){
			System.err.print(x);
		}
		System.err.println();
		for(byte x:data1){
			System.err.print(x);
		}
		System.err.println();
		assertArrayEquals(data1,data2);
		}
	
	@Test
	public void testEncodeMD4Hex()throws Exception{
		System.err.println("MD4Hex消息摘要");
		String str="MD4test";
		String data1 = MDCoder.encodeMD4Hex(str.getBytes());
		String data2 = MDCoder.encodeMD4Hex(str.getBytes());
		System.err.println(data1);
		System.err.println(data2);
		assertEquals(data1,data2);
		}
	
	@Test
	public void testEncodeMD5Hex()throws Exception{
		System.err.println("MD5消息摘要");
		String str="MD5test";
		
		byte[] data1 = MDCoder.ccEncodeMD5(str) ;
		byte[] data2 = MDCoder.ccEncodeMD5(str) ;
		byte[] data5 = MDCoder.encodeMD5(str.getBytes());
		System.err.println(Utils.bytesToHexString(data1));
		System.err.println(Utils.bytesToHexString(data5));
		
		assertArrayEquals(data1,data2);
		
		System.err.println("MD5Hex消息摘要");
		String str1="MD5消息摘要";
		String data3 = MDCoder.ccEncodeMD5Hex(str1) ;
		String data4 = MDCoder.ccEncodeMD5Hex(str1) ;
		System.err.println(data3);
		System.err.println(data4);
		assertEquals(data3,data4);
		}
}

erlang MD

 

erlang 数据摘要非常简单,只是结果是二进制,要转换成16进制

 

 

md5()->
    Bin = crypto:md5("MD5"),
    bin2hex(Bin).

bin2hex(Bin)->
    List = lists:map(fun(X)->
            [hex(X div 16),hex(X rem 16)] end,binary_to_list(Bin)),
    lists:flatten(List).
        
hex(N) when N < 10 ->  
    $0+N; 
hex(N) when N >= 10, N < 16 ->    
    $a + (N-10).

md51()->
    <<Int:128/integer>> = crypto:md5("MD5"),
    lists:flatten(io_lib:format("~.16b",[Int])).

运行结果

14> c(md5).    
{ok,bin2hex}
15> md5:md51().
"7f138a09169b250e9dcb378140907378"
16>md5:md5(). 
"7f138a09169b250e9dcb378140907378"

 

另外  erlang:md5/1  erlang:md5_init/0  erlang:md5_undate/2  erlang:md5_final/1和crypto模块下的同名方法作用完全一样

 

同一数据 进行init  update  final之后得到的结果 和直接md5是一样的

 

18> Str = "MD5".
"MD5"
19> Con = crypto:md5_init().
<<1,35,69,103,137,171,205,239,254,220,186,152,118,84,50,
  16,0,0,0,0,0,0,0,0,0,5,52,46,55,...>>
20> NewCon = crypto:md5_update(Con,Str).
<<1,35,69,103,137,171,205,239,254,220,186,152,118,84,50,
  16,24,0,0,0,0,0,0,0,77,68,53,0,55,...>>
21> Data = crypto:md5_final(NewCon).
<<127,19,138,9,22,155,37,14,157,203,55,129,64,144,115,120>>
22> Data1 = crypto:md5(Str).
<<127,19,138,9,22,155,37,14,157,203,55,129,64,144,115,120>>
23> Data == Data1.
true
 
 

yishan

 

 


                            

你可能感兴趣的:(erlang)