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

File BcPaddedSymmetricCipher.java

 

Coverage histogram

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

Code metrics

4
7
3
1
80
32
6
0.86
2.33
3
2

Classes

Class Line # Actions
BcPaddedSymmetricCipher 32 7 0% 6 7
0.550%
 

Contributing tests

This file is covered by 22 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.symmetric;
21   
22    import org.bouncycastle.crypto.BlockCipher;
23    import org.bouncycastle.crypto.CipherParameters;
24    import org.bouncycastle.crypto.paddings.BlockCipherPadding;
25    import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
26   
27    /**
28    * Base class for Bouncy Castle padded symmetric ciphers.
29    *
30    * @version $Id: d521dd634cf3472cd99fceefdc426fa0dcb20400 $
31    */
 
32    public class BcPaddedSymmetricCipher extends BcSymmetricCipher
33    {
34    private static final String SEPARATOR = "/";
35   
36    private static final String PADDING = "Padding";
37   
38    private static final String PKCS5_PADDING = "PKCS5" + PADDING;
39   
40    private static final String PKCS7_PADDING = "PKCS7" + PADDING;
41   
42    private String paddingName;
43   
44    /**
45    * Generic Bouncy Castle based block cipher with padding.
46    *
47    * @param cipher the block cipher to encapsulate.
48    * @param forEncryption true if the block cipher is setup for encryption.
49    * @param parameters parameters to initialize the cipher.
50    */
 
51  43 toggle public BcPaddedSymmetricCipher(BlockCipher cipher, boolean forEncryption, CipherParameters parameters)
52    {
53  43 super(new PaddedBufferedBlockCipher(cipher), forEncryption, parameters);
54  43 this.paddingName = (getOutputBlockSize() <= 8) ? PKCS5_PADDING : PKCS7_PADDING;
55    }
56   
57    /**
58    * Generic Bouncy Castle based block cipher with padding.
59    *
60    * @param cipher the block cipher to encapsulate.
61    * @param forEncryption true if the block cipher is setup for encryption.
62    * @param padding the padding to apply.
63    * @param parameters parameters to initialize the cipher.
64    */
 
65  0 toggle public BcPaddedSymmetricCipher(BlockCipher cipher, boolean forEncryption, CipherParameters parameters,
66    BlockCipherPadding padding)
67    {
68  0 super(new PaddedBufferedBlockCipher(cipher, padding), forEncryption, parameters);
69  0 this.paddingName = padding.getPaddingName() + PADDING;
70  0 if (PKCS7_PADDING.equals(this.paddingName) && getOutputBlockSize() <= 8) {
71  0 this.paddingName = PKCS5_PADDING;
72    }
73    }
74   
 
75  14 toggle @Override
76    public String getAlgorithmName()
77    {
78  14 return this.cipher.getUnderlyingCipher().getAlgorithmName() + SEPARATOR + this.paddingName;
79    }
80    }