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

File BcBufferedAsymmetricCipher.java

 

Coverage histogram

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

Code metrics

6
24
12
1
148
97
16
0.67
2
12
1.33

Classes

Class Line # Actions
BcBufferedAsymmetricCipher 40 24 0% 16 17
0.595238159.5%
 

Contributing tests

This file is covered by 2 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.cipher.internal.asymmetric;
21   
22    import java.io.FilterInputStream;
23    import java.io.FilterOutputStream;
24    import java.io.InputStream;
25    import java.io.OutputStream;
26    import java.security.GeneralSecurityException;
27   
28    import org.bouncycastle.crypto.AsymmetricBlockCipher;
29    import org.bouncycastle.crypto.BufferedAsymmetricBlockCipher;
30    import org.bouncycastle.crypto.CipherParameters;
31    import org.bouncycastle.crypto.InvalidCipherTextException;
32    import org.xwiki.crypto.cipher.AsymmetricCipher;
33   
34    /**
35    * Base class for Bouncy Castle asymmetric ciphers.
36    *
37    * @version $Id: 907c7f3ed206955fda8f81881289cb3370ba13d3 $
38    * @since 5.4M1
39    */
 
40    public class BcBufferedAsymmetricCipher implements AsymmetricCipher
41    {
42    private static final RuntimeException NO_STREAMING_ERROR =
43    new UnsupportedOperationException("Asymmetric cipher does not support being streamed.");
44   
45    private final BufferedAsymmetricBlockCipher cipher;
46   
47    private final boolean forEncryption;
48   
49    private final String algorithmName;
50   
51    /**
52    * Create a new Bouncy Castle based asymmetric cipher.
53    *
54    * @param cipher the native cipher to encapsulate.
55    * @param forEncryption true is the cipher should be initialized for encryption.
56    * @param parameters the cipher parameters in bouncy castle format.
57    * @param algorithmName the name to report for the algorithm of this cipher.
58    */
 
59  8 toggle public BcBufferedAsymmetricCipher(AsymmetricBlockCipher cipher, boolean forEncryption, CipherParameters parameters,
60    String algorithmName)
61    {
62  8 this.cipher = new BufferedAsymmetricBlockCipher(cipher);
63  8 this.forEncryption = forEncryption;
64  8 this.algorithmName = algorithmName;
65  8 this.cipher.init(forEncryption, parameters);
66    }
67   
 
68  0 toggle @Override
69    public String getAlgorithmName()
70    {
71  0 return this.algorithmName;
72    }
73   
 
74  0 toggle @Override
75    public int getInputBlockSize()
76    {
77  0 return this.cipher.getInputBlockSize();
78    }
79   
 
80  0 toggle @Override
81    public int getOutputBlockSize()
82    {
83  0 return this.cipher.getOutputBlockSize();
84    }
85   
 
86  0 toggle @Override
87    public boolean isForEncryption()
88    {
89  0 return this.forEncryption;
90    }
91   
 
92  0 toggle @Override
93    public FilterInputStream getInputStream(InputStream is)
94    {
95  0 throw NO_STREAMING_ERROR;
96    }
97   
 
98  0 toggle @Override
99    public FilterOutputStream getOutputStream(OutputStream os)
100    {
101  0 throw NO_STREAMING_ERROR;
102    }
103   
 
104  1 toggle @Override
105    public byte[] update(byte[] input)
106    {
107  1 if (input != null) {
108  1 return update(input, 0, input.length);
109    }
110  0 return null;
111    }
112   
 
113  9 toggle @Override
114    public byte[] update(byte[] input, int inputOffset, int inputLen)
115    {
116  9 this.cipher.processBytes(input, inputOffset, inputLen);
117  9 return null;
118    }
119   
 
120  3 toggle @Override
121    public byte[] doFinal() throws GeneralSecurityException
122    {
123  3 return doFinal(null, 0, 0);
124    }
125   
 
126  4 toggle @Override
127    public byte[] doFinal(byte[] input) throws GeneralSecurityException
128    {
129  4 if (input != null) {
130  4 return doFinal(input, 0, input.length);
131    } else {
132  0 return doFinal(null, 0, 0);
133    }
134    }
135   
 
136  8 toggle @Override
137    public byte[] doFinal(byte[] input, int inputOffset, int inputLen) throws GeneralSecurityException
138    {
139  8 if (input != null) {
140  5 this.cipher.processBytes(input, inputOffset, inputLen);
141    }
142  8 try {
143  8 return this.cipher.doFinal();
144    } catch (InvalidCipherTextException e) {
145  0 throw new GeneralSecurityException("Cipher failed to process data.", e);
146    }
147    }
148    }