jtulach@44
|
1 |
/*
|
jtulach@44
|
2 |
* To change this template, choose Tools | Templates
|
jtulach@44
|
3 |
* and open the template in the editor.
|
jtulach@44
|
4 |
*/
|
jtulach@44
|
5 |
|
jtulach@44
|
6 |
package org.apidesign.impl.security.spi;
|
jtulach@44
|
7 |
|
jtulach@44
|
8 |
import org.apidesign.impl.security.friendapi.DigestImplementation;
|
jtulach@44
|
9 |
import org.apidesign.impl.security.friendapi.DigestProvider;
|
jtulach@44
|
10 |
import java.nio.ByteBuffer;
|
jtulach@44
|
11 |
import java.util.ServiceLoader;
|
jtulach@44
|
12 |
import org.apidesign.spi.security.Digestor;
|
jtulach@44
|
13 |
|
jtulach@44
|
14 |
/**
|
jtulach@44
|
15 |
*
|
jtulach@44
|
16 |
* @author Jaroslav Tulach
|
jtulach@44
|
17 |
*/
|
jtulach@44
|
18 |
public class DigestorProvider implements DigestProvider {
|
jtulach@44
|
19 |
|
jtulach@44
|
20 |
public DigestImplementation create(String algorithm) {
|
jtulach@44
|
21 |
for (Digestor<?> d : ServiceLoader.load(Digestor.class)) {
|
jtulach@44
|
22 |
Handler<?> h = create(d, algorithm);
|
jtulach@44
|
23 |
if (h != null) {
|
jtulach@44
|
24 |
return h;
|
jtulach@44
|
25 |
}
|
jtulach@44
|
26 |
}
|
jtulach@44
|
27 |
return null;
|
jtulach@44
|
28 |
}
|
jtulach@44
|
29 |
|
jtulach@44
|
30 |
private static <Data> Handler<Data> create(Digestor<Data> dig, String algorithm) {
|
jtulach@44
|
31 |
Data d = DigestorAccessor.getDefault().create(dig, algorithm);
|
jtulach@44
|
32 |
if (d == null) {
|
jtulach@44
|
33 |
return null;
|
jtulach@44
|
34 |
} else {
|
jtulach@44
|
35 |
return new Handler<Data>(algorithm, dig, d);
|
jtulach@44
|
36 |
}
|
jtulach@44
|
37 |
}
|
jtulach@44
|
38 |
|
jtulach@44
|
39 |
private static final class Handler<Data> extends DigestImplementation {
|
jtulach@44
|
40 |
private final Digestor dig;
|
jtulach@44
|
41 |
private final Data data;
|
jtulach@44
|
42 |
|
jtulach@44
|
43 |
public Handler(String algorithm, Digestor dig, Data data) {
|
jtulach@44
|
44 |
super(algorithm);
|
jtulach@44
|
45 |
this.dig = dig;
|
jtulach@44
|
46 |
this.data = data;
|
jtulach@44
|
47 |
}
|
jtulach@44
|
48 |
|
jtulach@44
|
49 |
|
jtulach@44
|
50 |
@Override
|
jtulach@44
|
51 |
public void update(ByteBuffer bb) {
|
jtulach@44
|
52 |
DigestorAccessor.getDefault().update(dig, data, bb);
|
jtulach@44
|
53 |
}
|
jtulach@44
|
54 |
|
jtulach@44
|
55 |
@Override
|
jtulach@44
|
56 |
public byte[] digest() {
|
jtulach@44
|
57 |
return DigestorAccessor.getDefault().digest(dig, data);
|
jtulach@44
|
58 |
}
|
jtulach@44
|
59 |
|
jtulach@44
|
60 |
}
|
jtulach@44
|
61 |
}
|