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 public String digest()
73 {
74 if ( digest != null )
75 {
76 StringBuilder buffer = new StringBuilder( 64 );
77
78 byte[] bytes = digest.digest();
79 for ( byte aByte : bytes )
80 {
81 int b = aByte & 0xFF;
82
83 if ( b < 0x10 )
84 {
85 buffer.append( '0' );
86 }
87
88 buffer.append( Integer.toHexString( b ) );
89 }
90
91 return buffer.toString();
92 }
93 else
94 {
95 return Long.toHexString( hash );
96 }
97 }
98
99 }