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