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

File AbstractBcDigestFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

6
15
5
1
85
50
9
0.6
3
5
1.8

Classes

Class Line # Actions
AbstractBcDigestFactory 36 15 0% 9 12
0.5384615753.8%
 

Contributing tests

This file is covered by 28 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 javax.inject.Named;
23   
24    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
25    import org.xwiki.component.annotation.Component;
26    import org.xwiki.crypto.Digest;
27    import org.xwiki.crypto.internal.digest.BouncyCastleDigest;
28    import org.xwiki.crypto.params.DigestParameters;
29   
30    /**
31    * Abstract base class for factory creating Bouncy Castle based digest.
32    *
33    * @version $Id: 009f5761864514f598c680bc0e2177191b370c3a $
34    * @since 5.4M1
35    */
 
36    public abstract class AbstractBcDigestFactory implements BcDigestFactory
37    {
 
38  6 toggle @Override
39    public String getDigestAlgorithmName()
40    {
41  6 String hint = null;
42  6 Named named = this.getClass().getAnnotation(Named.class);
43  6 if (named != null) {
44  0 hint = named.value();
45    } else {
46  6 Component component = this.getClass().getAnnotation(Component.class);
47  6 if (component != null && component.hints().length > 0) {
48  6 hint = component.hints()[0];
49    }
50    }
51   
52  6 return hint;
53    }
54   
 
55  18 toggle @Override
56    public int getDigestSize()
57    {
58  18 return getDigestInstance().getDigestSize();
59    }
60   
 
61  28 toggle @Override
62    public Digest getInstance()
63    {
64  28 return new BouncyCastleDigest(getDigestInstance(), getAlgorithmIdentifier(), null);
65    }
66   
 
67  0 toggle @Override
68    public Digest getInstance(DigestParameters parameters)
69    {
70  0 return getInstance();
71    }
72   
 
73  0 toggle @Override
74    public Digest getInstance(byte[] encoded)
75    {
76  0 AlgorithmIdentifier algId = AlgorithmIdentifier.getInstance(encoded);
77   
78  0 if (!algId.getAlgorithm().equals(getAlgorithmIdentifier().getAlgorithm())) {
79  0 throw new IllegalArgumentException("Invalid algorithm identifier in encoded data for this digest factory: "
80    + algId.getAlgorithm().getId());
81    }
82   
83  0 return getInstance();
84    }
85    }