SUN 提供的消息摘要算法 MD与SHA

SUN 提供的消息摘要算法

接收任意大小的数据,并输出固定长度的哈希值

1. MD2,MD5,SHA-1,SHA-256,SHA-384,SHA-512

      MD5: 128 个比特位

       SHA: 160,256,384,512个比特位


2. package: java.security.MessageDigest


3. Example
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;

public class test {

	/**
	 * @param args
	 * @throws NoSuchAlgorithmException 
	 */
	public static void main(String[] args) throws NoSuchAlgorithmException {
		
		MessageDigest md;
        md = MessageDigest.getInstance("SHA-1");
        /* MD2
         * MD5
         * SHA1 OR SHA-1
         * SHA-256
         * SHA-384
         * SHA-512
         */
        String str="ni hao";
        
        //md.update(str.getBytes());
        //byte[] byteDigest = md.digest();
        
        byte[] byteDigest = md.digest(str.getBytes());
        
        Hex hex=new Hex();
        
        byte[] hexStr=hex.encode(byteDigest);
        
        System.out.println(new String(hexStr));
        
	}

}

你可能感兴趣的:(java,apache,算法,Security,sun)