KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > crypto > io > raes > RaesOutputStream


1 /*
2  * Copyright 2005-2006 Schlichtherle IT Services
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package de.schlichtherle.crypto.io.raes;
18
19 import de.schlichtherle.crypto.io.CipherOutputStream;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import org.bouncycastle.crypto.BufferedBlockCipher;
25 import org.bouncycastle.crypto.Mac;
26
27 /**
28  * An {@link OutputStream} to produce a file with data ecnrypted according
29  * to the Random Access Encryption Specification (RAES).
30  *
31  * @see RaesReadOnlyFile
32  *
33  * @author Christian Schlichtherle
34  * @version @version@
35  * @since TrueZIP 6.0
36  */

37 public abstract class RaesOutputStream
38         extends CipherOutputStream
39         implements RAES {
40
41     /**
42      * Update the given KLAC with the given file <tt>length</tt> in
43      * little endian order and finalize it, writing the result to <tt>buf</tt>.
44      * The KLAC must already have been initialized and updated with the
45      * password bytes as retrieved according to PKCS #12.
46      * The result is stored in <tt>buf</tt>, which must match the given
47      * KLAC's output size.
48      */

49     static void klac(final Mac klac, long length, final byte[] buf) {
50         for (int i = 0; i < 8; i++) {
51             klac.update((byte) length);
52             length >>= 8;
53         }
54         final int bufLen = klac.doFinal(buf, 0);
55         assert bufLen == buf.length;
56     }
57
58     /**
59      * Creates a new instance of <code>RaesOutputStream</code>.
60      *
61      * @param out The underlying output stream to use for the encrypted data.
62      * @param parameters The {@link RaesParameters} used to determine and
63      * configure the type of RAES file created.
64      * If the run time class of this parameter matches multiple
65      * parameter interfaces, it is at the discretion of this
66      * implementation which one is picked and hence which type of
67      * RAES file is created.
68      * If you need more control over this, pass in an instance which's
69      * run time class just implements the
70      * {@link RaesParametersAgent} interface.
71      * Instances of this interface are recursively used to find RAES
72      * parameters which match a known RAES type.
73      *
74      * @throws NullPointerException If {@link #out} is <tt>null</tt>
75      * or <tt>parameters</tt> is <tt>null</tt>.
76      * @throws IllegalArgumentException If an illegal keyStrength is provided
77      * in the parameters.
78      * @throws RaesParametersException If no suitable RAES parameters have been
79      * provided or something is wrong with the parameters.
80      * @throws IOException On any other I/O related issue.
81      */

82     public static RaesOutputStream getInstance(
83             final OutputStream out,
84             RaesParameters parameters)
85     throws NullPointerException JavaDoc,
86             IllegalArgumentException JavaDoc,
87             RaesParametersException,
88             IOException JavaDoc {
89         if (out == null)
90             throw new NullPointerException JavaDoc("out");
91
92         // Order is important here to support multiple interface implementations!
93
if (parameters == null) {
94             throw new RaesParametersException();
95         } else if (parameters instanceof Type0RaesParameters) {
96             return new Type0RaesOutputStream(out,
97                     (Type0RaesParameters) parameters);
98         } else if (parameters instanceof RaesParametersAgent) {
99             parameters = ((RaesParametersAgent) parameters).getParameters(
100                     RaesParameters.class);
101             return getInstance(out, parameters);
102         } else {
103             throw new RaesParametersException();
104         }
105     }
106
107     RaesOutputStream(OutputStream out, BufferedBlockCipher cipher) {
108         super(out, cipher);
109     }
110
111     /**
112      * Returns the key size in bits which is actually used to encrypt or
113      * decrypt the data for this output stream.
114      */

115     public abstract int getKeySizeBits();
116 }
117
Popular Tags