소프트웨어 개발/Java - Basic

sha256암호화

늘근이 2014. 11. 3. 18:43

package com.orakgarak.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CommonUtil {

 public static String toSHA256(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;
 }
 
}