[1174] | 1 | package br.gov.serpro.util; |
---|
| 2 | |
---|
| 3 | public class Base64Utils { |
---|
| 4 | |
---|
| 5 | private static byte[] mBase64EncMap, mBase64DecMap; |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Class initializer. Initializes the Base64 alphabet (specified in RFC-2045). |
---|
| 9 | */ |
---|
| 10 | static { |
---|
| 11 | byte[] base64Map = { |
---|
| 12 | (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', |
---|
| 13 | (byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', |
---|
| 14 | (byte)'M', (byte)'N', (byte)'O', (byte)'P', (byte)'Q', (byte)'R', |
---|
| 15 | (byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X', |
---|
| 16 | (byte)'Y', (byte)'Z', |
---|
| 17 | (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', |
---|
| 18 | (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', |
---|
| 19 | (byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r', |
---|
| 20 | (byte)'s', (byte)'t', (byte)'u', (byte)'v', (byte)'w', (byte)'x', |
---|
| 21 | (byte)'y', (byte)'z', |
---|
| 22 | (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', |
---|
| 23 | (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' }; |
---|
| 24 | mBase64EncMap = base64Map; |
---|
| 25 | mBase64DecMap = new byte[128]; |
---|
| 26 | for (int i=0; i<mBase64EncMap.length; i++) |
---|
| 27 | mBase64DecMap[mBase64EncMap[i]] = (byte) i; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * This class isn't meant to be instantiated. |
---|
| 32 | */ |
---|
| 33 | private Base64Utils() { |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * Encodes the given byte[] using the Base64-encoding, |
---|
| 38 | * as specified in RFC-2045 (Section 6.8). |
---|
| 39 | * |
---|
| 40 | * @param aData the data to be encoded |
---|
| 41 | * @return the Base64-encoded <var>aData</var> |
---|
| 42 | * @exception IllegalArgumentException if NULL or empty array is passed |
---|
| 43 | */ |
---|
| 44 | public static String base64Encode(byte[] aData) { |
---|
| 45 | if ((aData == null) || (aData.length == 0)) |
---|
| 46 | throw new IllegalArgumentException("Can not encode NULL or empty byte array."); |
---|
| 47 | |
---|
| 48 | byte encodedBuf[] = new byte[((aData.length+2)/3)*4]; |
---|
| 49 | |
---|
| 50 | // 3-byte to 4-byte conversion |
---|
| 51 | int srcIndex, destIndex; |
---|
| 52 | for (srcIndex=0, destIndex=0; srcIndex < aData.length-2; srcIndex += 3) { |
---|
| 53 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] >>> 2) & 077]; |
---|
| 54 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] >>> 4) & 017 | |
---|
| 55 | (aData[srcIndex] << 4) & 077]; |
---|
| 56 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+2] >>> 6) & 003 | |
---|
| 57 | (aData[srcIndex+1] << 2) & 077]; |
---|
| 58 | encodedBuf[destIndex++] = mBase64EncMap[aData[srcIndex+2] & 077]; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // Convert the last 1 or 2 bytes |
---|
| 62 | if (srcIndex < aData.length) { |
---|
| 63 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] >>> 2) & 077]; |
---|
| 64 | if (srcIndex < aData.length-1) { |
---|
| 65 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] >>> 4) & 017 | |
---|
| 66 | (aData[srcIndex] << 4) & 077]; |
---|
| 67 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex+1] << 2) & 077]; |
---|
| 68 | } |
---|
| 69 | else { |
---|
| 70 | encodedBuf[destIndex++] = mBase64EncMap[(aData[srcIndex] << 4) & 077]; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | // Add padding to the end of encoded data |
---|
| 75 | while (destIndex < encodedBuf.length) { |
---|
| 76 | encodedBuf[destIndex] = (byte) '='; |
---|
| 77 | destIndex++; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | String result = new String(encodedBuf); |
---|
| 81 | return result; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Decodes the given Base64-encoded data, |
---|
| 87 | * as specified in RFC-2045 (Section 6.8). |
---|
| 88 | * |
---|
| 89 | * @param aData the Base64-encoded aData. |
---|
| 90 | * @return the decoded <var>aData</var>. |
---|
| 91 | * @exception IllegalArgumentException if NULL or empty data is passed |
---|
| 92 | */ |
---|
| 93 | public static byte[] base64Decode(String aData) { |
---|
| 94 | if ((aData == null) || (aData.length() == 0)) |
---|
| 95 | throw new IllegalArgumentException("Can not decode NULL or empty string."); |
---|
| 96 | |
---|
| 97 | byte[] data = aData.getBytes(); |
---|
| 98 | |
---|
| 99 | // Skip padding from the end of encoded data |
---|
| 100 | int tail = data.length; |
---|
| 101 | while (data[tail-1] == '=') |
---|
| 102 | tail--; |
---|
| 103 | |
---|
| 104 | byte decodedBuf[] = new byte[tail - data.length/4]; |
---|
| 105 | |
---|
| 106 | // ASCII-printable to 0-63 conversion |
---|
| 107 | for (int i = 0; i < data.length; i++) |
---|
| 108 | data[i] = mBase64DecMap[data[i]]; |
---|
| 109 | |
---|
| 110 | // 4-byte to 3-byte conversion |
---|
| 111 | int srcIndex, destIndex; |
---|
| 112 | for (srcIndex = 0, destIndex=0; destIndex < decodedBuf.length-2; |
---|
| 113 | srcIndex += 4, destIndex += 3) { |
---|
| 114 | decodedBuf[destIndex] = (byte) ( ((data[srcIndex] << 2) & 255) | |
---|
| 115 | ((data[srcIndex+1] >>> 4) & 003) ); |
---|
| 116 | decodedBuf[destIndex+1] = (byte) ( ((data[srcIndex+1] << 4) & 255) | |
---|
| 117 | ((data[srcIndex+2] >>> 2) & 017) ); |
---|
| 118 | decodedBuf[destIndex+2] = (byte) ( ((data[srcIndex+2] << 6) & 255) | |
---|
| 119 | (data[srcIndex+3] & 077) ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | // Handle last 1 or 2 bytes |
---|
| 123 | if (destIndex < decodedBuf.length) |
---|
| 124 | decodedBuf[destIndex] = (byte) ( ((data[srcIndex] << 2) & 255) | |
---|
| 125 | ((data[srcIndex+1] >>> 4) & 003) ); |
---|
| 126 | if (++destIndex < decodedBuf.length) |
---|
| 127 | decodedBuf[destIndex] = (byte) ( ((data[srcIndex+1] << 4) & 255) | |
---|
| 128 | ((data[srcIndex+2] >>> 2) & 017) ); |
---|
| 129 | |
---|
| 130 | return decodedBuf; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
[5152] | 134 | public static String der2pem(byte[] der, boolean isCert){ |
---|
| 135 | |
---|
| 136 | int begin = 0; |
---|
| 137 | int lineSize = isCert ? 64 : 76; |
---|
| 138 | int end = lineSize; |
---|
[1174] | 139 | int bytesWriten = 0; |
---|
| 140 | |
---|
[5152] | 141 | String CRLF = "\r\n"; |
---|
| 142 | String base64encoded = Base64Utils.base64Encode(der); |
---|
| 143 | StringBuilder sb = new StringBuilder(); |
---|
[1174] | 144 | |
---|
[5152] | 145 | String beginCertificate = "-----BEGIN CERTIFICATE-----" +CRLF; |
---|
[5386] | 146 | String endCertificate = "-----END CERTIFICATE-----" +CRLF; |
---|
[1174] | 147 | |
---|
[5152] | 148 | if (isCert){ |
---|
| 149 | sb.append(beginCertificate); |
---|
| 150 | } |
---|
[1174] | 151 | |
---|
| 152 | do { |
---|
| 153 | |
---|
| 154 | if (base64encoded.length() - begin < end - begin){ |
---|
| 155 | end = base64encoded.length(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | String subs = base64encoded.substring(begin, end); |
---|
[5152] | 159 | sb.append(subs + CRLF); |
---|
[1174] | 160 | bytesWriten += end - begin; |
---|
| 161 | begin = end; |
---|
| 162 | end += lineSize; |
---|
| 163 | |
---|
| 164 | } while (bytesWriten != base64encoded.length()); |
---|
| 165 | |
---|
[5152] | 166 | if (isCert){ |
---|
| 167 | sb.append(endCertificate); |
---|
| 168 | } |
---|
[1174] | 169 | |
---|
| 170 | return sb.toString(); |
---|
| 171 | |
---|
[5152] | 172 | } |
---|
[1174] | 173 | |
---|
[5152] | 174 | public static String der2pem(byte[] der){ |
---|
| 175 | return Base64Utils.der2pem(der, false); |
---|
[1174] | 176 | } |
---|
| 177 | |
---|
| 178 | } |
---|