Java对字符串数据进行MD5/SHA1哈希散列运算
- package cn.aibo.test;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class TestMd5AndSha1 {
- public static String md5(String data) throws NoSuchAlgorithmException {
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(data.getBytes());
- StringBuffer buf = new StringBuffer();
- byte[] bits = md.digest();
- for(int i=0;i<bits.length;i++){
- int a = bits[i];
- if(a<0) a+=256;
- if(a<16) buf.append("0");
- buf.append(Integer.toHexString(a));
- }
- return buf.toString();
- }
- public static String sha1(String data) throws NoSuchAlgorithmException {
- MessageDigest md = MessageDigest.getInstance("SHA1");
- md.update(data.getBytes());
- StringBuffer buf = new StringBuffer();
- byte[] bits = md.digest();
- for(int i=0;i<bits.length;i++){
- int a = bits[i];
- if(a<0) a+=256;
- if(a<16) buf.append("0");
- buf.append(Integer.toHexString(a));
- }
- return buf.toString();
- }
- public static void main(String[] args) throws NoSuchAlgorithmException{
- String data = "abc";
- //MD5
- System.out.println("MD5 : "+md5(data));
- //SHA1
- System.out.println("SHA1 : "+sha1(data));
- }
- }
运算结果
1 | MD5 : 900150983cd24fb0d6963f7d28e17f72 |
2 | SHA1 : a9993e364706816aba3e25717850c26c9cd0d89d |