Debian Codegraph @

ssl-proxies/src/main/java/org/globus/gsi/util/PEMUtil.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.util;
16
17 import java.io.IOException;
18 import java.io.OutputStream;
19
20 // JGLOBUS-95 : how much of this can be borrwed directly form BC?
21
22 /**
23 * Fill Me
24 */
25 public final class PEMUtil {
26
27 public static final String LINE_SEP;
28 static final byte[] LINE_SEP_BYTES;
29
30 static final int LINE_LENGTH = 64;
31
32 private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
33 'A', 'B', 'C', 'D', 'E', 'F'};
34
35 static {
36 LINE_SEP = System.getProperty("line.separator");
37 LINE_SEP_BYTES = LINE_SEP.getBytes();
38 }
39
40 private PEMUtil() {
41 //This should not be instantiated
42 }
43
44
45 public static void writeBase64(
46 OutputStream out,
47 String header,
48 byte[] base64Data,
49 String footer)
50 throws IOException {
51
52 int length = LINE_LENGTH;
53 int offset = 0;
54
55 if (header != null) {
56 out.write(header.getBytes());
57 out.write(LINE_SEP_BYTES);
58 }
59
60 int size = base64Data.length;
61 while (offset < size) {
62 if (LINE_LENGTH > (size - offset)) {
63 length = size - offset;
64 }
65 out.write(base64Data, offset, length);
66 out.write(LINE_SEP_BYTES);
67 offset = offset + LINE_LENGTH;
68 }
69
70 if (footer != null) {
71 out.write(footer.getBytes());
72 out.write(LINE_SEP_BYTES);
73 }
74 }
75
76 /**
77 * Return a hexadecimal representation of a byte array
78 *
79 * @param b a byte array
80 * @return String containing the hexadecimal representation
81 */
82 public static String toHex(byte[] b) {
83 char[] buf = new char[b.length * 2];
84 int j = 0;
85 int k;
86
87 for (byte aB : b) {
88 k = aB;
89 buf[j++] = HEX[(k >>> 4) & 0x0F];
90 buf[j++] = HEX[k & 0x0F];
91 }
92 return new String(buf);
93 }
94
95
96 }