|
| 1 | +/* |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 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 | +package feign.auth; |
| 17 | + |
| 18 | +import java.io.UnsupportedEncodingException; |
| 19 | + |
| 20 | +/** |
| 21 | + * copied from <a href="https://github.com/square/okhttp/blob/master/okhttp-protocols/src/main/java/com/squareup/okhttp/internal/Base64.java">okhttp</a> |
| 22 | + * @author Alexander Y. Kleymenov |
| 23 | + */ |
| 24 | +final class Base64 { |
| 25 | + |
| 26 | + public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 27 | + |
| 28 | + private Base64() { |
| 29 | + } |
| 30 | + |
| 31 | + public static byte[] decode(byte[] in) { |
| 32 | + return decode(in, in.length); |
| 33 | + } |
| 34 | + |
| 35 | + public static byte[] decode(byte[] in, int len) { |
| 36 | + // approximate output length |
| 37 | + int length = len / 4 * 3; |
| 38 | + // return an empty array on empty or short input without padding |
| 39 | + if (length == 0) { |
| 40 | + return EMPTY_BYTE_ARRAY; |
| 41 | + } |
| 42 | + // temporary array |
| 43 | + byte[] out = new byte[length]; |
| 44 | + // number of padding characters ('=') |
| 45 | + int pad = 0; |
| 46 | + byte chr; |
| 47 | + // compute the number of the padding characters |
| 48 | + // and adjust the length of the input |
| 49 | + for (; ; len--) { |
| 50 | + chr = in[len - 1]; |
| 51 | + // skip the neutral characters |
| 52 | + if ((chr == '\n') || (chr == '\r') || (chr == ' ') || (chr == '\t')) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + if (chr == '=') { |
| 56 | + pad++; |
| 57 | + } else { |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + // index in the output array |
| 62 | + int outIndex = 0; |
| 63 | + // index in the input array |
| 64 | + int inIndex = 0; |
| 65 | + // holds the value of the input character |
| 66 | + int bits = 0; |
| 67 | + // holds the value of the input quantum |
| 68 | + int quantum = 0; |
| 69 | + for (int i = 0; i < len; i++) { |
| 70 | + chr = in[i]; |
| 71 | + // skip the neutral characters |
| 72 | + if ((chr == '\n') || (chr == '\r') || (chr == ' ') || (chr == '\t')) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + if ((chr >= 'A') && (chr <= 'Z')) { |
| 76 | + // char ASCII value |
| 77 | + // A 65 0 |
| 78 | + // Z 90 25 (ASCII - 65) |
| 79 | + bits = chr - 65; |
| 80 | + } else if ((chr >= 'a') && (chr <= 'z')) { |
| 81 | + // char ASCII value |
| 82 | + // a 97 26 |
| 83 | + // z 122 51 (ASCII - 71) |
| 84 | + bits = chr - 71; |
| 85 | + } else if ((chr >= '0') && (chr <= '9')) { |
| 86 | + // char ASCII value |
| 87 | + // 0 48 52 |
| 88 | + // 9 57 61 (ASCII + 4) |
| 89 | + bits = chr + 4; |
| 90 | + } else if (chr == '+') { |
| 91 | + bits = 62; |
| 92 | + } else if (chr == '/') { |
| 93 | + bits = 63; |
| 94 | + } else { |
| 95 | + return null; |
| 96 | + } |
| 97 | + // append the value to the quantum |
| 98 | + quantum = (quantum << 6) | (byte) bits; |
| 99 | + if (inIndex % 4 == 3) { |
| 100 | + // 4 characters were read, so make the output: |
| 101 | + out[outIndex++] = (byte) (quantum >> 16); |
| 102 | + out[outIndex++] = (byte) (quantum >> 8); |
| 103 | + out[outIndex++] = (byte) quantum; |
| 104 | + } |
| 105 | + inIndex++; |
| 106 | + } |
| 107 | + if (pad > 0) { |
| 108 | + // adjust the quantum value according to the padding |
| 109 | + quantum = quantum << (6 * pad); |
| 110 | + // make output |
| 111 | + out[outIndex++] = (byte) (quantum >> 16); |
| 112 | + if (pad == 1) { |
| 113 | + out[outIndex++] = (byte) (quantum >> 8); |
| 114 | + } |
| 115 | + } |
| 116 | + // create the resulting array |
| 117 | + byte[] result = new byte[outIndex]; |
| 118 | + System.arraycopy(out, 0, result, 0, outIndex); |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + private static final byte[] MAP = new byte[] { |
| 123 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
| 124 | + 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
| 125 | + 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', |
| 126 | + '5', '6', '7', '8', '9', '+', '/' |
| 127 | + }; |
| 128 | + |
| 129 | + public static String encode(byte[] in) { |
| 130 | + int length = (in.length + 2) * 4 / 3; |
| 131 | + byte[] out = new byte[length]; |
| 132 | + int index = 0, end = in.length - in.length % 3; |
| 133 | + for (int i = 0; i < end; i += 3) { |
| 134 | + out[index++] = MAP[(in[i] & 0xff) >> 2]; |
| 135 | + out[index++] = MAP[((in[i] & 0x03) << 4) | ((in[i + 1] & 0xff) >> 4)]; |
| 136 | + out[index++] = MAP[((in[i + 1] & 0x0f) << 2) | ((in[i + 2] & 0xff) >> 6)]; |
| 137 | + out[index++] = MAP[(in[i + 2] & 0x3f)]; |
| 138 | + } |
| 139 | + switch (in.length % 3) { |
| 140 | + case 1: |
| 141 | + out[index++] = MAP[(in[end] & 0xff) >> 2]; |
| 142 | + out[index++] = MAP[(in[end] & 0x03) << 4]; |
| 143 | + out[index++] = '='; |
| 144 | + out[index++] = '='; |
| 145 | + break; |
| 146 | + case 2: |
| 147 | + out[index++] = MAP[(in[end] & 0xff) >> 2]; |
| 148 | + out[index++] = MAP[((in[end] & 0x03) << 4) | ((in[end + 1] & 0xff) >> 4)]; |
| 149 | + out[index++] = MAP[((in[end + 1] & 0x0f) << 2)]; |
| 150 | + out[index++] = '='; |
| 151 | + break; |
| 152 | + } |
| 153 | + try { |
| 154 | + return new String(out, 0, index, "US-ASCII"); |
| 155 | + } catch (UnsupportedEncodingException e) { |
| 156 | + throw new AssertionError(e); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments