`
tanglei198577
  • 浏览: 59735 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

Simple expample of MessageDigest

    博客分类:
  • java
阅读更多

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();
	}
}

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics