Skip to content

Commit

Permalink
Move off deprecated Base64Utils.
Browse files Browse the repository at this point in the history
Closes gh-797
  • Loading branch information
mp911de committed Jun 19, 2023
1 parent 38d87b7 commit 98e16ed
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.vault.core;

import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -26,7 +27,6 @@

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.vault.VaultException;
Expand Down Expand Up @@ -168,7 +168,7 @@ public String encrypt(String keyName, String plaintext) {

Map<String, String> request = new LinkedHashMap<>();

request.put("plaintext", Base64Utils.encodeToString(plaintext.getBytes()));
request.put("plaintext", Base64.getEncoder().encodeToString(plaintext.getBytes()));

return (String) this.vaultOperations.write(String.format("%s/encrypt/%s", this.path, keyName), request)
.getRequiredData()
Expand All @@ -195,7 +195,7 @@ public String encrypt(String keyName, byte[] plaintext, VaultTransitContext tran

Map<String, String> request = new LinkedHashMap<>();

request.put("plaintext", Base64Utils.encodeToString(plaintext));
request.put("plaintext", Base64.getEncoder().encodeToString(plaintext));

applyTransitOptions(transitContext, request);

Expand All @@ -216,7 +216,7 @@ public List<VaultEncryptionResult> encrypt(String keyName, List<Plaintext> batch

Map<String, String> vaultRequest = new LinkedHashMap<>(2);

vaultRequest.put("plaintext", Base64Utils.encodeToString(request.getPlaintext()));
vaultRequest.put("plaintext", Base64.getEncoder().encodeToString(request.getPlaintext()));

if (request.getContext() != null) {
applyTransitOptions(request.getContext(), vaultRequest);
Expand Down Expand Up @@ -246,7 +246,7 @@ public String decrypt(String keyName, String ciphertext) {
.getRequiredData()
.get("plaintext");

return new String(Base64Utils.decodeFromString(plaintext));
return new String(Base64.getDecoder().decode(plaintext));
}

@Override
Expand Down Expand Up @@ -278,7 +278,7 @@ public byte[] decrypt(String keyName, String ciphertext, VaultTransitContext tra
.getRequiredData()
.get("plaintext");

return Base64Utils.decodeFromString(plaintext);
return Base64.getDecoder().decode(plaintext);
}

@Override
Expand Down Expand Up @@ -360,7 +360,9 @@ public Hmac getHmac(String keyName, VaultHmacRequest hmacRequest) {
Map<String, Object> request = new LinkedHashMap<>(3);
PropertyMapper mapper = PropertyMapper.get();

mapper.from(hmacRequest.getPlaintext()::getPlaintext).as(Base64Utils::encodeToString).to("input", request);
mapper.from(hmacRequest.getPlaintext()::getPlaintext)
.as(Base64.getEncoder()::encodeToString)
.to("input", request);
mapper.from(hmacRequest::getAlgorithm).whenHasText().to("algorithm", request);
mapper.from(hmacRequest::getKeyVersion).whenNonNull().to("key_version", request);

Expand Down Expand Up @@ -391,7 +393,9 @@ public Signature sign(String keyName, VaultSignRequest signRequest) {
Map<String, Object> request = new LinkedHashMap<>(3);
PropertyMapper mapper = PropertyMapper.get();

mapper.from(signRequest.getPlaintext()::getPlaintext).as(Base64Utils::encodeToString).to("input", request);
mapper.from(signRequest.getPlaintext()::getPlaintext)
.as(Base64.getEncoder()::encodeToString)
.to("input", request);
mapper.from(signRequest::getHashAlgorithm).whenHasText().to("hash_algorithm", request);
mapper.from(signRequest::getSignatureAlgorithm).whenHasText().to("signature_algorithm", request);

Expand Down Expand Up @@ -423,7 +427,7 @@ public SignatureValidation verify(String keyName, VaultSignatureVerificationRequ
PropertyMapper mapper = PropertyMapper.get();

mapper.from(verificationRequest.getPlaintext()::getPlaintext)
.as(Base64Utils::encodeToString)
.as(Base64.getEncoder()::encodeToString)
.to("input", request);
mapper.from(verificationRequest::getHmac).whenNonNull().as(Hmac::getHmac).to("hmac", request);
mapper.from(verificationRequest::getSignature)
Expand All @@ -447,11 +451,11 @@ public SignatureValidation verify(String keyName, VaultSignatureVerificationRequ
private static void applyTransitOptions(VaultTransitContext context, Map<String, String> request) {

if (!ObjectUtils.isEmpty(context.getContext())) {
request.put("context", Base64Utils.encodeToString(context.getContext()));
request.put("context", Base64.getEncoder().encodeToString(context.getContext()));
}

if (!ObjectUtils.isEmpty(context.getNonce())) {
request.put("nonce", Base64Utils.encodeToString(context.getNonce()));
request.put("nonce", Base64.getEncoder().encodeToString(context.getNonce()));
}
}

Expand Down Expand Up @@ -517,7 +521,7 @@ private static VaultDecryptionResult getDecryptionResult(Map<String, String> dat

if (StringUtils.hasText(data.get("plaintext"))) {

byte[] plaintext = Base64Utils.decodeFromString(data.get("plaintext"));
byte[] plaintext = Base64.getDecoder().decode(data.get("plaintext"));
return new VaultDecryptionResult(Plaintext.of(plaintext).with(ciphertext.getContext()));
}

Expand Down

0 comments on commit 98e16ed

Please sign in to comment.