@Override public String encode(final Serializable value, final Object[] parameters) { return EncodingUtils.encodeBase64(value.toString().getBytes(StandardCharsets.UTF_8)); }
/** * 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); }
/** * Deflate the given string via a {@link java.util.zip.Deflater}. * * @param data the data * @return base64 encoded string */ public static String deflate(final String data) { val deflater = new Deflater(); deflater.setInput(data.getBytes(StandardCharsets.UTF_8)); deflater.finish(); val buffer = new byte[data.length()]; val resultSize = deflater.deflate(buffer); val output = new byte[resultSize]; System.arraycopy(buffer, 0, output, 0, resultSize); return EncodingUtils.encodeBase64(output); }
/** * Sha base 64 string. * * @param salt the salt * @param data the data * @param separator the separator * @return the string */ public static String shaBase64(final String salt, final String data, final String separator) { val result = rawDigest(MessageDigestAlgorithms.SHA_1, salt, separator == null ? data : data + separator); return EncodingUtils.encodeBase64(result); }
private static Object transformAttributeValueIfNecessary(final Object attributeValue) { if (getBinaryAttributeValuePredicate().test(attributeValue)) { return EncodingUtils.encodeBase64((byte[]) attributeValue); } return attributeValue; }
/** * Sha base 64 string. * * @param salt the salt * @param data the data * @param separator a string separator, if any * @param chunked the chunked * @return the string */ public static String shaBase64(final String salt, final String data, final String separator, final boolean chunked) { val result = rawDigest(MessageDigestAlgorithms.SHA_1, salt, separator == null ? data : data + separator); return EncodingUtils.encodeBase64(result, chunked); }
/** * Prepare http request. Tries to set the authorization header * in cases where the URL endpoint does not actually produce the header * on its own. * * @param request the request * @param basicAuthUsername the basic auth username * @param basicAuthPassword the basic auth password * @param parameters the parameters */ private static void prepareHttpRequest(final HttpUriRequest request, final String basicAuthUsername, final String basicAuthPassword, final Map<String, Object> parameters) { if (StringUtils.isNotBlank(basicAuthUsername) && StringUtils.isNotBlank(basicAuthPassword)) { val auth = EncodingUtils.encodeBase64(basicAuthUsername + ':' + basicAuthPassword); request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + auth); } }
/** * Use ZipOutputStream to zip text to byte array, then convert * byte array to base64 string, so it can be transferred via http request. * * @param srcTxt the src txt * @return the string in UTF-8 format and base64'ed, or null. */ @SneakyThrows public static String compress(final String srcTxt) { try (val rstBao = new ByteArrayOutputStream(); val zos = new GZIPOutputStream(rstBao)) { zos.write(srcTxt.getBytes(StandardCharsets.UTF_8)); zos.flush(); zos.finish(); val bytes = rstBao.toByteArray(); val base64 = StringUtils.remove(EncodingUtils.encodeBase64(bytes), '\0'); return new String(StandardCharsets.UTF_8.encode(base64).array(), StandardCharsets.UTF_8); } } }
/** * Create headers org . springframework . http . http headers. * * @param basicAuthUser the basic auth user * @param basicAuthPassword the basic auth password * @return the org . springframework . http . http headers */ public static org.springframework.http.HttpHeaders createBasicAuthHeaders(final String basicAuthUser, final String basicAuthPassword) { val acceptHeaders = new org.springframework.http.HttpHeaders(); acceptHeaders.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON)); if (StringUtils.isNotBlank(basicAuthUser) && StringUtils.isNotBlank(basicAuthPassword)) { val authorization = basicAuthUser + ':' + basicAuthPassword; val basic = EncodingUtils.encodeBase64(authorization.getBytes(Charset.forName("US-ASCII"))); acceptHeaders.set(org.springframework.http.HttpHeaders.AUTHORIZATION, "Basic " + basic); } return acceptHeaders; } }
/** * Build consent attribute names string. * * @param attributes the attributes * @return the string */ protected String buildAndEncodeConsentAttributes(final Map<String, Object> attributes) { try { val json = MAPPER.writer(new MinimalPrettyPrinter()).writeValueAsString(attributes); val base64 = EncodingUtils.encodeBase64(json); return this.consentCipherExecutor.encode(base64); } catch (final Exception e) { throw new IllegalArgumentException("Could not serialize attributes for consent decision"); } } }
/** * Encrypt using the given cipher associated with the service, * and encode the data in base 64. * * @param data the data * @param service the registered service * @return the encoded piece of data in base64 */ @Override public String encode(final String data, final Optional<RegisteredService> service) { try { if (service.isPresent()) { val registeredService = service.get(); val publicKey = createRegisteredServicePublicKey(registeredService); val result = encodeInternal(data, publicKey, registeredService); if (result != null) { return EncodingUtils.encodeBase64(result); } } } catch (final Exception e) { LOGGER.warn(e.getMessage(), e); } return null; }
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; }
/** * Build encrypter for saml object encrypter. * * @param samlObject the saml object * @param service the service * @param adaptor the adaptor * @return the encrypter */ @SneakyThrows protected Encrypter buildEncrypterForSamlObject(final Object samlObject, final SamlRegisteredService service, final SamlRegisteredServiceServiceProviderMetadataFacade adaptor) { val className = samlObject.getClass().getName(); val entityId = adaptor.getEntityId(); LOGGER.debug("Attempting to encrypt [{}] for [{}]", className, entityId); val credential = getKeyEncryptionCredential(entityId, adaptor, service); LOGGER.info("Found encryption public key: [{}]", EncodingUtils.encodeBase64(credential.getPublicKey().getEncoded())); val keyEncParams = getKeyEncryptionParameters(samlObject, service, adaptor, credential); LOGGER.debug("Key encryption algorithm for [{}] is [{}]", keyEncParams.getRecipient(), keyEncParams.getAlgorithm()); val dataEncParams = getDataEncryptionParameters(samlObject, service, adaptor); LOGGER.debug("Data encryption algorithm for [{}] is [{}]", entityId, dataEncParams.getAlgorithm()); val encrypter = getEncrypter(samlObject, service, adaptor, keyEncParams, dataEncParams); LOGGER.debug("Attempting to encrypt [{}] for [{}] with key placement of [{}]", className, entityId, encrypter.getKeyPlacement()); return encrypter; }
LOGGER.trace("Logout request payload is [{}]", payload); val message = EncodingUtils.encodeBase64(payload.getBytes(StandardCharsets.UTF_8), false); LOGGER.trace("Logout message encoded in base64 is [{}]", message);
/** * Sign jws. * * @param key the key * @param value the value * @param algHeaderValue the alg header value * @return the byte [ ] */ @SneakyThrows public static byte[] signJws(final Key key, final byte[] value, final String algHeaderValue) { val base64 = EncodingUtils.encodeBase64(value); val jws = new JsonWebSignature(); jws.setEncodedPayload(base64); jws.setAlgorithmHeaderValue(algHeaderValue); jws.setKey(key); jws.setHeader("typ", "JWT"); return jws.getCompactSerialization().getBytes(StandardCharsets.UTF_8); }
SamlIdPUtils.getIssuerFromSamlObject(authnRequest))); val samlRequest = EncodingUtils.encodeBase64(writer.toString().getBytes(StandardCharsets.UTF_8)); builder.getQueryParams().add( new net.shibboleth.utilities.java.support.collection.Pair<>(SamlProtocolConstants.PARAMETER_SAML_REQUEST,