org.testifyproject.bouncycastle.operator.bc.BcSymmetricKeyWrapper Maven / Gradle / Ivy
The newest version!
package org.testifyproject.bouncycastle.operator.bc;
import java.security.SecureRandom;
import org.testifyproject.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.testifyproject.bouncycastle.crypto.Wrapper;
import org.testifyproject.bouncycastle.crypto.params.KeyParameter;
import org.testifyproject.bouncycastle.crypto.params.ParametersWithRandom;
import org.testifyproject.bouncycastle.operator.GenericKey;
import org.testifyproject.bouncycastle.operator.OperatorException;
import org.testifyproject.bouncycastle.operator.SymmetricKeyWrapper;
public class BcSymmetricKeyWrapper
extends SymmetricKeyWrapper
{
private SecureRandom random;
private Wrapper wrapper;
private KeyParameter wrappingKey;
public BcSymmetricKeyWrapper(AlgorithmIdentifier wrappingAlgorithm, Wrapper wrapper, KeyParameter wrappingKey)
{
super(wrappingAlgorithm);
this.wrapper = wrapper;
this.wrappingKey = wrappingKey;
}
public BcSymmetricKeyWrapper setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public byte[] generateWrappedKey(GenericKey encryptionKey)
throws OperatorException
{
byte[] contentEncryptionKeySpec = OperatorUtils.getKeyBytes(encryptionKey);
if (random == null)
{
wrapper.init(true, wrappingKey);
}
else
{
wrapper.init(true, new ParametersWithRandom(wrappingKey, random));
}
return wrapper.wrap(contentEncryptionKeySpec, 0, contentEncryptionKeySpec.length);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy