get string MD5

oxoxooxx發表於2011-09-09
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class cassandraMD5{
static final Scanner cin=new Scanner(System.in);

public static byte[] hash(String type, byte[]... data){
byte[] result = null;
try{
MessageDigest messageDigest = MessageDigest.getInstance(type);
for(byte[] block : data)
messageDigest.update(block);
result = messageDigest.digest();
}
catch (Exception e){
throw new RuntimeException(e);
}
return result;
}

public static BigInteger hash(String data){
byte[] result = null;
try {
result = hash("MD5", data.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BigInteger hash = new BigInteger(result);
return hash.abs();
}

public static void main(String[] args){
while(cin.hasNext()){
String str1=cin.next();
BigInteger a= hash(str1);
System.out.println(a);
}
BigInteger a= hash(str2);
System.out.println(a);
}
}[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23937368/viewspace-1054900/,如需轉載,請註明出處,否則將追究法律責任。

相關文章