1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.repository.utils;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import org.codehaus.plexus.util.IOUtil;
31
32
33
34
35
36
37
38 public final class DigestUtils
39 {
40 private static final int CHECKSUM_BUFFER_SIZE = 16384;
41
42 private static final int BYTE_MASK = 0xFF;
43
44 private DigestUtils()
45 {
46 }
47
48 public static String createChecksum( File file, String algorithm )
49 throws IOException, NoSuchAlgorithmException
50 {
51 MessageDigest digest = MessageDigest.getInstance( algorithm );
52
53 InputStream fis = new FileInputStream( file );
54 try
55 {
56 byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
57 int numRead;
58 do
59 {
60 numRead = fis.read( buffer );
61 if ( numRead > 0 )
62 {
63 digest.update( buffer, 0, numRead );
64 }
65 }
66 while ( numRead != -1 );
67 }
68 finally
69 {
70 IOUtil.close( fis );
71 }
72
73 return byteArrayToHexStr( digest.digest() );
74 }
75
76 public boolean verifyChecksum( File file, String checksum, String algorithm )
77 throws NoSuchAlgorithmException, IOException
78 {
79 boolean result = true;
80
81 String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
82
83 Matcher m = Pattern.compile( algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-zA-Z0-9]+)" )
84 .matcher( trimmedChecksum );
85 if ( m.matches() )
86 {
87 String filename = m.group( 1 );
88 if ( !filename.equals( file.getName() ) )
89 {
90
91 result = false;
92 }
93 trimmedChecksum = m.group( 2 );
94 }
95 else
96 {
97
98 m = Pattern.compile( "([a-zA-Z0-9]+)\\s\\*?(.+)" ).matcher( trimmedChecksum );
99 if ( m.matches() )
100 {
101 String filename = m.group( 2 );
102 if ( !filename.equals( file.getName() ) )
103 {
104
105 result = false;
106 }
107 trimmedChecksum = m.group( 1 );
108 }
109 }
110
111 if ( result )
112 {
113
114 String sum = createChecksum( file, algorithm );
115 result = trimmedChecksum.toUpperCase().equals( sum.toUpperCase() );
116 }
117 return result;
118 }
119
120
121
122
123
124
125
126 private static String byteArrayToHexStr( byte[] data )
127 {
128 String output = "";
129
130 for ( int cnt = 0; cnt < data.length; cnt++ )
131 {
132
133 int tempInt = data[cnt] & BYTE_MASK;
134
135
136 String tempStr = Integer.toHexString( tempInt );
137
138
139 if ( tempStr.length() == 1 )
140 {
141 tempStr = "0" + tempStr;
142 }
143
144
145 output = output + tempStr;
146 }
147
148 return output.toUpperCase();
149 }
150 }