mirrored from https://www.bouncycastle.org/repositories/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathDumpUtil.java
More file actions
71 lines (64 loc) · 2.09 KB
/
DumpUtil.java
File metadata and controls
71 lines (64 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.bouncycastle.test;
import org.bouncycastle.util.Pack;
import org.bouncycastle.util.encoders.Hex;
public class DumpUtil
{
/**
* Return a formatted hex dump of the given byte array.
* @param array byte array
*/
public static String hexdump(byte[] array)
{
return hexdump(0, array);
}
/**
* Return a formatted hex dump of the given byte array.
* If startIndent is non-zero, the dump is shifted right by startIndent octets.
* @param startIndent shift the octet stream between by a number of bytes
* @param array byte array
*/
public static String hexdump(int startIndent, byte[] array)
{
if (startIndent < 0)
{
throw new IllegalArgumentException("Start-Indent must be a positive number");
}
if (array == null)
{
return "<null>";
}
// -DM Hex.toHexString
String hex = Hex.toHexString(array);
StringBuilder withWhiteSpace = new StringBuilder();
// shift the dump a number of octets to the right
for (int i = 0; i < startIndent; i++)
{
withWhiteSpace.append(" ");
}
// Split into hex octets (pairs of two chars)
String[] octets = withWhiteSpace.append(hex).toString().split("(?<=\\G.{2})");
StringBuilder out = new StringBuilder();
int l = 0;
byte[] counterLabel = new byte[4];
while (l < octets.length)
{
// index row
Pack.intToBigEndian(l, counterLabel, 0);
out.append(Hex.toHexString(counterLabel)).append(" ");
// first 8 octets of a line
for (int i = l ; i < l + 8 && i < octets.length; i++)
{
out.append(octets[i]).append(" ");
}
out.append(" ");
// second 8 octets of a line
for (int i = l+8; i < l + 16 && i < octets.length; i++)
{
out.append(octets[i]).append(" ");
}
out.append("\n");
l += 16;
}
return out.toString();
}
}