@Override public String encode(final Serializable value, final Object[] parameters) { return EncodingUtils.encodeBase64(value.toString().getBytes(StandardCharsets.UTF_8)); }
/** * Base64-decode the given string as byte[]. * * @param data the base64 string * @return the encoded array */ public static byte[] decodeUrlSafeBase64(final String data) { return decodeBase64(data); }
/** * Gets encryption certificate decoded. * * @return the encryption certificate decoded */ @JsonIgnore public String getEncryptionCertificateDecoded() { if (EncodingUtils.isBase64(encryptionCertificate)) { return EncodingUtils.decodeBase64ToString(encryptionCertificate); } return encryptionCertificate; }
private void ensureEncryptionKeyExists(final String encryptionSecretKey, final int encryptionKeySize) { final byte[] genEncryptionKey; if (StringUtils.isBlank(encryptionSecretKey)) { LOGGER.warn("Secret key for encryption is not defined under [{}]. CAS will attempt to auto-generate the encryption key", getEncryptionKeySetting()); val key = new Base64RandomStringGenerator(encryptionKeySize).getNewString(); LOGGER.warn("Generated encryption key [{}] of size [{}]. The generated key MUST be added to CAS settings under setting [{}].", key, encryptionKeySize, getEncryptionKeySetting()); genEncryptionKey = EncodingUtils.decodeBase64(key); } else { val base64 = EncodingUtils.isBase64(encryptionSecretKey); val key = base64 ? EncodingUtils.decodeBase64(encryptionSecretKey) : ArrayUtils.EMPTY_BYTE_ARRAY; if (base64 && key.length == encryptionKeySize) { LOGGER.trace("Secret key for encryption defined under [{}] is Base64 encoded.", getEncryptionKeySetting()); genEncryptionKey = key; } else if (encryptionSecretKey.length() != encryptionKeySize) { LOGGER.warn("Secret key for encryption defined under [{}] is Base64 encoded but the size does not match the key size [{}].", getEncryptionKeySetting(), encryptionKeySize); genEncryptionKey = encryptionSecretKey.getBytes(StandardCharsets.UTF_8); } else { LOGGER.warn("Secret key for encryption defined under [{}] is not Base64 encoded. Clear the setting to regenerate (Recommended) or replace with" + " [{}].", getEncryptionKeySetting(), EncodingUtils.encodeBase64(encryptionSecretKey)); genEncryptionKey = encryptionSecretKey.getBytes(StandardCharsets.UTF_8); } } this.encryptionSecretKey = genEncryptionKey; }
@Override protected String convertBytesToString(final byte[] random) { return EncodingUtils.hexEncode(random); } }
/** * Hex decode string. * * @param data the data * @return the string */ public static String hexDecode(final String data) { if (StringUtils.isNotBlank(data)) { return hexDecode(data.toCharArray()); } return null; }
/** * Converts byte[] to String by Base64 encoding. * * @param random raw bytes * @return a converted String */ @Override protected String convertBytesToString(final byte[] random) { return EncodingUtils.encodeUrlSafeBase64(random); }
@Override public Map<String, Object> getConsentableAttributesFrom(final ConsentDecision decision) { try { val result = this.consentCipherExecutor.decode(decision.getAttributes()); if (StringUtils.isBlank(result)) { LOGGER.warn("Unable to decipher attributes from consent decision [{}]", decision.getId()); return new HashMap<>(0); } val names = EncodingUtils.decodeBase64ToString(result); return MAPPER.readValue(names, Map.class); } catch (final Exception e) { throw new IllegalArgumentException("Could not serialize attributes for consent decision"); } }
/** * Computes hex encoded digest. * * @param alg Digest algorithm to use * @param data data to be hashed * @return hex encoded hash */ public static String digest(final String alg, final byte[] data) { return EncodingUtils.hexEncode(rawDigest(alg, data)); }
@Override public Map<String, Object> encodeAttributes(final Map<String, Object> attributes, final RegisteredService service) { val finalAttributes = Maps.<String, Object>newHashMapWithExpectedSize(attributes.size()); attributes.forEach((k, v) -> { val attributeName = EncodingUtils.hexDecode(k); if (StringUtils.isNotBlank(attributeName)) { LOGGER.debug("Decoded SAML attribute [{}] to [{}] with value(s) [{}]", k, attributeName, v); finalAttributes.put(attributeName, v); } else { LOGGER.debug("Unable to decode SAML attribute [{}]; accepting it verbatim", k); finalAttributes.put(k, v); } }); return finalAttributes; } }
/** * Gets metadata decoded. * * @return the metadata decoded */ @JsonIgnore public String getMetadataDecoded() { if (EncodingUtils.isBase64(metadata)) { return EncodingUtils.decodeBase64ToString(metadata); } return metadata; } }
private static String calculateCodeVerifierHash(final String method, final String codeVerifier) { if ("plain".equalsIgnoreCase(method)) { return codeVerifier; } if ("S256".equalsIgnoreCase(method)) { val sha256 = DigestUtils.sha256(codeVerifier); return EncodingUtils.encodeUrlSafeBase64(sha256.getBytes(StandardCharsets.UTF_8)); } throw new CredentialsException("Code verification method is unrecognized: " + method); } }
/** * Url encode a value via UTF-8. * * @param value the value to encode * @return the encoded value */ public static String urlEncode(final String value) { return urlEncode(value, StandardCharsets.UTF_8.name()); }
/** * Digest and encode with salt string. * * @param md the md * @return the string */ protected String digestAndEncodeWithSalt(final MessageDigest md) { val sanitizedSalt = StringUtils.replace(salt, "\n", " "); val digested = md.digest(sanitizedSalt.getBytes(StandardCharsets.UTF_8)); return EncodingUtils.encodeBase64(digested, false); }
/** * Base64-decode the given string as String. * * @param data the base64 string * @return the string */ public static String decodeBase64ToString(final String data) { return new String(decodeBase64(data), StandardCharsets.UTF_8); }
private String serializeSessionValues(final Map<String, Object> attributes) { val blob = serializer.toString(attributes); return EncodingUtils.hexEncode(blob); }
throw new IllegalArgumentException("No cookie could be found to determine session state"); val blob = EncodingUtils.hexDecode(value); val session = serializer.from(blob); request.setAttribute(this.themeParamName, session.get(this.themeParamName));
/** * Gets signing certificate decoded. * * @return the signing certificate decoded */ @JsonIgnore public String getSigningCertificateDecoded() { if (EncodingUtils.isBase64(signingCertificate)) { return EncodingUtils.decodeBase64ToString(signingCertificate); } return signingCertificate; }
/** * Encodes the message in UTF-8 format in preparation to send. * * @param message Message to format and encode * @return The encoded message. */ protected String formatOutputMessageInternal(final String message) { try { return EncodingUtils.urlEncode(message); } catch (final Exception e) { LOGGER.warn("Unable to encode URL " + message, e); } return message; } }