Introduce overloaded methods to Plaintext to configure the Charset to use
See gh-574.
This commit is contained in:
@@ -111,7 +111,12 @@ public interface VaultTransitOperations {
|
||||
void rotate(String keyName);
|
||||
|
||||
/**
|
||||
* Encrypts the provided plaintext using the named key.
|
||||
* Encrypts the provided plaintext using the named key. The given {@code plaintext} is
|
||||
* encoded into bytes using the {@link java.nio.charset.Charset#defaultCharset()
|
||||
* default charset}. Use
|
||||
* {@link #encrypt(String, org.springframework.vault.support.Plaintext)} to construct
|
||||
* a {@link org.springframework.vault.support.Plaintext#of(byte[]) Plaintext} object
|
||||
* from bytes to avoid {@link java.nio.charset.Charset} mismatches.
|
||||
* @param keyName must not be empty or {@literal null}.
|
||||
* @param plaintext must not be empty or {@literal null}.
|
||||
* @return cipher text.
|
||||
@@ -149,7 +154,12 @@ public interface VaultTransitOperations {
|
||||
List<VaultEncryptionResult> encrypt(String keyName, List<Plaintext> batchRequest);
|
||||
|
||||
/**
|
||||
* Decrypts the provided plaintext using the named key.
|
||||
* Decrypts the provided plaintext using the named key. The decoded {@code plaintext}
|
||||
* is decoded into {@link String} the {@link java.nio.charset.Charset#defaultCharset()
|
||||
* default charset}. Use
|
||||
* {@link #decrypt(String, org.springframework.vault.support.Ciphertext)} to obtain a
|
||||
* {@link org.springframework.vault.support.Ciphertext} object that allows to control
|
||||
* the {@link java.nio.charset.Charset} for later consumption.
|
||||
* @param keyName must not be empty or {@literal null}.
|
||||
* @param ciphertext must not be empty or {@literal null}.
|
||||
* @return plain text.
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.vault.support;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -70,19 +71,32 @@ public class Plaintext {
|
||||
/**
|
||||
* Factory method to create {@link Plaintext} using from {@link String}.
|
||||
* {@link String} is encoded to {@code byte} using the default
|
||||
* {@link java.nio.charset.Charset}.
|
||||
* {@link java.nio.charset.Charset}. Use {@link #of(String, java.nio.charset.Charset)}
|
||||
* to control the {@link java.nio.charset.Charset} to use.
|
||||
* @param plaintext the plaintext to encrypt, must not be {@literal null}.
|
||||
* @return the {@link Plaintext} for {@code plaintext}.
|
||||
*/
|
||||
public static Plaintext of(String plaintext) {
|
||||
return of(plaintext, Charset.defaultCharset());
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create {@link Plaintext} using from {@link String} using the
|
||||
* given {@link java.nio.charset.Charset}. {@link java.nio.charset.Charset}.
|
||||
* @param plaintext the plaintext to encrypt, must not be {@literal null}.
|
||||
* @return the {@link Plaintext} for {@code plaintext}.
|
||||
* @since 2.3
|
||||
*/
|
||||
public static Plaintext of(String plaintext, Charset charset) {
|
||||
|
||||
Assert.notNull(plaintext, "Plaintext must not be null");
|
||||
Assert.notNull(charset, "Charset must not be null");
|
||||
|
||||
if (plaintext.length() == 0) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
return of(plaintext.getBytes());
|
||||
return of(plaintext.getBytes(charset));
|
||||
}
|
||||
|
||||
public byte[] getPlaintext() {
|
||||
@@ -108,7 +122,20 @@ public class Plaintext {
|
||||
* {@link java.nio.charset.Charset}.
|
||||
*/
|
||||
public String asString() {
|
||||
return new String(getPlaintext());
|
||||
return asString(Charset.defaultCharset());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param charset the charset to use for decoding.
|
||||
* @return the plaintext as {@link String} decoded using the default
|
||||
* {@link java.nio.charset.Charset}.
|
||||
* @since 2.3
|
||||
*/
|
||||
public String asString(Charset charset) {
|
||||
|
||||
Assert.notNull(charset, "Charset must not be null");
|
||||
|
||||
return new String(getPlaintext(), charset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user