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
26
27
28
29
30 class SimpleDigest
31 {
32
33 private static final String[] HASH_ALGOS = new String[] { "SHA-1", "MD5" };
34
35 private MessageDigest digest;
36
37 private long hash;
38
39 SimpleDigest()
40 {
41 for ( String hashAlgo : HASH_ALGOS )
42 {
43 try
44 {
45 digest = MessageDigest.getInstance( hashAlgo );
46 hash = 0;
47 break;
48 }
49 catch ( NoSuchAlgorithmException ne )
50 {
51 digest = null;
52 hash = 13;
53 }
54 }
55 }
56
57 public void update( String data )
58 {
59 if ( data == null || data.length() <= 0 )
60 {
61 return;
62 }
63 if ( digest != null )
64 {
65 digest.update( data.getBytes( StandardCharsets.UTF_8 ) );
66 }
67 else
68 {
69 hash = hash * 31 + data.hashCode();
70 }
71 }
72
73 @SuppressWarnings( "checkstyle:magicnumber" )
74 public String digest()
75 {
76 if ( digest != null )
77 {
78 StringBuilder buffer = new StringBuilder( 64 );
79
80 byte[] bytes = digest.digest();
81 for ( byte aByte : bytes )
82 {
83 int b = aByte & 0xFF;
84
85 if ( b < 0x10 )
86 {
87 buffer.append( '0' );
88 }
89
90 buffer.append( Integer.toHexString( b ) );
91 }
92
93 return buffer.toString();
94 }
95 else
96 {
97 return Long.toHexString( hash );
98 }
99 }
100
101 }