달력
- 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
'sha'에 해당되는 글 1건
-
2008.09.23
[JAVA] SHA-1 암호화 하기
import java.io.*;
import java.security.*;
import sun.misc.*;
public class TestSHA{
public static void main(String[] args) throws Exception {
byte[] txtByte = "테스트테스트".getBytes();
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(txtByte);
byte[] digest = md.digest();
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(digest);
// should be 20 bytes, 160 bits long
System.out.println( digest.length );
// dump out the hash
for ( byte b : digest )
{
System.out.print( Integer.toHexString( b & 0xff ) );
}
//String result = new String(digest.toCha);
System.out.println("\r\n" + toString(digest, 0, digest.length));
//System.out.println("hexaString : " + HexString.bufferToHex(md.digest()));
}
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
public static final String toString(byte[] ba, int offset, int length) {
char[] buf = new char[length * 2];
for (int i = 0, j = 0, k; i < length; ) {
k = ba[offset + i++];
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
buf[j++] = HEX_DIGITS[ k & 0x0F];
}
return new String(buf);
}
}
Posted by tornado