1 package org.apache.maven.it;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.security.DigestInputStream;
25 import java.security.MessageDigest;
26
27
28
29
30 class ItUtils
31 {
32
33 public static String calcHash( File file, String algo )
34 throws Exception
35 {
36 MessageDigest digester = MessageDigest.getInstance( algo );
37
38 DigestInputStream dis;
39 try ( FileInputStream is = new FileInputStream( file ) )
40 {
41 dis = new DigestInputStream( is, digester );
42
43 for ( byte[] buffer = new byte[1024 * 4]; dis.read( buffer ) >= 0; )
44 {
45
46 }
47 }
48
49 byte[] digest = digester.digest();
50
51 StringBuilder hash = new StringBuilder( digest.length * 2 );
52
53 for ( byte aDigest : digest )
54 {
55 int b = aDigest & 0xFF;
56
57 if ( b < 0x10 )
58 {
59 hash.append( '0' );
60 }
61
62 hash.append( Integer.toHexString( b ) );
63 }
64
65 return hash.toString();
66 }
67
68 }