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

File BouncyCastleDigest.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
22
14
1
153
94
14
0.64
1.57
14
1

Classes

Class Line # Actions
BouncyCastleDigest 39 22 0% 14 2
0.944444494.4%
 

Contributing tests

This file is covered by 38 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;
21   
22    import java.io.FilterInputStream;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.OutputStream;
26   
27    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
28    import org.bouncycastle.crypto.io.DigestInputStream;
29    import org.bouncycastle.operator.DigestCalculator;
30    import org.xwiki.crypto.Digest;
31    import org.xwiki.crypto.params.DigestParameters;
32   
33    /**
34    * Base class for Bouncy Castle based Digest.
35    *
36    * @version $Id: a648a337572e5c010645ebf02bcefb4b9f05b6bf $
37    * @since 5.4M1
38    */
 
39    public class BouncyCastleDigest implements Digest, DigestCalculator
40    {
41    private final org.bouncycastle.crypto.Digest digest;
42   
43    private AlgorithmIdentifier algId;
44   
45    private DigestParameters parameters;
46   
47    /**
48    * Create a new digest based on the given Bouncy Castle digest engine.
49    *
50    * @param digest a bouncy castle digest.
51    * @param algId the algorithm identifier of this digest.
52    * @param parameters digest parameters if any.
53    */
 
54  28 toggle public BouncyCastleDigest(org.bouncycastle.crypto.Digest digest, AlgorithmIdentifier algId,
55    DigestParameters parameters)
56    {
57  28 this.digest = digest;
58  28 this.algId = algId;
59  28 this.parameters = parameters;
60    }
61   
 
62  6 toggle @Override
63    public String getAlgorithmName()
64    {
65  6 return this.digest.getAlgorithmName();
66    }
67   
 
68  6 toggle @Override
69    public int getDigestSize()
70    {
71  6 return this.digest.getDigestSize();
72    }
73   
 
74  0 toggle @Override
75    public DigestParameters getParameters()
76    {
77  0 return this.parameters;
78    }
79   
 
80  6 toggle @Override
81    public FilterInputStream getInputStream(InputStream is)
82    {
83  6 this.digest.reset();
84  6 return new DigestInputStream(is, this.digest);
85    }
86   
 
87  22 toggle @Override
88    public OutputStream getOutputStream()
89    {
90  22 this.digest.reset();
91  22 return new org.bouncycastle.crypto.io.DigestOutputStream(this.digest);
92    }
93   
 
94  18 toggle @Override
95    public void update(byte[] input)
96    {
97  18 update(input, 0, input.length);
98    }
99   
 
100  42 toggle @Override
101    public void update(byte[] input, int inputOffset, int inputLen)
102    {
103  42 this.digest.update(input, inputOffset, inputLen);
104    }
105   
 
106  58 toggle @Override
107    public byte[] digest()
108    {
109  58 byte[] dig = new byte[this.digest.getDigestSize()];
110  58 this.digest.doFinal(dig, 0);
111  58 return dig;
112    }
113   
 
114  12 toggle @Override
115    public byte[] digest(byte[] input)
116    {
117  12 update(input);
118  12 return digest();
119    }
120   
 
121  6 toggle @Override
122    public byte[] digest(byte[] input, int inputOffset, int inputLen)
123    {
124  6 update(input, inputOffset, inputLen);
125  6 return digest();
126    }
127   
128    /**
129    * {@inheritDoc}
130    *
131    * @since 6.0M1
132    */
 
133  16 toggle @Override
134    public byte[] getDigest()
135    {
136  16 return digest();
137    }
138   
139    /**
140    * @return the algorithm identifier of this digest.
141    */
 
142  20 toggle @Override
143    public AlgorithmIdentifier getAlgorithmIdentifier()
144    {
145  20 return this.algId;
146    }
147   
 
148  6 toggle @Override
149    public byte[] getEncoded() throws IOException
150    {
151  6 return getAlgorithmIdentifier().getEncoded();
152    }
153    }