SHA256을 사용하면 성능 이슈가 있다고 하여 SHA1을 사용하는 곳도 있긴 하다. 필요한
알고리즘을 사용하면 될 것 같다.
SHA256을 사용하려면 SHA-1 대신 SHA-256을 넣고 사용하면 된다.
| 
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 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 | 
public class Sha1 { 
    private static final String ALGORITHM = "SHA-1"; 
    public static byte[] getHash(byte[] input) { 
        try { 
            MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
            return md.digest(input); 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
            return null; 
        } 
    } 
    public static byte[] getHash(InputStream input) throws IOException { 
        try { 
            MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
            int read = -1; 
            byte[] buffer = new byte[1024]; 
            while ((read = input.read(buffer)) != -1) { 
                md.update(buffer, 0, read); 
            } 
            return md.digest(); 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
            return null; 
        } 
    } 
    public static byte[] getHash(File file) throws IOException { 
        byte[] hash = null; 
        BufferedInputStream bis = null; 
        try { 
            bis = new BufferedInputStream(new FileInputStream(file)); 
            hash = getHash(bis); 
        } finally { 
            if (bis != null) try { bis.close(); } catch(IOException ie) {} 
        } 
        return hash; 
    } 
    public static String getHashHexString(byte[] input) { 
        byte[] hash = getHash(input); 
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < hash.length; i++) {  
             sb.append(Integer.toString((hash[i] & 0xf0) >> 4, 16));  
             sb.append(Integer.toString(hash[i] & 0x0f, 16)); 
        }  
        return sb.toString(); 
    } 
    public static String getHashHexString(String input) { 
        return getHashHexString(input.getBytes()); 
    } 
    public static String getHashHexString(String input, String charsetName) throws UnsupportedEncodingException { 
        return getHashHexString(input.getBytes(charsetName)); 
    } 
    public static String getHashHexString(InputStream input) throws IOException { 
        byte[] hash = getHash(input); 
        StringBuffer sb = new StringBuffer(hash.length * 2);  
        for (int i = 0; i < hash.length; i++) {  
             sb.append(Integer.toString((hash[i] & 0xf0) >> 4, 16));  
             sb.append(Integer.toString(hash[i] & 0x0f, 16)); 
        }  
        return sb.toString(); 
    } 
    public static String getHashHexString(File file) throws IOException { 
        byte[] hash = getHash(file); 
        StringBuffer sb = new StringBuffer(hash.length * 2); 
        for (int i = 0; i < hash.length; i++) {  
             sb.append(Integer.toString((hash[i] & 0xf0) >> 4, 16));  
             sb.append(Integer.toString(hash[i] & 0x0f, 16)); 
        } 
        return sb.toString(); 
    } 
    public static void main(String args[]){ 
        String path = "C:/test.txt"; 
        File file = new File(path); 
        try { 
            System.out.println(getHashHexString(file)); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
} | 
댓글 없음:
댓글 쓰기