/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ 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. * *

* Data hashes are useful for summarizing the signed data of signatures for * debugging and testing purposes. Do not use these hashes for signatures. *

*

* A data hash is always a Base64 encoded String. *

* * @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); } } }