1. Message Digest Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | static String getDigest(String method, String str) { String digest = null; try{ MessageDigest md = MessageDigest.getInstance(method); md.update(str.getBytes()); byte byteData[] = md.digest(); StringBuilder sb = new StringBuilder(); for(int i = 0 ; i < byteData.length ; i++){ sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1)); } digest = sb.toString(); }catch(NoSuchAlgorithmException e){ e.printStackTrace(); } return digest; } static String getMD5(String str){ return getDigest("MD5", str); } static String getSHA256(String str){ return getDigest("SHA-256", str); } public static void main( String[] args ) { System.out.println("MD5: " + getMD5("MD5 Test")); System.out.println("----------"); System.out.println("SHA256: " + getSHA256("SHA Test")); } |
Result:
MD5: 48a491f89c683690789717a910709287 ---------- SHA256: 58a4ddfa25f98d22760369485a917c19f56591080760848bab66eb3d8cf8074f
댓글 없음:
댓글 쓰기