1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.nio.charset.StandardCharsets;
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.Arrays;
26
27
28
29
30
31 class SimpleDigest
32 {
33
34 private static final String[] HASH_ALGOS = new String[] { "SHA-1", "MD5" };
35
36 private final MessageDigest digest;
37
38 SimpleDigest()
39 {
40 MessageDigest md = null;
41 for ( String hashAlgo : HASH_ALGOS )
42 {
43 try
44 {
45 md = MessageDigest.getInstance( hashAlgo );
46 break;
47 }
48 catch ( NoSuchAlgorithmException ne )
49 {
50 }
51 }
52 if ( md == null )
53 {
54 throw new IllegalStateException( "Not supported digests: " + Arrays.toString( HASH_ALGOS ) );
55 }
56 this.digest = md;
57 }
58
59 public void update( String data )
60 {
61 if ( data == null || data.length() <= 0 )
62 {
63 return;
64 }
65 digest.update( data.getBytes( StandardCharsets.UTF_8 ) );
66 }
67
68 @SuppressWarnings( "checkstyle:magicnumber" )
69 public String digest()
70 {
71 StringBuilder buffer = new StringBuilder( 64 );
72
73 byte[] bytes = digest.digest();
74 for ( byte aByte : bytes )
75 {
76 int b = aByte & 0xFF;
77
78 if ( b < 0x10 )
79 {
80 buffer.append( '0' );
81 }
82
83 buffer.append( Integer.toHexString( b ) );
84 }
85
86 return buffer.toString();
87 }
88 }