01: /*************************************************************************
02: * *
03: * EJBCA: The OpenSource Certificate Authority *
04: * *
05: * This software is free software; you can redistribute it and/or *
06: * modify it under the terms of the GNU Lesser General Public *
07: * License as published by the Free Software Foundation; either *
08: * version 2.1 of the License, or any later version. *
09: * *
10: * See terms of license at gnu.org. *
11: * *
12: *************************************************************************/package org.ejbca.util;
13:
14: import java.io.ByteArrayOutputStream;
15:
16: /**
17: * This class implements a BASE64 Character encoder/decoder as specified in RFC1521.
18: * It extends the bouncycastle implementation and adds the functionality to split lines
19: * with a '\n' after every 64 bytes.
20: *
21: * @version $Id: Base64.java,v 1.1 2006/01/17 20:32:19 anatom Exp $
22: */
23: public class Base64 {
24:
25: /**
26: * encode the input data producong a base 64 encoded byte array with the output lines be split by '\n' (64 byte rows).
27: *
28: * @param data data to be encoded
29: * @return a byte array containing the base 64 encoded data.
30: */
31: public static byte[] encode(byte[] data) {
32: return encode(data, true);
33: }
34:
35: /**
36: * encode the input data producong a base 64 encoded byte array.
37: *
38: * @param data the data to be encoded
39: * @param splitlines whether the output lines will be split by '\n' (64 byte rows) or not.
40: * @return a byte array containing the base 64 encoded data.
41: */
42: public static byte[] encode(byte[] data, boolean splitlines) {
43: byte[] bytes = org.bouncycastle.util.encoders.Base64
44: .encode(data);
45: if (!splitlines) {
46: return bytes;
47: }
48:
49: // make sure we get limited lines...
50: ByteArrayOutputStream os = new ByteArrayOutputStream();
51: for (int i = 0; i < bytes.length; i += 64) {
52: if ((i + 64) < bytes.length) {
53: os.write(bytes, i, 64);
54: os.write('\n');
55: } else {
56: os.write(bytes, i, bytes.length - i);
57: }
58: }
59: return os.toByteArray();
60: }
61:
62: public static byte[] decode(byte[] bytes) {
63: return org.bouncycastle.util.encoders.Base64.decode(bytes);
64: }
65:
66: }
|