1 package org.apache.maven.it;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * @author Benjamin Bentmann
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 // just read it
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 }