1 package org.metricshub.winrm.service.client.encryption;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 import org.metricshub.winrm.Utils;
28
29
30
31
32
33 public class ByteArrayUtils {
34
35 private ByteArrayUtils() {}
36
37 private static final int WIDTH = 32;
38
39 public static String formatHexDump(final byte[] array) {
40 if (array == null) {
41 return "null";
42 }
43
44
45
46 final StringBuilder builder = new StringBuilder();
47
48 for (int rowOffset = 0; rowOffset < array.length; rowOffset += WIDTH) {
49 builder.append(String.format("%06d: ", rowOffset));
50
51 for (int index = 0; index < WIDTH; index++) {
52 if (rowOffset + index < array.length) {
53 builder.append(String.format("%02x", array[rowOffset + index]));
54 } else {
55 builder.append(" ");
56 }
57
58 if (index % 4 == 3) {
59 builder.append(" ");
60 }
61 }
62
63 if (rowOffset < array.length) {
64 builder.append(" | ");
65 for (int index = 0; index < WIDTH; index++) {
66 if (rowOffset + index < array.length) {
67 final byte c = array[rowOffset + index];
68 builder.append((c >= 20 && c < 127) ? (char) c : '.');
69
70 if (index % 8 == 7) builder.append(" ");
71 }
72 }
73 }
74
75 builder.append(Utils.NEW_LINE);
76 }
77
78 return builder.toString();
79 }
80
81 public static byte[] getLittleEndianUnsignedInt(final long x) {
82 final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
83 byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
84 byteBuffer.putInt((int) (x & 0xFFFFFFFF));
85 return byteBuffer.array();
86 }
87
88 public static long readLittleEndianUnsignedInt(final byte[] input, final int offset) {
89 final ByteBuffer byteBuffer = ByteBuffer.wrap(input);
90 byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
91 return Integer.toUnsignedLong(byteBuffer.getInt(offset));
92 }
93
94 public static byte[] concat(final byte[]... sequences) {
95 try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
96 for (byte[] s : sequences) {
97 out.write(s);
98 }
99 return out.toByteArray();
100 } catch (final IOException e) {
101 throw new IllegalStateException(e);
102 }
103 }
104 }