|
| 1 | +package org.bouncycastle.bcpg; |
| 2 | + |
| 3 | +import org.bouncycastle.util.encoders.Hex; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | + |
| 7 | +public class HexDumpUtil { |
| 8 | + |
| 9 | + /** |
| 10 | + * Return a formatted hex dump of the given byte array. |
| 11 | + * @param array byte array |
| 12 | + */ |
| 13 | + public static String hexdump(byte[] array) { |
| 14 | + return hexdump(0, array); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Return a formatted hex dump of the given byte array. |
| 19 | + * If startIndent is non-zero, the dump is shifted right by startIndent octets. |
| 20 | + * @param startIndent shift the octet stream between by a number of bytes |
| 21 | + * @param array byte array |
| 22 | + */ |
| 23 | + public static String hexdump(int startIndent, byte[] array) { |
| 24 | + if (startIndent < 0) { |
| 25 | + throw new IllegalArgumentException("Start-Indent must be a positive number"); |
| 26 | + } |
| 27 | + if (array == null) { |
| 28 | + return "<null>"; |
| 29 | + } |
| 30 | + String hex = Hex.toHexString(array); |
| 31 | + StringBuilder withWhiteSpace = new StringBuilder(); |
| 32 | + // shift the dump a number of octets to the right |
| 33 | + for (int i = 0; i < startIndent; i++) { |
| 34 | + withWhiteSpace.append(" "); |
| 35 | + } |
| 36 | + // Split into hex octets (pairs of two chars) |
| 37 | + String[] octets = withWhiteSpace.append(hex).toString().split("(?<=\\G.{2})"); |
| 38 | + |
| 39 | + StringBuilder out = new StringBuilder(); |
| 40 | + int l = 0; |
| 41 | + while (l < octets.length) { |
| 42 | + // index row |
| 43 | + out.append(String.format("%08X", l)).append(" "); |
| 44 | + // first 8 octets of a line |
| 45 | + for (int i = l ; i < l + 8 && i < octets.length; i++) { |
| 46 | + out.append(octets[i]).append(" "); |
| 47 | + } |
| 48 | + out.append(" "); |
| 49 | + // second 8 octets of a line |
| 50 | + for (int i = l+8; i < l + 16 && i < octets.length; i++) { |
| 51 | + out.append(octets[i]).append(" "); |
| 52 | + } |
| 53 | + out.append("\n"); |
| 54 | + |
| 55 | + l += 16; |
| 56 | + } |
| 57 | + return out.toString(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Return a formatted hex dump of the packet encoding of the given packet. |
| 62 | + * @param packet packet |
| 63 | + * @return formatted hex dump |
| 64 | + * @throws IOException if an exception happens during packet encoding |
| 65 | + */ |
| 66 | + public static String hexdump(ContainedPacket packet) |
| 67 | + throws IOException { |
| 68 | + return hexdump(packet.getEncoded()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Return a formatted hex dump of the packet encoding of the given packet. |
| 73 | + * If startIndent is non-zero, the hex dump is shifted right by the startIndent octets. |
| 74 | + * @param startIndent shift the encodings octet stream by a number of bytes |
| 75 | + * @param packet packet |
| 76 | + * @return formatted hex dump |
| 77 | + * @throws IOException if an exception happens during packet encoding |
| 78 | + */ |
| 79 | + public static String hexdump(int startIndent, ContainedPacket packet) |
| 80 | + throws IOException { |
| 81 | + return hexdump(startIndent, packet.getEncoded()); |
| 82 | + } |
| 83 | +} |
0 commit comments