'SHA1'에 해당되는 글 1건

  1. 2016.07.08 [Java] Hash Algorithm 테스트

[Java] Hash Algorithm 테스트

ITWeb/개발일반 2016.07.08 11:19

그냥 인터넷에 돌아 다니는 코드 가져가 기록해 봤습니다.


[코드]

public class HashGenerator {

public static void main (String[] args)
throws NoSuchAlgorithmException {

String md2 = getHash("test", "md2");
String md5 = getHash("test", "md5");
String sha1 = getHash("test", "sha1");
String sha256 = getHash("test", "sha-256");
String sha384 = getHash("test", "sha-384");
String sha512 = getHash("test", "sha-512");

// “MD2″, “MD5″, “SHA1″, “SHA-256″, “SHA-384″, “SHA-512″
System.out.println("MD2 : [" + md2 + "](" + md2.length() + ")");
System.out.println("MD5 : [" + md5 + "](" + md5.length() + ")");
System.out.println("SHA1 : [" + sha1 + "](" + sha1.length() + ")");
System.out.println("SHA-256 : [" + sha256 + "](" + sha256.length() + ")");
System.out.println("SHA-384 : [" + sha384 + "](" + sha384.length() + ")");
System.out.println("SHA-512 : [" + sha512 + "](" + sha512.length() + ")");
}

public static String getHash(String message, String algorithm)
throws NoSuchAlgorithmException {

try {
byte[] buffer = message.getBytes();
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(buffer);
byte[] digest = md.digest();
String hex = "";

for(int i = 0 ; i < digest.length ; i++) {
int b = digest[i] & 0xff;
if (Integer.toHexString(b).length() == 1) hex = hex + "0";
hex = hex + Integer.toHexString(b);
}

return hex;
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}

return null;
}
}


[결과]

MD2     : [dd34716876364a02d0195e2fb9ae2d1b](32)

MD5     : [098f6bcd4621d373cade4e832627b4f6](32)

SHA1    : [a94a8fe5ccb19ba61c4c0873d391e987982fbbd3](40)

SHA-256 : [9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08](64)

SHA-384 : [768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9](96)

SHA-512 : [ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff](128)


저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

티스토리 툴바