1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.internal.digest.factory; |
21 |
|
|
22 |
|
import java.io.OutputStream; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Singleton; |
26 |
|
|
27 |
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
28 |
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
29 |
|
import org.bouncycastle.operator.DigestCalculator; |
30 |
|
import org.bouncycastle.operator.DigestCalculatorProvider; |
31 |
|
import org.bouncycastle.operator.OperatorCreationException; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.component.manager.ComponentManager; |
35 |
|
import org.xwiki.crypto.Digest; |
36 |
|
import org.xwiki.crypto.DigestFactory; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
48 |
|
@Component |
49 |
|
@Singleton |
|
|
| 42.3% |
Uncovered Elements: 15 (26) |
Complexity: 11 |
Complexity Density: 0.73 |
|
50 |
|
public class DefaultDigestFactory extends AbstractBcDigestFactory implements DigestCalculatorProvider |
51 |
|
{ |
52 |
|
private static final RuntimeException UNSUPPORTED = |
53 |
|
new UnsupportedOperationException("Unexpected internal function call."); |
54 |
|
|
55 |
|
@Inject |
56 |
|
private ComponentManager manager; |
57 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
58 |
0 |
@Override... |
59 |
|
public org.bouncycastle.crypto.Digest getDigestInstance() |
60 |
|
{ |
61 |
0 |
throw UNSUPPORTED; |
62 |
|
} |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0 |
@Override... |
65 |
|
public AlgorithmIdentifier getAlgorithmIdentifier() |
66 |
|
{ |
67 |
0 |
throw UNSUPPORTED; |
68 |
|
} |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
70 |
0 |
@Override... |
71 |
|
public Digest getInstance() |
72 |
|
{ |
73 |
0 |
throw new UnsupportedOperationException("Sorry, cannot get an instance without a determined algorithm."); |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
76 |
6 |
@Override... |
77 |
|
public Digest getInstance(byte[] encoded) |
78 |
|
{ |
79 |
6 |
AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(encoded); |
80 |
|
|
81 |
6 |
return getFactory(algId.getAlgorithm()).getInstance(); |
82 |
|
} |
83 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
84 |
22 |
private DigestFactory getFactory(ASN1ObjectIdentifier algId)... |
85 |
|
{ |
86 |
22 |
try { |
87 |
22 |
return this.manager.getInstance(DigestFactory.class, algId.getId()); |
88 |
|
} catch (ComponentLookupException e) { |
89 |
0 |
throw new UnsupportedOperationException("Digest algorithm not found.", e); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
@inheritDoc |
95 |
|
|
96 |
|
@since |
97 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
98 |
16 |
@Override... |
99 |
|
public DigestCalculator get(final AlgorithmIdentifier algorithmIdentifier) throws OperatorCreationException |
100 |
|
{ |
101 |
16 |
final Digest digest = getFactory(algorithmIdentifier.getAlgorithm()).getInstance(); |
102 |
|
|
103 |
16 |
if (digest instanceof DigestCalculator) { |
104 |
16 |
return (DigestCalculator) digest; |
105 |
|
} else { |
106 |
0 |
return new DigestCalculator() |
107 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0 |
@Override... |
109 |
|
public AlgorithmIdentifier getAlgorithmIdentifier() |
110 |
|
{ |
111 |
0 |
return algorithmIdentifier; |
112 |
|
} |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
0 |
@Override... |
115 |
|
public OutputStream getOutputStream() |
116 |
|
{ |
117 |
0 |
return digest.getOutputStream(); |
118 |
|
} |
119 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
0 |
@Override... |
121 |
|
public byte[] getDigest() |
122 |
|
{ |
123 |
0 |
return digest.digest(); |
124 |
|
} |
125 |
|
}; |
126 |
|
} |
127 |
|
} |
128 |
|
} |