Debian Codegraph @

ssl-proxies/src/main/java/org/globus/util/PEMUtils.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.util;
16
17 import java.io.OutputStream;
18 import java.io.IOException;
19
20 public class PEMUtils {
21
22 static final int LINE_LENGTH = 64;
23
24 public final static String lineSep;
25 public final static byte[] lineSepBytes;
26
27 static {
28 lineSep = System.getProperty("line.separator");
29 lineSepBytes = lineSep.getBytes();
30 }
31
32 public static void writeBase64(OutputStream out,
33 String header,
34 byte[] base64Data,
35 String footer)
36 throws IOException {
37
38 int length = LINE_LENGTH;
39 int offset = 0;
40
41 if (header != null) {
42 out.write(header.getBytes());
43 out.write(lineSepBytes);
44 }
45
46 int size = base64Data.length;
47 while (offset < size) {
48 if (LINE_LENGTH > (size - offset)) {
49 length = size - offset;
50 }
51 out.write(base64Data, offset, length);
52 out.write(lineSepBytes);
53 offset = offset + LINE_LENGTH;
54 }
55
56 if (footer != null) {
57 out.write(footer.getBytes());
58 out.write(lineSepBytes);
59 }
60 }
61
62 /**
63 * Return a hexadecimal representation of a byte array
64 * @param b a byte array
65 * @return String containing the hexadecimal representation
66 */
67 public final static String toHex(byte [] b) {
68 char[] buf = new char[b.length * 2];
69 int i, j, k;
70
71 i = j = 0;
72 for (; i < b.length; i++) {
73 k = b[i];
74 buf[j++] = hex[(k >>> 4) & 0x0F];
75 buf[j++] = hex[ k & 0x0F];
76 }
77 return new String(buf);
78 }
79
80 private static final char[] hex = {'0','1','2','3','4','5','6','7','8','9',
81 'A','B','C','D','E','F'};
82
83 }