-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtils.java
More file actions
496 lines (456 loc) · 17.7 KB
/
Utils.java
File metadata and controls
496 lines (456 loc) · 17.7 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
* Copyright 2011 Google Inc.
* Copyright 2013 Ronald W Hoffman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package JavaBitcoin;
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Static utility methods
*/
public class Utils {
/** Constant -1 */
public static final BigInteger NEGATIVE_ONE = BigInteger.valueOf(-1);
/** Constant 1,000 */
private static final BigInteger DISPLAY_1K = new BigInteger("1000");
/** Constant 1,000,000 */
private static final BigInteger DISPLAY_1M = new BigInteger("1000000");
/** Constant 1,000,000,000 */
private static final BigInteger DISPLAY_1G = new BigInteger("1000000000");
/** Constant 1,000,000,000,000 */
private static final BigInteger DISPLAY_1T = new BigInteger("1000000000000");
/** Constant 1,000,000,000,000,000 */
private static final BigInteger DISPLAY_1P = new BigInteger("1000000000000000");
/** Bit masks (Low-order bit is bit 0 and high-order bit is bit 7) */
private static final int bitMask[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
/** Instance of a SHA-256 digest which we will use as needed */
private static final MessageDigest digest;
static {
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Can't happen.
}
}
/**
* How many "nanocoins" there are in a Bitcoin.
*
* A nanocoin is the smallest unit that can be transferred using Bitcoin.
* The term nanocoin is very misleading, though, because there are only 100 million
* of them in a coin (whereas one would expect 1 billion.
*/
public static final BigInteger COIN = new BigInteger("100000000", 10);
/**
* How many "nanocoins" there are in 0.01 BitCoins.
*
* A nanocoin is the smallest unit that can be transferred using Bitcoin.
* The term nanocoin is very misleading, though, because there are only 100 million
* of them in a coin (whereas one would expect 1 billion).
*/
public static final BigInteger CENT = new BigInteger("1000000", 10);
/**
* Calculate the SHA-256 hash of the input
*
* @param input Data to be hashed
* @return The hash digest
*/
public static byte[] singleDigest(byte[] input) {
return singleDigest(input, 0, input.length);
}
/**
* Calculate the SHA-256 hash of the input
*
* @param input Data to be hashed
* @param offset Starting offset within the data
* @param length Number of bytes to hash
* @return The hash digest
*/
public static byte[] singleDigest(byte[] input, int offset, int length) {
synchronized (digest) {
digest.reset();
digest.update(input, offset, length);
return digest.digest();
}
}
/**
* Calculate the SHA-256 hash of the input and then hash the resulting hash again
*
* @param input Data to be hashed
* @return The hash digest
*/
public static byte[] doubleDigest(byte[] input) {
return doubleDigest(input, 0, input.length);
}
/**
* Calculate the SHA-256 hash of the input and then hash the resulting hash again
*
* @param input Data to be hashed
* @param offset Starting offset within the data
* @param length Number of data bytes to hash
* @return The hash digest
*/
public static byte[] doubleDigest(byte[] input, int offset, int length) {
synchronized (digest) {
digest.reset();
digest.update(input, offset, length);
byte[] first = digest.digest();
return digest.digest(first);
}
}
/**
* Calculate SHA256(SHA256(byte range 1 + byte range 2)).
*
* @param input1 First input byte array
* @param offset1 Starting position in the first array
* @param length1 Number of bytes to process in the first array
* @param input2 Second input byte array
* @param offset2 Starting position in the second array
* @param length2 Number of bytes to process in the second array
* @return The SHA-256 digest
*/
public static byte[] doubleDigestTwoBuffers(byte[]input1, int offset1, int length1,
byte[]input2, int offset2, int length2) {
synchronized (digest) {
digest.reset();
digest.update(input1, offset1, length1);
digest.update(input2, offset2, length2);
byte[]first = digest.digest();
return digest.digest(first);
}
}
/**
* Return the given byte array encoded as a hex string
*
* @param bytes The data to be encoded
* @return The encoded string
*/
public static String bytesToHexString(byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length*2);
for (byte b : bytes) {
String s = Integer.toString(0xFF&b, 16);
if (s.length() < 2)
buf.append('0');
buf.append(s);
}
return buf.toString();
}
/**
* Returns a string representing the shortened numeric value. For example,
* the value 1,500,000 will be returned as 1.500M.
*
* @param number The number to be displayed
* @return Display string
*/
public static String numberToShortString(BigInteger number) {
int scale;
String suffix;
BigDecimal work;
if (number.compareTo(DISPLAY_1P) >= 0) {
scale = 15;
suffix = "P";
} else if (number.compareTo(DISPLAY_1T) >= 0) {
scale = 12;
suffix = "T";
} else if (number.compareTo(DISPLAY_1G) >= 0) {
scale = 9;
suffix = "G";
} else if (number.compareTo(DISPLAY_1M) >= 0) {
scale = 6;
suffix = "M";
} else if (number.compareTo(DISPLAY_1K) >= 0) {
scale = 3;
suffix = "K";
} else {
scale = 0;
suffix = "";
}
if (scale != 0)
work = new BigDecimal(number, scale);
else
work = new BigDecimal(number);
return String.format("%3.3f%s", work.floatValue(), suffix);
}
/**
* Checks if the specified bit is set
*
* @param data Byte array to check
* @param index Bit position
* @return TRUE if the bit is set
*/
public static boolean checkBitLE(byte[] data, int index) {
return (data[index>>>3] & bitMask[7&index]) != 0;
}
/**
* Sets the specified bit
* @param data Byte array
* @param index Bit position
*/
public static void setBitLE(byte[] data, int index) {
data[index>>>3] |= bitMask[7&index];
}
/**
* The representation of nBits uses another home-brew encoding, as a way to represent a large
* hash value in only 32 bits.
*
* @param compact The compact bit representation
* @return The decoded result
*/
public static BigInteger decodeCompactBits(long compact) {
int size = ((int)(compact>>24)) & 0xFF;
byte[] bytes = new byte[4 + size];
bytes[3] = (byte)size;
if (size>=1) bytes[4] = (byte)((compact>>16) & 0xFF);
if (size>=2) bytes[5] = (byte)((compact>>8) & 0xFF);
if (size>=3) bytes[6] = (byte)(compact & 0xFF);
return decodeMPI(bytes, true);
}
/**
* MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of
* a 4 byte big-endian length field, followed by the stated number of bytes representing
* the number in big-endian format (with a sign bit).
*
* @param mpi Encoded byte array
* @param hasLength FALSE if the given array is missing the 4-byte length field
* @return Decoded value
*/
public static BigInteger decodeMPI(byte[] mpi, boolean hasLength) {
byte[] buf;
if (hasLength) {
int length = (int)readUint32BE(mpi, 0);
buf = new byte[length];
System.arraycopy(mpi, 4, buf, 0, length);
} else {
buf = mpi;
}
if (buf.length == 0)
return BigInteger.ZERO;
boolean isNegative = (buf[0] & 0x80) == 0x80;
if (isNegative)
buf[0] &= 0x7f;
BigInteger result = new BigInteger(buf);
return isNegative ? result.negate() : result;
}
/**
* Returns a copy of the given byte array in reverse order.
*
* @param bytes Array to be reversed
* @return New byte array in reverse order
*/
public static byte[] reverseBytes(byte[] bytes) {
byte[] buf = new byte[bytes.length];
for (int i=0; i<bytes.length; i++)
buf[i] = bytes[bytes.length-1-i];
return buf;
}
/**
* Returns a copy of the given byte array in reverse order
*
* @param bytes Array to be reversed
* @param offset Starting offset in the array
* @param length Number of bytes to reverse
* @return New byte array in reverse order
*/
public static byte[] reverseBytes(byte[] bytes, int offset, int length) {
byte[] buf = new byte[length];
for (int i=0; i<length; i++)
buf[i] = bytes[offset+length-1-i];
return buf;
}
/**
* Returns a copy of the given byte array with the bytes of each double-word (4 bytes) reversed.
*
* @param bytes Bytes to reverse (length must be divisible by 4)
* @param trimLength Trim output to this length (If positive, must be divisible by 4)
* @return Reversed bytes
*/
public static byte[] reverseDwordBytes(byte[] bytes, int trimLength) {
byte[] rev = new byte[trimLength >= 0 && bytes.length > trimLength ? trimLength : bytes.length];
for (int i = 0; i < rev.length; i += 4) {
System.arraycopy(bytes, i, rev, i , 4);
for (int j = 0; j < 4; j++) {
rev[i + j] = bytes[i + 3 - j];
}
}
return rev;
}
/**
* Form an integer value from an 2-byte array in big-endian format
*
* @param bytes The byte array
* @param offset Starting offset within the array
* @return The decoded value
*/
public static int readUint16BE(byte[] bytes, int offset) {
return (((int)bytes[offset++]&0x00ff) << 8) |
((int)bytes[offset]&0x00ff);
}
/**
* Form a long value from a 4-byte array in little-endian format
*
* @param bytes The byte array
* @param offset Starting offset within the array
* @return The decoded value
*/
public static long readUint32LE(byte[] bytes, int offset) {
return ((long)bytes[offset++]&0x00FFL) |
(((long)bytes[offset++]&0x00FFL) << 8) |
(((long)bytes[offset++]&0x00FFL) << 16) |
(((long)bytes[offset]&0x00FFL) << 24);
}
/**
* Form a long value from a 4-byte array in big-endian format
*
* @param bytes The byte array
* @param offset Starting offset within the array
* @return The long value
*/
public static long readUint32BE(byte[] bytes, int offset) {
return (((long)bytes[offset++]&0x00FFL) << 24) |
(((long)bytes[offset++]&0x00FFL) << 16) |
(((long)bytes[offset++]&0x00FFL) << 8) |
((long)bytes[offset]&0x00FFL);
}
/**
* Write an unsigned 32-bit value to a byte array in little-endian format
*
* @param val Value to be written
* @param out Output array
* @param offset Starting offset
*/
public static void uint32ToByteArrayLE(long val, byte[] out, int offset) {
out[offset++] = (byte)val;
out[offset++] = (byte)(val >> 8);
out[offset++] = (byte)(val >> 16);
out[offset] = (byte)(val >> 24);
}
/**
* Write an unsigned 32-bit value to a byte array in big-endian format
*
* @param val Value to be written
* @param out Output array
* @param offset Starting offset
*/
public static void uint32ToByteArrayBE(long val, byte[] out, int offset) {
out[offset++] = (byte)(val>>24);
out[offset++] = (byte)(val>>16);
out[offset++] = (byte)(val>>8);
out[offset] = (byte)val;
}
/**
* Write an unsigned 32-bit value to an output stream in little-endian format
*
* @param val Value to be written
* @param stream Output stream
*
* @throws IOException
*/
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException {
stream.write((int)(0x00FF&val));
stream.write((int)(0x00FF&(val >> 8)));
stream.write((int)(0x00FF&(val >> 16)));
stream.write((int)(0x00FF&(val >> 24)));
}
/**
* Form a long value from an 8-byte array in little-endian format
*
* @param bytes The byte array
* @param offset Starting offset within the array
* @return The long value
*/
public static long readUint64LE(byte[] bytes, int offset) {
return ((long)bytes[offset++]&0x00FFL) |
(((long)bytes[offset++]&0x00FFL) << 8) |
(((long)bytes[offset++]&0x00FFL) << 16) |
(((long)bytes[offset++]&0x00FFL) << 24) |
(((long)bytes[offset++]&0x00FFL) << 32) |
(((long)bytes[offset++]&0x00FFL) << 40) |
(((long)bytes[offset++]&0x00FFL) << 48) |
(((long)bytes[offset]&0x00FFL) << 56);
}
/**
* Write an unsigned 64-bit value to a byte array in little-endian format
*
* @param val Value to be written
* @param out Output array
* @param offset Starting offset
*/
public static void uint64ToByteArrayLE(long val, byte[] out, int offset) {
out[offset++] = (byte)val;
out[offset++] = (byte)(val >> 8);
out[offset++] = (byte)(val >> 16);
out[offset++] = (byte)(val >> 24);
out[offset++] = (byte)(val >> 32);
out[offset++] = (byte)(val >> 40);
out[offset++] = (byte)(val >> 48);
out[offset] = (byte)(val >> 56);
}
/**
* Write a 64-bit value to a byte stream in little-endian format
*
* @param val The value to be written
* @param stream The output stream
* @throws IOException
*/
public static void uint64ToByteStreamLE(long val, OutputStream stream) throws IOException {
stream.write((int)(0x00FF&val));
stream.write((int)(0x00FF&(val >> 8)));
stream.write((int)(0x00FF&(val >> 16)));
stream.write((int)(0x00FF&(val >> 24)));
stream.write((int)(0x00FF&(val >> 32)));
stream.write((int)(0x00FF&(val >> 40)));
stream.write((int)(0x00FF&(val >> 48)));
stream.write((int)(0x00FF&(val >> 56)));
}
/**
* Write a BigInteger value to a byte stream in little-endian format
*
* @param val BigInteger to be written
* @param stream Output stream
* @throws IOException
*/
public static void uint64ToByteStreamLE(BigInteger val, OutputStream stream) throws IOException {
byte[] bytes = val.toByteArray();
if (bytes.length > 8)
throw new RuntimeException("Input too large to encode into a uint64");
bytes = reverseBytes(bytes);
stream.write(bytes);
if (bytes.length < 8) {
for (int i=0; i<8-bytes.length; i++)
stream.write(0);
}
}
/**
* Calculate RIPEMD160(SHA256(input)). This is used in Address calculations.
*
* @param input The byte array to be hashed
* @return The hashed result
*/
public static byte[] sha256Hash160(byte[] input) {
byte[] out = new byte[20];
synchronized(digest) {
digest.reset();
byte[] sha256 = digest.digest(input);
RIPEMD160Digest rDigest = new RIPEMD160Digest();
rDigest.update(sha256, 0, sha256.length);
rDigest.doFinal(out, 0);
}
return out;
}
}