Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Tyley <roberto.tyley@gmail.com>2014-07-15 01:38:01 +0400
committerRoberto Tyley <roberto.tyley@gmail.com>2014-07-26 11:23:17 +0400
commit7cb752aaf746dc0b473afeb9e892b7fbc12666c5 (patch)
treecc4f91ddc18332b5adbe82e3fcb040d976c90105 /pg/src/main/java/org/spongycastle/openpgp/PGPOnePassSignature.java
parent551830f8ea5177042af2c7dd1fc90888bc67387d (diff)
Execute become-spongy.sh
https://github.com/rtyley/spongycastle/blob/3040af/become-spongy.sh
Diffstat (limited to 'pg/src/main/java/org/spongycastle/openpgp/PGPOnePassSignature.java')
-rw-r--r--pg/src/main/java/org/spongycastle/openpgp/PGPOnePassSignature.java222
1 files changed, 222 insertions, 0 deletions
diff --git a/pg/src/main/java/org/spongycastle/openpgp/PGPOnePassSignature.java b/pg/src/main/java/org/spongycastle/openpgp/PGPOnePassSignature.java
new file mode 100644
index 00000000..8e1dbcf6
--- /dev/null
+++ b/pg/src/main/java/org/spongycastle/openpgp/PGPOnePassSignature.java
@@ -0,0 +1,222 @@
+package org.spongycastle.openpgp;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.spongycastle.bcpg.BCPGInputStream;
+import org.spongycastle.bcpg.BCPGOutputStream;
+import org.spongycastle.bcpg.OnePassSignaturePacket;
+import org.spongycastle.openpgp.operator.PGPContentVerifier;
+import org.spongycastle.openpgp.operator.PGPContentVerifierBuilder;
+import org.spongycastle.openpgp.operator.PGPContentVerifierBuilderProvider;
+
+/**
+ * A one pass signature object.
+ */
+public class PGPOnePassSignature
+{
+ private OnePassSignaturePacket sigPack;
+ private int signatureType;
+
+ private PGPContentVerifier verifier;
+ private byte lastb;
+ private OutputStream sigOut;
+
+ PGPOnePassSignature(
+ BCPGInputStream pIn)
+ throws IOException, PGPException
+ {
+ this((OnePassSignaturePacket)pIn.readPacket());
+ }
+
+ PGPOnePassSignature(
+ OnePassSignaturePacket sigPack)
+ throws PGPException
+ {
+ this.sigPack = sigPack;
+ this.signatureType = sigPack.getSignatureType();
+ }
+
+ /**
+ * Initialise the signature object for verification.
+ *
+ * @param verifierBuilderProvider provider for a content verifier builder for the signature type of interest.
+ * @param pubKey the public key to use for verification
+ * @throws PGPException if there's an issue with creating the verifier.
+ */
+ public void init(PGPContentVerifierBuilderProvider verifierBuilderProvider, PGPPublicKey pubKey)
+ throws PGPException
+ {
+ PGPContentVerifierBuilder verifierBuilder = verifierBuilderProvider.get(sigPack.getKeyAlgorithm(), sigPack.getHashAlgorithm());
+
+ verifier = verifierBuilder.build(pubKey);
+
+ lastb = 0;
+ sigOut = verifier.getOutputStream();
+ }
+
+ public void update(
+ byte b)
+ {
+ if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT)
+ {
+ if (b == '\r')
+ {
+ byteUpdate((byte)'\r');
+ byteUpdate((byte)'\n');
+ }
+ else if (b == '\n')
+ {
+ if (lastb != '\r')
+ {
+ byteUpdate((byte)'\r');
+ byteUpdate((byte)'\n');
+ }
+ }
+ else
+ {
+ byteUpdate(b);
+ }
+
+ lastb = b;
+ }
+ else
+ {
+ byteUpdate(b);
+ }
+ }
+
+ public void update(
+ byte[] bytes)
+ {
+ if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT)
+ {
+ for (int i = 0; i != bytes.length; i++)
+ {
+ this.update(bytes[i]);
+ }
+ }
+ else
+ {
+ blockUpdate(bytes, 0, bytes.length);
+ }
+ }
+
+ public void update(
+ byte[] bytes,
+ int off,
+ int length)
+ {
+ if (signatureType == PGPSignature.CANONICAL_TEXT_DOCUMENT)
+ {
+ int finish = off + length;
+
+ for (int i = off; i != finish; i++)
+ {
+ this.update(bytes[i]);
+ }
+ }
+ else
+ {
+ blockUpdate(bytes, off, length);
+ }
+ }
+
+ private void byteUpdate(byte b)
+ {
+ try
+ {
+ sigOut.write(b);
+ }
+ catch (IOException e)
+ {
+ throw new PGPRuntimeOperationException(e.getMessage(), e);
+ }
+ }
+
+ private void blockUpdate(byte[] block, int off, int len)
+ {
+ try
+ {
+ sigOut.write(block, off, len);
+ }
+ catch (IOException e)
+ {
+ throw new PGPRuntimeOperationException(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Verify the calculated signature against the passed in PGPSignature.
+ *
+ * @param pgpSig
+ * @return boolean
+ * @throws PGPException
+ */
+ public boolean verify(
+ PGPSignature pgpSig)
+ throws PGPException
+ {
+ try
+ {
+ sigOut.write(pgpSig.getSignatureTrailer());
+
+ sigOut.close();
+ }
+ catch (IOException e)
+ {
+ throw new PGPException("unable to add trailer: " + e.getMessage(), e);
+ }
+
+ return verifier.verify(pgpSig.getSignature());
+ }
+
+ public long getKeyID()
+ {
+ return sigPack.getKeyID();
+ }
+
+ public int getSignatureType()
+ {
+ return sigPack.getSignatureType();
+ }
+
+ public int getHashAlgorithm()
+ {
+ return sigPack.getHashAlgorithm();
+ }
+
+ public int getKeyAlgorithm()
+ {
+ return sigPack.getKeyAlgorithm();
+ }
+
+ public byte[] getEncoded()
+ throws IOException
+ {
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream();
+
+ this.encode(bOut);
+
+ return bOut.toByteArray();
+ }
+
+ public void encode(
+ OutputStream outStream)
+ throws IOException
+ {
+ BCPGOutputStream out;
+
+ if (outStream instanceof BCPGOutputStream)
+ {
+ out = (BCPGOutputStream)outStream;
+ }
+ else
+ {
+ out = new BCPGOutputStream(outStream);
+ }
+
+ out.writePacket(sigPack);
+ }
+}