From ae25e394add1225729109be7ff52b07a0b773520 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sun, 9 Dec 2018 16:28:18 +0100 Subject: [PATCH] Replace own Base64 class with Spring's Base64Utils. Resolves gh-344. --- .../springframework/vault/support/Base64.java | 112 ------------------ .../vault/support/Certificate.java | 5 +- .../vault/support/CertificateBundle.java | 3 +- 3 files changed, 5 insertions(+), 115 deletions(-) delete mode 100644 spring-vault-core/src/main/java/org/springframework/vault/support/Base64.java diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/Base64.java b/spring-vault-core/src/main/java/org/springframework/vault/support/Base64.java deleted file mode 100644 index e38473cf..00000000 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/Base64.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 2016-2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.vault.support; - -class Base64 { - private Base64() { - } - - static byte[] decode(String in) { - // Ignore trailing '=' padding and whitespace from the input. - int limit = in.length(); - for (; limit > 0; limit--) { - char c = in.charAt(limit - 1); - if (c != '=' && c != '\n' && c != '\r' && c != ' ' && c != '\t') { - break; - } - } - - // If the input includes whitespace, this output array will be longer than - // necessary. - byte[] out = new byte[(int) (limit * 6L / 8L)]; - int outCount = 0; - int inCount = 0; - - int word = 0; - for (int pos = 0; pos < limit; pos++) { - char c = in.charAt(pos); - - int bits; - if (c >= 'A' && c <= 'Z') { - // char ASCII value - // A 65 0 - // Z 90 25 (ASCII - 65) - bits = c - 65; - } - else if (c >= 'a' && c <= 'z') { - // char ASCII value - // a 97 26 - // z 122 51 (ASCII - 71) - bits = c - 71; - } - else if (c >= '0' && c <= '9') { - // char ASCII value - // 0 48 52 - // 9 57 61 (ASCII + 4) - bits = c + 4; - } - else if (c == '+' || c == '-') { - bits = 62; - } - else if (c == '/' || c == '_') { - bits = 63; - } - else if (c == '\n' || c == '\r' || c == ' ' || c == '\t') { - continue; - } - else { - return null; - } - - // Append this char's 6 bits to the word. - word = (word << 6) | (byte) bits; - - // For every 4 chars of input, we accumulate 24 bits of output. Emit 3 bytes. - inCount++; - if (inCount % 4 == 0) { - out[outCount++] = (byte) (word >> 16); - out[outCount++] = (byte) (word >> 8); - out[outCount++] = (byte) word; - } - } - - int lastWordChars = inCount % 4; - if (lastWordChars == 1) { - // We read 1 char followed by "===". But 6 bits is a truncated byte! Fail. - return null; - } - else if (lastWordChars == 2) { - // We read 2 chars followed by "==". Emit 1 byte with 8 of those 12 bits. - word = word << 12; - out[outCount++] = (byte) (word >> 16); - } - else if (lastWordChars == 3) { - // We read 3 chars, followed by "=". Emit 2 bytes for 16 of those 18 bits. - word = word << 6; - out[outCount++] = (byte) (word >> 16); - out[outCount++] = (byte) (word >> 8); - } - - // If we sized our out array perfectly, we're done. - if (outCount == out.length) - return out; - - // Copy the decoded bytes to a new, right-sized array. - byte[] prefix = new byte[outCount]; - System.arraycopy(out, 0, prefix, 0, outCount); - return prefix; - } -} diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/Certificate.java b/spring-vault-core/src/main/java/org/springframework/vault/support/Certificate.java index 37f2d832..c7feada5 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/Certificate.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/support/Certificate.java @@ -24,6 +24,7 @@ import java.security.cert.X509Certificate; import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.util.Assert; +import org.springframework.util.Base64Utils; import org.springframework.vault.VaultException; /** @@ -102,7 +103,7 @@ public class Certificate { public X509Certificate getX509Certificate() { try { - byte[] bytes = Base64.decode(getCertificate()); + byte[] bytes = Base64Utils.decodeFromString(getCertificate()); return KeystoreUtil.getCertificate(bytes); } catch (IOException | CertificateException e) { @@ -119,7 +120,7 @@ public class Certificate { public X509Certificate getX509IssuerCertificate() { try { - byte[] bytes = Base64.decode(getIssuingCaCertificate()); + byte[] bytes = Base64Utils.decodeFromString(getIssuingCaCertificate()); return KeystoreUtil.getCertificate(bytes); } catch (IOException | CertificateException e) { diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java b/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java index 79d385e3..24e53b15 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/support/CertificateBundle.java @@ -24,6 +24,7 @@ import java.security.spec.KeySpec; import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.util.Assert; +import org.springframework.util.Base64Utils; import org.springframework.vault.VaultException; /** @@ -89,7 +90,7 @@ public class CertificateBundle extends Certificate { public KeySpec getPrivateKeySpec() { try { - byte[] bytes = Base64.decode(getPrivateKey()); + byte[] bytes = Base64Utils.decodeFromString(getPrivateKey()); return KeystoreUtil.getRSAKeySpec(bytes); } catch (IOException e) {