// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.kems;

import javax.security.auth.DestroyFailedException;
import org.bouncycastle.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import org.bouncycastle.crypto.SecretWithEncapsulation;

class SecretWithEncapsulationImpl implements SecretWithEncapsulation
{
    private final AtomicBoolean hasBeenDestroyed;
    private final byte[] sessionKey;
    private final byte[] cipher_text;
    
    public SecretWithEncapsulationImpl(final byte[] sessionKey, final byte[] cipher_text) {
        this.hasBeenDestroyed = new AtomicBoolean(false);
        this.sessionKey = sessionKey;
        this.cipher_text = cipher_text;
    }
    
    @Override
    public byte[] getSecret() {
        final byte[] clone = Arrays.clone(this.sessionKey);
        this.checkDestroyed();
        return clone;
    }
    
    @Override
    public byte[] getEncapsulation() {
        final byte[] clone = Arrays.clone(this.cipher_text);
        this.checkDestroyed();
        return clone;
    }
    
    @Override
    public void destroy() throws DestroyFailedException {
        if (!this.hasBeenDestroyed.getAndSet(true)) {
            Arrays.clear(this.sessionKey);
            Arrays.clear(this.cipher_text);
        }
    }
    
    @Override
    public boolean isDestroyed() {
        return this.hasBeenDestroyed.get();
    }
    
    void checkDestroyed() {
        if (this.isDestroyed()) {
            throw new IllegalStateException("data has been destroyed");
        }
    }
}
