commons-codecのいくつかの応用
TestCodecDemo.JAvaコード:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
public class TestCodecDemo {
/**
* MD5
* */
private static String getMd5Pwd(String password){
String returnStr = DigestUtils.md5Hex(password);
System.out.println(returnStr);
return DigestUtils.md5Hex(returnStr);
}
/**
* SHA1
* */
private static String getSha1Pwd(String password){
String returnStr = DigestUtils.shaHex(password);
System.out.println(returnStr);
return returnStr;
}
/**
* BASE64
* */
private static String getBase64Pwd(String password){
byte[] b = Base64.encodeBase64(password.getBytes(), true);
String returnStr = new String(b);
System.out.println(returnStr);
return returnStr;
}
/**
* BASE64
* */
private static String getUnBase64Pwd(String password){
byte[] b = Base64.decodeBase64(password.getBytes());
String returnStr = new String(b);
System.out.println(returnStr);
return returnStr;
}
/**
* @param args
*/
public static void main(String[] args) {
String password = "abc";
TestCodecDemo.getMd5Pwd(password);
TestCodecDemo.getSha1Pwd(password);
TestCodecDemo.getBase64Pwd(password);
TestCodecDemo.getUnBase64Pwd("YWJj");
}
}