2019년 3월 25일 월요일

[Java] MD5 와 SHA-256 예제

Java / MD5 & SHA-256 method 예제.

MD5

public String testMD5(String str){
String MD5 = "";
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < byteData.length ; i++){
sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
}
MD5 = sb.toString();

}catch(NoSuchAlgorithmException e){
e.printStackTrace();
MD5 = null;
}
return MD5;
}

-------------------------------------------------------------------------------------------------
SHA-256

public String testSHA256(String str){
String SHA = "";
try{
MessageDigest sh = MessageDigest.getInstance("SHA-256");
sh.update(str.getBytes());
byte byteData[] = sh.digest();
StringBuffer sb = new StringBuffer();
for(int i = 0 ; i < byteData.length ; i++){
sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
}
SHA = sb.toString();

}catch(NoSuchAlgorithmException e){
e.printStackTrace();
SHA = null;
}
return SHA;
}

댓글 없음:

댓글 쓰기