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.io.UnsupportedEncodingException;
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 try
161 {
162 digest.update( string.getBytes( "UTF-8" ) );
163 }
164 catch ( UnsupportedEncodingException e )
165 {
166 throw new IllegalStateException( e );
167 }
168 }
169 }
170 }
171 }
172
173
174
175
176
177
178 public void update( char... chars )
179 {
180 if ( chars != null )
181 {
182 for ( char c : chars )
183 {
184 digest.update( (byte) ( c >> 8 ) );
185 digest.update( (byte) ( c & 0xFF ) );
186 }
187 }
188 }
189
190
191
192
193
194
195 public void update( byte... bytes )
196 {
197 if ( bytes != null )
198 {
199 digest.update( bytes );
200 }
201 }
202
203 private String digest()
204 {
205 byte[] bytes = digest.digest();
206 StringBuilder buffer = new StringBuilder( bytes.length * 2 );
207 for ( byte aByte : bytes )
208 {
209 int b = aByte & 0xFF;
210 if ( b < 0x10 )
211 {
212 buffer.append( '0' );
213 }
214 buffer.append( Integer.toHexString( b ) );
215 }
216 return buffer.toString();
217 }
218
219 }