All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gatling.recorder.internal.bouncycastle.cms.CMSEncryptedDataGenerator Maven / Gradle / Ivy

package io.gatling.recorder.internal.bouncycastle.cms;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import io.gatling.recorder.internal.bouncycastle.asn1.ASN1OctetString;
import io.gatling.recorder.internal.bouncycastle.asn1.ASN1Set;
import io.gatling.recorder.internal.bouncycastle.asn1.BEROctetString;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.ContentInfo;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.EncryptedContentInfo;
import io.gatling.recorder.internal.bouncycastle.asn1.cms.EncryptedData;
import io.gatling.recorder.internal.bouncycastle.asn1.x509.AlgorithmIdentifier;
import io.gatling.recorder.internal.bouncycastle.operator.OutputEncryptor;

/**
 * General class for generating a CMS encrypted-data message.
 *
 * A simple example of usage.
 *
 * 
 *       CMSTypedData msg     = new CMSProcessableByteArray("Hello World!".getBytes());
 *
 *       CMSEncryptedDataGenerator edGen = new CMSEncryptedDataGenerator();
 *
 *       CMSEncryptedData ed = edGen.generate(
 *                                       msg,
 *                                       new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC)
 *                                              .setProvider("BC").build());
 *
 * 
*/ public class CMSEncryptedDataGenerator extends CMSEncryptedGenerator { /** * base constructor */ public CMSEncryptedDataGenerator() { } private CMSEncryptedData doGenerate( CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException { AlgorithmIdentifier encAlgId; ASN1OctetString encContent; ByteArrayOutputStream bOut = new ByteArrayOutputStream(); try { OutputStream cOut = contentEncryptor.getOutputStream(bOut); content.write(cOut); cOut.close(); } catch (IOException e) { throw new CMSException(""); } byte[] encryptedContent = bOut.toByteArray(); encAlgId = contentEncryptor.getAlgorithmIdentifier(); encContent = new BEROctetString(encryptedContent); EncryptedContentInfo eci = CMSUtils.getEncryptedContentInfo( content.getContentType(), encAlgId, encryptedContent); ASN1Set unprotectedAttrSet = CMSUtils.getAttrBERSet(unprotectedAttributeGenerator); ContentInfo contentInfo = new ContentInfo( CMSObjectIdentifiers.encryptedData, new EncryptedData(eci, unprotectedAttrSet)); return new CMSEncryptedData(contentInfo); } /** * generate an encrypted object that contains an CMS Encrypted Data structure. * * @param content the content to be encrypted * @param contentEncryptor the symmetric key based encryptor to encrypt the content with. */ public CMSEncryptedData generate( CMSTypedData content, OutputEncryptor contentEncryptor) throws CMSException { return doGenerate(content, contentEncryptor); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy