PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/extern/spongycastle/pkix/src/main/java/org/spongycastle/tsp/cms/CMSTimeStampedDataGenerator.java

https://gitlab.com/vizilo/fdroidclient
Java | 70 lines | 57 code | 13 blank | 0 comment | 6 complexity | daf673e7f023751f1c962b4b8e60e754 MD5 | raw file
 1package org.spongycastle.tsp.cms;
 2
 3import java.io.ByteArrayInputStream;
 4import java.io.ByteArrayOutputStream;
 5import java.io.IOException;
 6import java.io.InputStream;
 7
 8import org.spongycastle.asn1.ASN1OctetString;
 9import org.spongycastle.asn1.BEROctetString;
10import org.spongycastle.asn1.DERIA5String;
11import org.spongycastle.asn1.cms.CMSObjectIdentifiers;
12import org.spongycastle.asn1.cms.ContentInfo;
13import org.spongycastle.asn1.cms.Evidence;
14import org.spongycastle.asn1.cms.TimeStampAndCRL;
15import org.spongycastle.asn1.cms.TimeStampTokenEvidence;
16import org.spongycastle.asn1.cms.TimeStampedData;
17import org.spongycastle.cms.CMSException;
18import org.spongycastle.tsp.TimeStampToken;
19import org.spongycastle.util.io.Streams;
20
21public class CMSTimeStampedDataGenerator
22    extends CMSTimeStampedGenerator
23{
24    public CMSTimeStampedData generate(TimeStampToken timeStamp) throws CMSException
25    {
26        return generate(timeStamp, (InputStream)null);
27    }
28
29    public CMSTimeStampedData generate(TimeStampToken timeStamp, byte[] content) throws CMSException
30    {
31        return generate(timeStamp, new ByteArrayInputStream(content));
32    }
33
34    public CMSTimeStampedData generate(TimeStampToken timeStamp, InputStream content)
35        throws CMSException
36    {
37        ByteArrayOutputStream contentOut = new ByteArrayOutputStream();
38
39        if (content != null)
40        {
41            try
42            {
43                Streams.pipeAll(content, contentOut);
44            }
45            catch (IOException e)
46            {
47                throw new CMSException("exception encapsulating content: " + e.getMessage(), e);
48            }
49        }
50
51        ASN1OctetString encContent = null;
52
53        if (contentOut.size() != 0)
54        {
55            encContent = new BEROctetString(contentOut.toByteArray());
56        }
57
58        TimeStampAndCRL stamp = new TimeStampAndCRL(timeStamp.toCMSSignedData().toASN1Structure());
59
60        DERIA5String asn1DataUri = null;
61
62        if (dataUri != null)
63        {
64            asn1DataUri = new DERIA5String(dataUri.toString());
65        }
66        
67        return new CMSTimeStampedData(new ContentInfo(CMSObjectIdentifiers.timestampedData, new TimeStampedData(asn1DataUri, metaData, encContent, new Evidence(new TimeStampTokenEvidence(stamp)))));
68    }
69}
70