aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/utils
diff options
context:
space:
mode:
authornetconomy <netconomy@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2007-08-17 06:10:56 +0000
committernetconomy <netconomy@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2007-08-17 06:10:56 +0000
commit3d982813b34f6f230baf4a467cdc37ec92a77595 (patch)
tree85319d39cee2ded1bb7a2b2dd9e8ea37e3778248 /src/main/java/at/gv/egiz/pdfas/utils
parent07f6c8f33b2d700276fe6ec6339ff836c8710131 (diff)
downloadpdf-as-3-3d982813b34f6f230baf4a467cdc37ec92a77595.tar.gz
pdf-as-3-3d982813b34f6f230baf4a467cdc37ec92a77595.tar.bz2
pdf-as-3-3d982813b34f6f230baf4a467cdc37ec92a77595.zip
Performance
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@167 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/utils')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/utils/DataHashUtils.java136
-rw-r--r--src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java42
2 files changed, 178 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/DataHashUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/DataHashUtils.java
new file mode 100644
index 0000000..19d7b33
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/utils/DataHashUtils.java
@@ -0,0 +1,136 @@
+/**
+ *
+ */
+package at.gv.egiz.pdfas.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.codec.binary.Base64;
+
+import at.knowcenter.wag.egov.egiz.pdf.BinarySignatureHolder;
+import at.knowcenter.wag.egov.egiz.pdf.SignatureHolder;
+import at.knowcenter.wag.egov.egiz.pdf.TextualSignatureHolder;
+
+/**
+ * Contains helpful methods for building data hashed.
+ *
+ * <p>
+ * Data hashes are useful for summarizing the signed data of signatures for
+ * debugging and testing purposes. Do not use these hashes for signatures.
+ * </p>
+ * <p>
+ * A data hash is always a Base64 encoded String.
+ * </p>
+ *
+ * @author wprinz
+ *
+ */
+public final class DataHashUtils
+{
+
+ /**
+ *
+ * @param text
+ * @return
+ */
+ public static String buildDataHash(String text)
+ {
+ try
+ {
+ MessageDigest md = getMessageDigest();
+ // probable performance leak for very large texts
+ md.update(text.getBytes("UTF-8"));
+ byte[] rawDigest = md.digest();
+
+ return encodeDigest(rawDigest);
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static String buildDataHash(byte[] data)
+ {
+ MessageDigest md = getMessageDigest();
+ md.update(data);
+ byte[] rawDigest = md.digest();
+
+ return encodeDigest(rawDigest);
+ }
+
+ public static String buildDataHash(InputStream is)
+ {
+ try
+ {
+ MessageDigest md = getMessageDigest();
+
+ DigestInputStream dis = new DigestInputStream(is, md);
+
+ byte[] temp = new byte[1024];
+ int i = 0;
+ while (dis.read(temp) >= 0)
+ {
+ // this just keeps the compiler from optimizing this loop away
+ i++;
+ }
+ dis.close();
+
+ byte[] rawDigest = md.digest();
+
+ return encodeDigest(rawDigest);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static String buildDataHash(SignatureHolder sh)
+ {
+ if (sh instanceof TextualSignatureHolder)
+ {
+ TextualSignatureHolder tsh = (TextualSignatureHolder) sh;
+ String signedText = tsh.getSignedText();
+ return buildDataHash(signedText);
+ }
+
+ {
+ BinarySignatureHolder bsh = (BinarySignatureHolder) sh;
+ InputStream is = bsh.getSignedPdf().createInputStream();
+ return buildDataHash(is);
+ }
+ }
+
+ protected static MessageDigest getMessageDigest()
+ {
+ try
+ {
+ MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
+ return sha1;
+ }
+ catch (NoSuchAlgorithmException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected static String encodeDigest(byte[] rawDigest)
+ {
+ try
+ {
+ byte[] encoded = Base64.encodeBase64(rawDigest);
+ String str = new String(encoded, "US-ASCII");
+ return str;
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java
new file mode 100644
index 0000000..acf7e75
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/utils/StreamUtils.java
@@ -0,0 +1,42 @@
+package at.gv.egiz.pdfas.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class StreamUtils
+{
+
+ public static void writeInputStreamToOutputStream (InputStream is, OutputStream os) throws IOException
+ {
+ byte [] buffer = new byte [2048];
+
+ int read = -1;
+ while ((read = is.read(buffer)) > 0)
+ {
+ os.write(buffer, 0, read);
+ }
+ is.close();
+ os.close();
+ }
+
+ // The InputStream should be self- delimited.
+// public static void writeInputStreamToOutputStream(InputStream is, OutputStream os, int length) throws IOException
+// {
+// byte [] buffer = new byte [2048];
+//
+// int bytes_to_write = length;
+//
+// int read = -1;
+// while ((read = is.read(buffer, 0, (bytes_to_write >= buffer.length) ? buffer.length : bytes_to_write)) > 0)
+// {
+// os.write(buffer, 0, read);
+// bytes_to_write -= read;
+// }
+// is.close();
+// os.close();
+//
+// assert bytes_to_write == 0;
+// }
+
+}