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