public Object createProtectedString(String string) throws EncryptionException { return protector.encryptString(string); }
public ProtectedStringType encrypt(String string) { try { return protector.encryptString(string); } catch (EncryptionException e) { throw new SystemException(e.getMessage(), e); } }
public static <D extends ItemDefinition> Object convertToOutputValue(String stringValue, D outputDefinition, Protector protector) throws ExpressionEvaluationException, SchemaException { if (stringValue == null) { return null; } QName outputType = outputDefinition.getTypeName(); if (outputType.equals(DOMUtil.XSD_STRING)) { return stringValue; } else if (outputType.equals(ProtectedStringType.COMPLEX_TYPE)) { try { return protector.encryptString(stringValue); } catch (EncryptionException e) { throw new ExpressionEvaluationException("Crypto error: " + e.getMessage(), e); } } else if (XmlTypeConverter.canConvert(outputType)) { Class<?> outputJavaType = XsdTypeMapper.toJavaType(outputType); try { return XmlTypeConverter.toJavaValue(stringValue, outputJavaType, true); } catch (NumberFormatException e) { throw new SchemaException("Cannot convert string '" + stringValue + "' to data type " + outputType + ": invalid number format", e); } catch (IllegalArgumentException e) { throw new SchemaException("Cannot convert string '" + stringValue + "' to data type " + outputType + ": " + e.getMessage(), e); } } else { throw new IllegalArgumentException( "Expression cannot generate values for properties of type " + outputType); } }
intermediateVal = protector.encryptString(valueToEncrypt); } catch (EncryptionException e) { throw new SystemException(e.getMessage(), e);
@Test public void testParseProtectedStringEncrypted() throws Exception { final String TEST_NAME = "testParseProtectedStringEncrypted"; displayTestTitle(TEST_NAME); // GIVEN Protector protector = PrismInternalTestUtil.createProtector(XMLCipher.AES_128); ProtectedStringType protectedStringType = protector.encryptString("salalala"); PrismContext prismContext = PrismTestUtil.getPrismContext(); // WHEN MapXNodeImpl protectedStringTypeXNode = ((PrismContextImpl) prismContext).getBeanMarshaller().marshalProtectedDataType(protectedStringType, null); System.out.println("Protected string type XNode: " + protectedStringTypeXNode.debugDump()); // THEN ProtectedStringType unmarshalled = new ProtectedStringType(); prismContext.hacks().parseProtectedType(unmarshalled, protectedStringTypeXNode, prismContext, createDefaultParsingContext()); System.out.println("Unmarshalled value: " + unmarshalled); assertEquals("Unmarshalled value differs from the original", protectedStringType, unmarshalled); }
ProtectedStringType ps = protector.encryptString("t4k30v3rTh3W0rld");
ProtectedStringType passPs = protector.encryptString("salalala");
private static void reencryptProtectedStringType(ProtectedStringType ps, String propName, Holder<Integer> modCountHolder, Protector protector) { if (ps == null) { // nothing to do here } else if (ps.isHashed()) { // nothing to do here } else if (ps.getClearValue() != null) { try { protector.encrypt(ps); increment(modCountHolder); } catch (EncryptionException e) { throw new TunnelException(new EncryptionException("Failed to encrypt value for field " + propName + ": " + e.getMessage(), e)); } } else if (ps.getEncryptedDataType() != null) { try { if (!protector.isEncryptedByCurrentKey(ps.getEncryptedDataType())) { ProtectedStringType reencrypted = protector.encryptString(protector.decryptString(ps)); ps.setEncryptedData(reencrypted.getEncryptedDataType()); increment(modCountHolder); } } catch (EncryptionException e) { throw new TunnelException(new EncryptionException("Failed to check/reencrypt value for field " + propName + ": " + e.getMessage(), e)); } } else { // no clear nor encrypted value } }
ProtectedStringType pstEnc = protector256.encryptString(value);