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