1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.digest.factory

File DefaultDigestFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

2
15
9
1
128
79
11
0.73
1.67
9
1.22

Classes

Class Line # Actions
DefaultDigestFactory 50 15 0% 11 15
0.4230769342.3%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Default digest factory to create digest from encoded ASN.1 identifiers.
40    *
41    * This factory could only create digest from previously serialized digest, that use the ASN.1 encoding standard.
42    * It delegate the creation operation to the appropriate factory. The factory is requested to the component manager
43    * using the algorithm OID as hint.
44    *
45    * @version $Id: ee77e9480d89e471bd6e7330c4133ce0f4b563d5 $
46    * @since 5.4M1
47    */
48    @Component
49    @Singleton
 
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   
 
58  0 toggle @Override
59    public org.bouncycastle.crypto.Digest getDigestInstance()
60    {
61  0 throw UNSUPPORTED;
62    }
63   
 
64  0 toggle @Override
65    public AlgorithmIdentifier getAlgorithmIdentifier()
66    {
67  0 throw UNSUPPORTED;
68    }
69   
 
70  0 toggle @Override
71    public Digest getInstance()
72    {
73  0 throw new UnsupportedOperationException("Sorry, cannot get an instance without a determined algorithm.");
74    }
75   
 
76  6 toggle @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   
 
84  22 toggle 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 6.0M1
97    */
 
98  16 toggle @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    {
 
108  0 toggle @Override
109    public AlgorithmIdentifier getAlgorithmIdentifier()
110    {
111  0 return algorithmIdentifier;
112    }
113   
 
114  0 toggle @Override
115    public OutputStream getOutputStream()
116    {
117  0 return digest.getOutputStream();
118    }
119   
 
120  0 toggle @Override
121    public byte[] getDigest()
122    {
123  0 return digest.digest();
124    }
125    };
126    }
127    }
128    }