Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NullCipher.java « crypto « javax « java « main « src « jce - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb1ac756081535ef93ce7251b3140b78401a6d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package javax.crypto;

import java.security.Key;
import java.security.SecureRandom;
import java.security.InvalidKeyException;
import java.security.AlgorithmParameters;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;

/**
 * The NullCipher class is a class that provides an
 * "identity cipher" -- one that does not tranform the plaintext.  As
 * a consequence, the ciphertext is identical to the plaintext.  All
 * initialization methods do nothing, while the blocksize is set to 1
 * byte.
 *
 * @since JCE1.2
 */
public class NullCipher
    extends Cipher
{
    static private class NullCipherSpi
        extends CipherSpi
    {
        /**
         * Sets the mode of this cipher - no op.
         */
        protected void engineSetMode(
            String  mode)
        throws NoSuchAlgorithmException
        {
        }
    
        /**
         * Sets the padding mechanism of this cipher - no op.
         */
        protected void engineSetPadding(
            String  padding)
        throws NoSuchPaddingException
        {
        }
    
        /**
         * Returns the block size (in bytes) - 1
         */
        protected int engineGetBlockSize()
        {
            return 1;
        }

        /**
         * Returns the length in bytes that an output buffer would
         * need to be in order to hold the result of the next <code>update</code>
         * or <code>doFinal</code> operation, given the input length
         * <code>inputLen</code> (in bytes).
         *
         * @param inputLen the input length (in bytes)
         * @return the required output buffer size (in bytes)
         */
        protected int engineGetOutputSize(
            int  inputLen)
        {
            return inputLen;
        }

        /**
         * Returns the initialization vector (IV) in a new buffer. 
         *
         * @return null
         */
        protected byte[] engineGetIV()
        {
            return null;
        }
    
        /**
         * Returns the parameters used with this cipher - null
         */
        protected AlgorithmParameters engineGetParameters()
        {
            return null;
        }

        /**
         * Initializes this cipher with a key and a source
         * of randomness - no op.
         */
        protected void engineInit(
            int             opmode,
            Key             key,
            SecureRandom    random)
        throws InvalidKeyException
        {
        }
    
        /**
         * Initializes this cipher with a key, a set of
         * algorithm parameters, and a source of randomness - no op.
         */
        protected void engineInit(
            int                     opmode,
            Key                     key,
            AlgorithmParameterSpec  params,
            SecureRandom            random)
        throws InvalidKeyException, InvalidAlgorithmParameterException
        {
        }
    
        /**
         * Initializes this cipher with a key, a set of
         * algorithm parameters, and a source of randomness - no op.
         */
        protected void engineInit(
            int                 opmode,
            Key                 key,
            AlgorithmParameters params,
            SecureRandom        random)
        throws InvalidKeyException, InvalidAlgorithmParameterException
        {
        }
    
        /**
         * Continues a multiple-part encryption or decryption operation
         * (depending on how this cipher was initialized), processing another data
         * part - in this case just return a copy of the input.
         */
        protected byte[] engineUpdate(
            byte[]      input,
            int         inputOffset,
            int         inputLen)
        {
            if (input == null)
            {
                return null;
            }

            byte[] tmp = new byte[inputLen];

            System.arraycopy(input, inputOffset, tmp, 0, inputLen);

            return tmp;
        }
    
        /**
         * Continues a multiple-part encryption or decryption operation
         * (depending on how this cipher was initialized), processing another data
         * part - in this case just copy the input to the output.
         */
        protected int engineUpdate(
            byte[]      input,
            int         inputOffset,
            int         inputLen,
            byte[]      output,
            int         outputOffset)
        throws ShortBufferException
        {
            if (input == null)
            {
                return 0;
            }

            if ((output.length - outputOffset) < inputLen)
            {
                throw new ShortBufferException("output buffer to short for NullCipher");
            }

            System.arraycopy(input, inputOffset, output, outputOffset, inputLen);

            return inputLen;
        }
    
        /**
         * Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.
         * The data is encrypted or decrypted, depending on how this cipher was initialized
         * - in this case just return a copy of the input.
         */
        protected byte[] engineDoFinal(
            byte[]      input,
            int         inputOffset,
            int         inputLen)
        throws IllegalBlockSizeException, BadPaddingException
        {
            if (input == null)
            {
                return new byte[0];
            }

            byte[] tmp = new byte[inputLen];

            System.arraycopy(input, inputOffset, tmp, 0, inputLen);

            return tmp;
        }

        /**
         * Encrypts or decrypts data in a single-part operation,
         * or finishes a multiple-part operation.
         * The data is encrypted or decrypted, depending on how this cipher was
         * initialized.
         */
        protected int engineDoFinal(
            byte[]      input,
            int         inputOffset,
            int         inputLen,
            byte[]      output,
            int         outputOffset)
        throws ShortBufferException, IllegalBlockSizeException, BadPaddingException
        {
            if (input == null)
            {
                return 0;
            }

            if ((output.length - outputOffset) < inputLen)
            {
                throw new ShortBufferException("output buffer too short for NullCipher");
            }

            System.arraycopy(input, inputOffset, output, outputOffset, inputLen);

            return inputLen;
        }
    
        /**
         * Returns the key size of the given key object - 0
         */
        protected int engineGetKeySize(
            Key     key)
        throws InvalidKeyException
        {
            return 0;
        }
    }

    public NullCipher()
    {
        super(new NullCipherSpi(), null, "NULL");
    }
}