1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.example.ocsp;
18
19 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
20 import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
21 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
22 import org.bouncycastle.cert.ocsp.OCSPReqBuilder;
23 import org.bouncycastle.crypto.Digest;
24 import org.bouncycastle.crypto.digests.SHA1Digest;
25 import org.bouncycastle.crypto.digests.SHA256Digest;
26 import org.bouncycastle.crypto.io.DigestOutputStream;
27 import org.bouncycastle.operator.DigestCalculator;
28
29 import java.io.OutputStream;
30
31
32
33
34
35
36
37 public final class Digester implements DigestCalculator {
38
39 public static DigestCalculator sha1() {
40 Digest digest = new SHA1Digest();
41 AlgorithmIdentifier algId = new AlgorithmIdentifier(
42 OIWObjectIdentifiers.idSHA1);
43
44 return new Digester(digest, algId);
45 }
46
47 public static DigestCalculator sha256() {
48 Digest digest = new SHA256Digest();
49
50
51 ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier(
52 "2.16.840.1.101.3.4.2.1").intern();
53 AlgorithmIdentifier algId = new AlgorithmIdentifier(oid);
54
55 return new Digester(digest, algId);
56 }
57
58 private final DigestOutputStream dos;
59
60 private final AlgorithmIdentifier algId;
61
62 private Digester(Digest digest, AlgorithmIdentifier algId) {
63 this.dos = new DigestOutputStream(digest);
64 this.algId = algId;
65 }
66
67 @Override
68 public AlgorithmIdentifier getAlgorithmIdentifier() {
69 return algId;
70 }
71
72 @Override
73 public OutputStream getOutputStream() {
74 return dos;
75 }
76
77 @Override
78 public byte[] getDigest() {
79 return dos.getDigest();
80 }
81 }