Код: Выделить всё
public class DSecretKey implements SecretKey, KeySpec {
final String algorithm;
final byte[] key;
final AtomicBoolean destroyed = new AtomicBoolean(false);
public DSecretKey(byte[] key, String algorithm) {
Asserts.notNull(key);
this.key = key.clone(); this.algorithm = algorithm;
}
@Override
public String getAlgorithm() { return this.algorithm; }
@Override
public String getFormat() { return "RAW"; }
@Override
public byte[] getEncoded() {
if (isDestroyed()) throw new IllegalStateException("The key is destroyed.");
return this.key.clone();
}
@Override
public boolean isDestroyed() { return this.destroyed.get(); }
@Override
public void destroy() throws DestroyFailedException {
if (!this.destroyed.compareAndSet(false, true)) {
CryptoUtils.clear(this.key);
}
}
}
Вопрос: Следует ли В этом случае я не использую AtomicBoolean, а вместо этого использую логический тип данных с синхронизированным, изменчивым или Java Lock? Какой метод лучше?
Примечание. Ключ можно использовать/повторно использовать одновременно.
Подробнее здесь: https://stackoverflow.com/questions/792 ... hread-safe
Мобильная версия