-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataUtils.java
More file actions
131 lines (115 loc) · 4.54 KB
/
Copy pathDataUtils.java
File metadata and controls
131 lines (115 loc) · 4.54 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.Random;
public class DataUtils {
public static byte[] hexToBytes(String hex) {
hex = hex.replaceAll("\\s+", "").toLowerCase();
if (hex.length() % 2 != 0) hex = "0" + hex;
// every byte is represented by 2 hex chars
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
// parse hex characters, so we use base 16
bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
}
return bytes;
}
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
// 02x -> 0 padded format to be sure it is well represented in hex
// the & is used here to prevent getting a bad negative overflow, when doing & ff we make sure it is unsigned
for (byte b : bytes) sb.append(String.format("%02x", b & 0xff));
return sb.toString();
}
public static int[] bytesToBits(byte[] bytes) {
// every byte will be represented in 8 bits (8 items in this array)
int[] bits = new int[bytes.length * 8];
for (int i = 0; i < bytes.length; i++) {
for (int j = 0; j < 8; j++) {
// each bit in the one byte is:-
// shift by the bit index (7 - j)
// then & 1 to determine if the bit here is 1 or 0
bits[i * 8 + j] = (bytes[i] >> (7 - j)) & 1;
}
}
return bits;
}
public static byte[] bitsToBytes(int[] bits) {
int numBytes = (bits.length + 7) / 8;
byte[] bytes = new byte[numBytes];
for (int i = 0; i < bits.length; i++) {
// the byte is initially 00000000
// for every one bit we can put it in it's position by shifting by it's index
// example: 1 in index 7 (leftmost bit)
// shift 1 by 7 positions => 10000000
// then | with the byte
// 1 will be there
bytes[i / 8] = (byte) (bytes[i / 8] | (bits[i] << (7 - (i % 8))));
}
return bytes;
}
public static String bitsToHex(int[] bits) {
return bytesToHex(bitsToBytes(bits));
}
public static String bitsToBinaryString(int[] bits) {
// just normal auto casting so the integer is in the string as a char
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bits.length; i++) {
if (i > 0 && i % 8 == 0) sb.append(" ");
sb.append(bits[i]);
}
return sb.toString();
}
public static int[] hexToBits(String hex) {
return bytesToBits(hexToBytes(hex));
}
public static int[] stringToBits(String str) {
return bytesToBits(str.getBytes());
}
public static String bitsToString(int[] bits) {
return new String(bitsToBytes(bits));
}
public static int[] generateRandomKey() {
Random rnd = new Random();
byte[] keyBytes = new byte[8];
rnd.nextBytes(keyBytes);
return bytesToBits(keyBytes);
}
public static int[] padBytes(byte[] data) {
int blockSize = 8;
// the remaining bytes so it is % 8
int padLen = blockSize - (data.length % blockSize);
byte[] padded = new byte[data.length + padLen];
for (int i = 0; i < data.length; i++) padded[i] = data[i];
for (int i = data.length; i < padded.length; i++) {
// padding bytes will be = how much pad, so i can remove them
padded[i] = (byte) padLen;
}
return bytesToBits(padded);
}
public static byte[] unpadBytes(byte[] data) {
if (data.length == 0) return data;
int padLen = data[data.length - 1] & 0xff;
// no padding
if (padLen < 1 || padLen > 8) return data;
for (int i = data.length - padLen; i < data.length; i++) {
if ((data[i] & 0xff) != padLen) return data;
}
// remove pad
byte[] result = new byte[data.length - padLen];
for (int i = 0; i < result.length; i++) result[i] = data[i];
return result;
}
public static int[] hexToKeyBits(String hex) {
return hexToBits(hex);
}
public static int[] stringToKeyBits(String str) {
byte[] bytes = str.getBytes();
if (bytes.length > 8) {
byte[] truncated = new byte[8];
for (int i = 0; i < 8; i++) truncated[i] = bytes[i];
return bytesToBits(truncated);
}
return bytesToBits(bytes);
}
public static String keyBitsToString(int[] bits) {
return bitsToString(bits);
}
}