use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.
the class SecureQueryDecryptProcessor method decode.
String decode(String string) throws UnsupportedEncodingException {
byte[] bytes = Base64.decodeBase64(string);
EncryptionResult result = EncryptionResult.fromByteArray(bytes);
byte[] clear = null;
try {
clear = encryptor.decrypt(result.salt, result.iv, result.cipher);
} catch (Exception e) {
log.unableToDecryptValue(e);
}
if (clear != null) {
return new String(clear, StandardCharsets.UTF_8);
}
return null;
}
use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.
the class KnoxSessionStore method uncompressDecryptBase64.
private Serializable uncompressDecryptBase64(final String v) {
if (v != null && !v.isEmpty()) {
byte[] bytes = Base64.decodeBase64(v);
EncryptionResult result = EncryptionResult.fromByteArray(bytes);
byte[] clear = cryptoService.decryptForCluster(this.clusterName, PAC4J_PASSWORD, result.cipher, result.iv, result.salt);
if (clear != null) {
try {
return javaSerializationHelper.deserializeFromBytes(unCompress(clear));
} catch (IOException e) {
throw new TechnicalException(e);
}
}
}
return null;
}
use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.
the class KnoxSessionStore method compressEncryptBase64.
private String compressEncryptBase64(final Object o) {
if (o == null || o.equals("") || (o instanceof Map<?, ?> && ((Map<?, ?>) o).isEmpty())) {
return null;
} else {
byte[] bytes = javaSerializationHelper.serializeToBytes((Serializable) o);
/* compress the data */
try {
bytes = compress(bytes);
if (bytes.length > 3000) {
logger.warn("Cookie too big, it might not be properly set");
}
} catch (final IOException e) {
throw new TechnicalException(e);
}
EncryptionResult result = cryptoService.encryptForCluster(this.clusterName, PAC4J_PASSWORD, bytes);
return Base64.encodeBase64String(result.toByteAray());
}
}
use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.
the class ConfigurableEncryptor method encrypt.
public EncryptionResult encrypt(byte[] plain) throws Exception {
byte[] salt = new byte[saltSize];
SecureRandom rnd = new SecureRandom();
rnd.nextBytes(salt);
SecretKey tmp = getKeyFromPassword(new String(passPhrase), salt);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), alg);
Cipher ecipher = Cipher.getInstance(transformation);
ecipher.init(Cipher.ENCRYPT_MODE, secret);
return new EncryptionResult(salt, ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(), ecipher.doFinal(plain));
}
Aggregations