1 package org.eclipse.aether.repository;
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 import org.eclipse.aether.RepositorySystemSession;
27
28
29
30
31
32 public final class AuthenticationDigest
33 {
34
35 private final MessageDigest digest;
36
37 private final RepositorySystemSession session;
38
39 private final RemoteRepository repository;
40
41 private final Proxy proxy;
42
43
44
45
46
47
48
49
50
51 public static String forRepository( RepositorySystemSession session, RemoteRepository repository )
52 {
53 String digest = "";
54 Authentication auth = repository.getAuthentication();
55 if ( auth != null )
56 {
57 AuthenticationDigest authDigest = new AuthenticationDigest( session, repository, null );
58 auth.digest( authDigest );
59 digest = authDigest.digest();
60 }
61 return digest;
62 }
63
64
65
66
67
68
69
70
71
72 public static String forProxy( RepositorySystemSession session, RemoteRepository repository )
73 {
74 String digest = "";
75 Proxy proxy = repository.getProxy();
76 if ( proxy != null )
77 {
78 Authentication auth = proxy.getAuthentication();
79 if ( auth != null )
80 {
81 AuthenticationDigest authDigest = new AuthenticationDigest( session, repository, proxy );
82 auth.digest( authDigest );
83 digest = authDigest.digest();
84 }
85 }
86 return digest;
87 }
88
89 private AuthenticationDigest( RepositorySystemSession session, RemoteRepository repository, Proxy proxy )
90 {
91 this.session = session;
92 this.repository = repository;
93 this.proxy = proxy;
94 digest = newDigest();
95 }
96
97 private static MessageDigest newDigest()
98 {
99 try
100 {
101 return MessageDigest.getInstance( "SHA-1" );
102 }
103 catch ( NoSuchAlgorithmException e )
104 {
105 try
106 {
107 return MessageDigest.getInstance( "MD5" );
108 }
109 catch ( NoSuchAlgorithmException ne )
110 {
111 throw new IllegalStateException( ne );
112 }
113 }
114 }
115
116
117
118
119
120
121 public RepositorySystemSession getSession()
122 {
123 return session;
124 }
125
126
127
128
129
130
131
132 public RemoteRepository getRepository()
133 {
134 return repository;
135 }
136
137
138
139
140
141
142 public Proxy getProxy()
143 {
144 return proxy;
145 }
146
147
148
149
150
151
152 public void update( String... strings )
153 {
154 if ( strings != null )
155 {
156 for ( String string : strings )
157 {
158 if ( string != null )
159 {
160 digest.update( string.getBytes( StandardCharsets.UTF_8 ) );
161 }
162 }
163 }
164 }
165
166
167
168
169
170
171 @SuppressWarnings( "checkstyle:magicnumber" )
172 public void update( char... chars )
173 {
174 if ( chars != null )
175 {
176 for ( char c : chars )
177 {
178 digest.update( (byte) ( c >> 8 ) );
179 digest.update( (byte) ( c & 0xFF ) );
180 }
181 }
182 }
183
184
185
186
187
188
189 public void update( byte... bytes )
190 {
191 if ( bytes != null )
192 {
193 digest.update( bytes );
194 }
195 }
196
197 @SuppressWarnings( "checkstyle:magicnumber" )
198 private String digest()
199 {
200 byte[] bytes = digest.digest();
201 StringBuilder buffer = new StringBuilder( bytes.length * 2 );
202 for ( byte aByte : bytes )
203 {
204 int b = aByte & 0xFF;
205 if ( b < 0x10 )
206 {
207 buffer.append( '0' );
208 }
209 buffer.append( Integer.toHexString( b ) );
210 }
211 return buffer.toString();
212 }
213
214 }