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.extension;
|
jtulach@44
|
7 |
|
jtulach@44
|
8 |
import org.apidesign.api.security.Digest;
|
jtulach@44
|
9 |
import java.nio.ByteBuffer;
|
jtulach@44
|
10 |
import java.security.MessageDigest;
|
jtulach@44
|
11 |
import java.security.NoSuchAlgorithmException;
|
jtulach@44
|
12 |
import java.security.Provider;
|
jtulach@44
|
13 |
import java.security.Security;
|
jtulach@44
|
14 |
import java.util.Collections;
|
jtulach@44
|
15 |
import java.util.List;
|
jtulach@44
|
16 |
import java.util.Map;
|
jtulach@44
|
17 |
|
jtulach@44
|
18 |
/**
|
jtulach@44
|
19 |
*
|
jtulach@44
|
20 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@44
|
21 |
*/
|
jtulach@44
|
22 |
public class BridgeToOld extends Provider {
|
jtulach@44
|
23 |
|
jtulach@44
|
24 |
public BridgeToOld() {
|
jtulach@44
|
25 |
super("spi.Digestor", 1.0, "");
|
jtulach@44
|
26 |
Security.addProvider(this);
|
jtulach@44
|
27 |
}
|
jtulach@44
|
28 |
|
jtulach@44
|
29 |
@Override
|
jtulach@44
|
30 |
public synchronized Service getService(String type, String algorithm) {
|
jtulach@44
|
31 |
if ("MessageDigest".equals(type)) {
|
jtulach@44
|
32 |
Digest dig = Digest.getInstance(algorithm);
|
jtulach@44
|
33 |
if (dig != null) {
|
jtulach@44
|
34 |
return new ServiceImpl(dig, this, type, algorithm, "", Collections.<String>emptyList(), Collections.<String,String>emptyMap());
|
jtulach@44
|
35 |
}
|
jtulach@44
|
36 |
}
|
jtulach@44
|
37 |
return null;
|
jtulach@44
|
38 |
}
|
jtulach@44
|
39 |
|
jtulach@44
|
40 |
private static class ServiceImpl<Data> extends Service {
|
jtulach@44
|
41 |
Digest dig;
|
jtulach@44
|
42 |
|
jtulach@44
|
43 |
public ServiceImpl(Digest dig, Provider provider, String type, String algorithm, String className, List<String> aliases, Map<String, String> attributes) {
|
jtulach@44
|
44 |
super(provider, type, algorithm, className, aliases, attributes);
|
jtulach@44
|
45 |
this.dig = dig;
|
jtulach@44
|
46 |
}
|
jtulach@44
|
47 |
|
jtulach@44
|
48 |
@Override
|
jtulach@44
|
49 |
public Object newInstance(Object constructorParameter) throws NoSuchAlgorithmException {
|
jtulach@44
|
50 |
return new MessageDigest(getAlgorithm()) {
|
jtulach@44
|
51 |
private byte[] res;
|
jtulach@44
|
52 |
|
jtulach@44
|
53 |
@Override
|
jtulach@44
|
54 |
protected void engineUpdate(byte input) {
|
jtulach@44
|
55 |
ByteBuffer bb = ByteBuffer.wrap(new byte[] { input });
|
jtulach@44
|
56 |
res = dig.digest(bb);
|
jtulach@44
|
57 |
}
|
jtulach@44
|
58 |
|
jtulach@44
|
59 |
@Override
|
jtulach@44
|
60 |
protected void engineUpdate(byte[] input, int offset, int len) {
|
jtulach@44
|
61 |
ByteBuffer bb = ByteBuffer.wrap(input);
|
jtulach@44
|
62 |
bb.position(offset);
|
jtulach@44
|
63 |
bb.limit(offset + len);
|
jtulach@44
|
64 |
res = dig.digest(bb);
|
jtulach@44
|
65 |
}
|
jtulach@44
|
66 |
|
jtulach@44
|
67 |
@Override
|
jtulach@44
|
68 |
protected byte[] engineDigest() {
|
jtulach@44
|
69 |
return res;
|
jtulach@44
|
70 |
}
|
jtulach@44
|
71 |
|
jtulach@44
|
72 |
@Override
|
jtulach@44
|
73 |
protected void engineReset() {
|
jtulach@44
|
74 |
dig = Digest.getInstance(getAlgorithm());
|
jtulach@44
|
75 |
}
|
jtulach@44
|
76 |
};
|
jtulach@44
|
77 |
}
|
jtulach@44
|
78 |
|
jtulach@44
|
79 |
|
jtulach@44
|
80 |
}
|
jtulach@44
|
81 |
|
jtulach@44
|
82 |
}
|