Java Utililty Methods XML Base64 Encode Decode

List of utility methods to do XML Base64 Encode Decode

Description

The list of methods to do XML Base64 Encode Decode are organized into topic(s).

Method

StringencodeBase64(String input)
Creates a base 64 encoded string from the given input string.
return DatatypeConverter.printBase64Binary(input.getBytes(StandardCharsets.UTF_8));
StringencodeBase64(String stringValue)
encode Base
byte[] bytesValue = stringValue.getBytes("UTF-8");
String encodedValue = DatatypeConverter.printBase64Binary(bytesValue);
return encodedValue;
StringencodeBase64Binary(byte[] data)
encode Base Binary
return DatatypeConverter.printBase64Binary(data);
StringfromBase64(String base64)
from Base
byte[] encodedHelloBytes = DatatypeConverter.parseBase64Binary(base64);
return new String(encodedHelloBytes, StandardCharsets.UTF_8);
byte[]fromBase64(String hex)
Converts a string of hexadecimal characters into a byte array.
return DatatypeConverter.parseBase64Binary(hex);
StringgetBase64(byte[] bytes)
get Base
String h = DatatypeConverter.printBase64Binary(bytes);
h = h.replaceFirst("=*$", "");
h = h.replace('+', '-');
h = h.replace('/', '_');
return h;
StringgetHashBase64(String origStr, String digestAlgorithm)
get Hash Base
MessageDigest sha = MessageDigest.getInstance(digestAlgorithm);
byte[] resultDigest = sha.digest(origStr.getBytes());
return DatatypeConverter.printBase64Binary(resultDigest);
byte[]readBase64Bytes(String str)
read Base Bytes
return DatatypeConverter.parseBase64Binary(str);
StringreadFileToBase64(String filePath)
Lee todos los bytes de un archivo en particular y lo convierte a un string en Base64
Path path = FileSystems.getDefault().getPath(filePath);
byte[] bytesFromFile = Files.readAllBytes(path);
return byteToBase64(bytesFromFile);
Stringsha256SaltBase64(String password, String salt)
sha256 encryption with salt.
try {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();
    byte[] binaryData = digest.digest((salt + password).getBytes("UTF-8"));
    return DatatypeConverter.printBase64Binary(binaryData);
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
    throw new Exception("Error al encriptar el password.", e);