if (cache.containsKey(clazz.getDescribedClass().getName())) {
IEncryptionProvider provider = cache.get(clazz.getDescribedClass().getName()).get();
if (provider != null) return provider;
}
EncryptionConfig config = clazz.getAnnotation(EncryptionConfig.class);
byte[] key = null;
if (config != null && config.source() == Source.PASSWORD_LOCKER) {
if ("".equals( config.sourceName() )) throw new IllegalArgumentException("Password locker source name must be provider for: " + clazz);
if (PasswordContext.get() == null) throw new IllegalStateException("No password locker available for the current context");
IPassword pw = PasswordContext.get().getPassword(config.sourceName());
if (pw == null) throw new IllegalArgumentException("No such password in current context locker: " + config.sourceName());
key = pw.getPassword();
} else {
ClassProperty property = clazz.getProperty(EncryptionKey.class);
if (property == null) throw new IllegalStateException("No encryption key property available for: " + clazz);
key = ReflectionUtil.coerce(byte[].class, property.getValue(instance));
}
try {
IEncryptionProvider provider = new SimpleEncryptionProvider(SimpleEncryptionKey.createUsing(Strength.STRONG, config == null ? "AES" : config.algorithm(), key));
cache.put(clazz.getDescribedClass().getName(), new SoftReference<IEncryptionProvider>(provider));
return provider;
} catch (InvalidKeyException e) {
throw ThrowableManagerRegistry.caught(e);
}