/** * Embeds the new signature into the document, by copying the rest of the document * * @param docBytes byte array of the document * @param output target, where the file will be written * @throws IOException */ private void embedNewSignatureIntoDocument(byte[] docBytes, OutputStream output) throws IOException { int[] byteRange = signature.getByteRange(); output.write(docBytes, byteRange[0], byteRange[1] + 1); output.write(changedEncodedSignature); int addingLength = byteRange[2] - byteRange[1] - 2 - changedEncodedSignature.length; byte[] zeroes = Hex.getBytes(new byte[(addingLength + 1) / 2]); output.write(zeroes); output.write(docBytes, byteRange[2] - 1, byteRange[3] + 1); }
/** * This will take this string and create a hex representation of the bytes that make the string. * * @return A hex string representing the bytes in this string. */ public String toHexString() { return Hex.getString(bytes); }
try decodedAppearanceXML = Hex.decodeBase64(base64EncodedAppearance);
/** * Returns the bytes corresponding to the ASCII hex encoding of the given byte. */ public static byte[] getBytes(byte b) { return new byte[]{HEX_BYTES[getHighNibble(b)], HEX_BYTES[getLowNibble(b)]}; }
writer.write(Hex.getChars(srcFrom.get(index).shortValue())); writer.write("> "); writer.write(Hex.getChars(srcTo.get(index).shortValue())); writer.write("> "); writer.write(Hex.getCharsUTF16BE(dstString.get(index))); writer.write(">\n");
/** * Writes the given byte array as hex value to the given output stream. * @param bytes the byte array to be written * @param output the output stream to be written to * @throws IOException exception if anything went wrong */ public static void writeHexBytes(byte[] bytes, OutputStream output) throws IOException { for (byte b : bytes) { writeHexByte(b, output); } }
Hex.writeHexBytes(bytes, output); output.write('>');
/** * Returns the bytes corresponding to the ASCII hex encoding of the given bytes. */ public static byte[] getBytes(byte[] bytes) { byte[] asciiBytes = new byte[bytes.length*2]; for(int i=0; i< bytes.length; i++) { asciiBytes[i*2] = HEX_BYTES[getHighNibble(bytes[i])]; asciiBytes[i*2+1] = HEX_BYTES[getLowNibble(bytes[i])]; } return asciiBytes; }
writer.write(Hex.getChars(srcFrom.get(index).shortValue())); writer.write("> "); writer.write(Hex.getChars(srcTo.get(index).shortValue())); writer.write("> "); writer.write(Hex.getCharsUTF16BE(dstString.get(index))); writer.write(">\n");
@Override public void encode(InputStream input, OutputStream encoded, COSDictionary parameters) throws IOException { int byteRead; while ((byteRead = input.read()) != -1) { Hex.writeHexByte((byte)byteRead, encoded); } encoded.flush(); } }
Hex.writeHexBytes(bytes, output); output.write('>');
/** * Returns a hex string of the given byte. */ public static String getString(byte b) { char[] chars = new char[]{HEX_CHARS[getHighNibble(b)], HEX_CHARS[getLowNibble(b)]}; return new String(chars); }
/** * Write externally created signature of PDF data obtained via {@link #getDataToSign()} method. * * @param cmsSignature CMS signature byte array * @throws IllegalStateException if PDF is not prepared for external signing * @throws IOException if source data stream is closed */ public void writeExternalSignature(byte[] cmsSignature) throws IOException { if (incrementPart == null || incrementalInput == null) { throw new IllegalStateException("PDF not prepared for setting signature"); } byte[] signatureBytes = Hex.getBytes(cmsSignature); // substract 2 bytes because of the enclosing "<>" if (signatureBytes.length > signatureLength - 2) { throw new IOException("Can't write signature, not enough space"); } // overwrite the signature Contents in the buffer int incPartSigOffset = (int) (signatureOffset - incrementalInput.length()); System.arraycopy(signatureBytes, 0, incrementPart, incPartSigOffset + 1, signatureBytes.length); // write the data to the incremental output stream IOUtils.copy(new RandomAccessInputStream(incrementalInput), incrementalOutput); incrementalOutput.write(incrementPart); // prevent further use incrementPart = null; }
/** * Gets the SHA-1-Hash has of given byte[]-content. * * @param content to be hashed * @return SHA-1 hash String */ protected static String getSha1Hash(byte[] content) { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); return Hex.getString(md.digest(content)); } catch (NoSuchAlgorithmException e) { LOG.error("No SHA-1 Algorithm found", e); } return null; }
writer.write(Hex.getChars(srcFrom.get(index).shortValue())); writer.write("> "); writer.write(Hex.getChars(srcTo.get(index).shortValue())); writer.write("> "); writer.write(Hex.getCharsUTF16BE(dstString.get(index))); writer.write(">\n");
Hex.writeHexByte(b, output);
Hex.writeHexBytes(bytes, output); output.write('>');
try binImage = Hex.decodeBase64(tb.getImage());
/** * Writes the given byte as hex value to the given output stream. * @param b the byte to be written * @param output the output stream to be written to * @throws IOException exception if anything went wrong */ public static void writeHexByte(byte b, OutputStream output) throws IOException { output.write(HEX_BYTES[getHighNibble(b)]); output.write(HEX_BYTES[getLowNibble(b)]); }
byte[] newEncoded = Hex.getBytes(signedData.getEncoded()); int maxSize = signature.getByteRange()[2] - signature.getByteRange()[1]; System.out.println(