View Javadoc
1   package org.metricshub.winrm.service.client.encryption;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * WinRM Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 - 2024 Metricshub
8    * ჻჻჻჻჻჻
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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   * Code from io.cloudsoft.winrm4j.client.encryption.ByteArrayUtils
31   * release 0.12.3 @link https://github.com/cloudsoft/winrm4j
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  		// from https://gist.github.com/jen20/906db194bd97c14d91df
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 }