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

package com.google.crypto.tink.hybrid.subtle;

import com.google.crypto.tink.Aead;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import com.google.crypto.tink.subtle.Hkdf;
import java.security.PublicKey;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.aead.subtle.AeadFactory;
import java.security.interfaces.RSAPublicKey;
import com.google.crypto.tink.HybridEncrypt;

public final class RsaKemHybridEncrypt implements HybridEncrypt
{
    private final RSAPublicKey recipientPublicKey;
    private final String hkdfHmacAlgo;
    private final byte[] hkdfSalt;
    private final AeadFactory aeadFactory;
    
    public RsaKemHybridEncrypt(final RSAPublicKey recipientPublicKey, final String hkdfHmacAlgo, final byte[] hkdfSalt, final AeadFactory aeadFactory) throws GeneralSecurityException {
        RsaKem.validateRsaModulus(recipientPublicKey.getModulus());
        this.recipientPublicKey = recipientPublicKey;
        this.hkdfHmacAlgo = hkdfHmacAlgo;
        this.hkdfSalt = hkdfSalt;
        this.aeadFactory = aeadFactory;
    }
    
    @Override
    public byte[] encrypt(final byte[] plaintext, final byte[] contextInfo) throws GeneralSecurityException {
        final BigInteger mod = this.recipientPublicKey.getModulus();
        final byte[] sharedSecret = RsaKem.generateSecret(mod);
        final byte[] token = RsaKem.rsaEncrypt(this.recipientPublicKey, sharedSecret);
        final byte[] demKey = Hkdf.computeHkdf(this.hkdfHmacAlgo, sharedSecret, this.hkdfSalt, contextInfo, this.aeadFactory.getKeySizeInBytes());
        final Aead aead = this.aeadFactory.createAead(demKey);
        final byte[] ciphertext = aead.encrypt(plaintext, RsaKem.EMPTY_AAD);
        return ByteBuffer.allocate(token.length + ciphertext.length).put(token).put(ciphertext).array();
    }
}
