티스토리 뷰
728x90
DigestUtils
package bluexmas.utils; import java.io.FileNotFoundException; import java.io.RandomAccessFile; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class DigestUtils { public static String extractStringHashSHA256(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; } public static String extractFileHashSHA256(String filename) throws Exception { String SHA = ""; int buff = 16384; try { RandomAccessFile file = new RandomAccessFile(filename, "r"); MessageDigest hashSum = MessageDigest.getInstance("SHA-256"); byte[] buffer = new byte[buff]; byte[] partialHash = null; long read = 0; // calculate the hash of the hole file for the test long offset = file.length(); int unitsize; while (read < offset) { unitsize = (int) (((offset - read) >= buff) ? buff : (offset - read)); file.read(buffer, 0, unitsize); hashSum.update(buffer, 0, unitsize); read += unitsize; } file.close(); partialHash = new byte[hashSum.getDigestLength()]; partialHash = hashSum.digest(); StringBuffer sb = new StringBuffer(); for(int i = 0 ; i < partialHash.length ; i++){ sb.append(Integer.toString((partialHash[i]&0xff) + 0x100, 16).substring(1)); } SHA = sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } return SHA; } public static void main(String[] args) throws Exception { System.out.println(extractStringHashSHA256("1")); System.out.println(extractFileHashSHA256("a.pdf")); } }
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Delphi Tip
- KOBA
- JavaScript
- koba2010
- 튜닝쇼 2008
- Spring MVC
- Spring
- Mac
- 서울오토살롱
- sas2009
- ubuntu
- 전예희
- MySQL
- android
- flex
- Java
- 일본여행
- 레이싱모델 익스트림 포토 페스티벌
- 송주경
- 지스타2007
- BPI-M4
- NDK
- 동경
- Linux
- oracle
- SAS
- Xcode
- ble
- ffmpeg
- Delphi
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함