org.spongycastle.openpgp.PGPEncryptedDataList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pg Show documentation
Show all versions of pg Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.openpgp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.spongycastle.bcpg.BCPGInputStream;
import org.spongycastle.bcpg.InputStreamPacket;
import org.spongycastle.bcpg.PacketTags;
import org.spongycastle.bcpg.PublicKeyEncSessionPacket;
import org.spongycastle.bcpg.SymmetricKeyEncSessionPacket;
import org.spongycastle.util.Iterable;
/**
* A holder for a list of PGP encryption method packets and the encrypted data associated with them.
*
* This holder supports reading a sequence of the following encryption methods, followed by an
* encrypted data packet:
*
* - {@link PacketTags#SYMMETRIC_KEY_ENC_SESSION} - produces a {@link PGPPBEEncryptedData}
* - {@link PacketTags#PUBLIC_KEY_ENC_SESSION} - produces a {@link PGPPublicKeyEncryptedData}
*
*
* All of the objects returned from this holder share a reference to the same encrypted data input
* stream, which can only be consumed once.
*
*/
public class PGPEncryptedDataList
implements Iterable
{
List list = new ArrayList();
InputStreamPacket data;
/**
* Construct an encrypted data packet holder, reading PGP encrypted method packets and an
* encrytped data packet from the stream.
*
* The next packet in the stream should be one of {@link PacketTags#SYMMETRIC_KEY_ENC_SESSION}
* or {@link PacketTags#PUBLIC_KEY_ENC_SESSION}.
*
* @param pIn the PGP object stream being read.
* @throws IOException if an error occurs reading from the PGP input.
*/
public PGPEncryptedDataList(
BCPGInputStream pIn)
throws IOException
{
while (pIn.nextPacketTag() == PacketTags.PUBLIC_KEY_ENC_SESSION
|| pIn.nextPacketTag() == PacketTags.SYMMETRIC_KEY_ENC_SESSION)
{
list.add(pIn.readPacket());
}
data = (InputStreamPacket)pIn.readPacket();
for (int i = 0; i != list.size(); i++)
{
if (list.get(i) instanceof SymmetricKeyEncSessionPacket)
{
list.set(i, new PGPPBEEncryptedData((SymmetricKeyEncSessionPacket)list.get(i), data));
}
else
{
list.set(i, new PGPPublicKeyEncryptedData((PublicKeyEncSessionPacket)list.get(i), data));
}
}
}
/**
* Gets the encryption method object at the specified index.
*
* @param index the encryption method to obtain (0 based).
*/
public Object get(
int index)
{
return list.get(index);
}
/**
* Gets the number of encryption methods in this list.
*/
public int size()
{
return list.size();
}
/**
* Returns true
iff there are 0 encryption methods in this list.
*/
public boolean isEmpty()
{
return list.isEmpty();
}
/**
* Returns an iterator over the encryption method objects held in this list, in the order they
* appeared in the stream they are read from.
*/
public Iterator getEncryptedDataObjects()
{
return list.iterator();
}
/**
* Support method for Iterable where available.
*/
public Iterator iterator()
{
return getEncryptedDataObjects();
}
}