Simple expample of MessageDigest

When use the encrypted arithmetic to encrypt some files.we can use the method create by MessageDigest:

package com.CAHelper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MyTest {

	public static void main(String args[]){
		String info = "This is my test info: Hello World";
		try {
			MessageDigest digester = MessageDigest.getInstance("SHA-1");
			System.out.println("Method is :"+digester.getAlgorithm());
			digester.update(info.getBytes());
			byte[] digeByte = digester.digest(); 
			System.out.println("Info is :"+byte2hex(digeByte));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String byte2hex(byte[] b){
		String hs = "";
		String stemp = "";
		System.out.println(b.length);
		for(int i=0;i<b.length;i++){
			stemp = Integer.toHexString(b[i]);
			if(stemp.length()==1){
				hs = hs +"0"+stemp;
			}else{
				hs = hs + stemp;
			}
			if(i<b.length-1){
				hs = hs+":";
			}
		}
		return stemp.toUpperCase();
	}
}

 

你可能感兴趣的:(java,Security)