public class Utils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
BITCOIN_SIGNED_MESSAGE_HEADER
The string that prefixes all text messages signed using Bitcoin keys.
|
static byte[] |
BITCOIN_SIGNED_MESSAGE_HEADER_BYTES |
static BaseEncoding |
HEX
Hex encoding used throughout the framework.
|
static Date |
mockTime
If non-null, overrides the return value of now().
|
Constructor and Description |
---|
Utils() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
appendByte(byte[] bytes,
byte b)
Creates a copy of bytes and appends b to the end of it
|
static byte[] |
bigIntegerToBytes(BigInteger b,
int numBytes)
The regular
BigInteger.toByteArray() method isn't quite what we often need: it appends a
leading zero to indicate that the number is positive and may need padding. |
static boolean |
checkBitLE(byte[] data,
int index)
Checks if the given bit is set in data, using little endian (not the same as Java native big endian)
|
static byte[] |
copyOf(byte[] in,
int length) |
static long |
currentTimeMillis()
Returns the current time in milliseconds since the epoch, or a mocked out equivalent.
|
static long |
currentTimeSeconds() |
static BigInteger |
decodeCompactBits(long compact)
The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a
floating point format.
|
static BigInteger |
decodeMPI(byte[] mpi,
boolean hasLength)
MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
|
static byte[] |
doubleDigest(byte[] input)
|
static byte[] |
doubleDigest(byte[] input,
int offset,
int length)
Calculates the SHA-256 hash of the given byte range, and then hashes the resulting hash again.
|
static byte[] |
doubleDigestTwoBuffers(byte[] input1,
int offset1,
int length1,
byte[] input2,
int offset2,
int length2)
Calculates SHA256(SHA256(byte range 1 + byte range 2)).
|
static long |
encodeCompactBits(BigInteger value) |
static byte[] |
encodeMPI(BigInteger value,
boolean includeLength)
MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.
|
static void |
finishMockSleep()
Let the sleeping thread pass the synchronization point any number of times.
|
static byte[] |
formatMessageForSigning(String message)
Given a textual message, returns a byte buffer formatted as follows:
|
static String |
getResourceAsString(URL url)
Reads and joins together with LF char (\n) all the lines from given file.
|
static void |
int64ToByteStreamLE(long val,
OutputStream stream) |
static boolean |
isAndroidRuntime() |
static boolean |
isLessThanOrEqualToUnsigned(long n1,
long n2)
Work around lack of unsigned types in Java.
|
static boolean |
isLessThanUnsigned(long n1,
long n2)
Work around lack of unsigned types in Java.
|
static boolean |
isWindows() |
static int |
maxOfMostFreq(int... items) |
static int |
maxOfMostFreq(List<Integer> items) |
static Date |
now()
Returns the current time, or a mocked out equivalent.
|
static byte[] |
parseAsHexOrBase58(String data)
Attempts to parse the given string as arbitrary-length hex or base58 and then return the results, or null if
neither parse was successful.
|
static void |
passMockSleep()
Let sleeping thread pass the synchronization point.
|
static long |
readInt64(byte[] bytes,
int offset) |
static int |
readUint16BE(byte[] bytes,
int offset) |
static long |
readUint32(byte[] bytes,
int offset) |
static long |
readUint32BE(byte[] bytes,
int offset) |
static byte[] |
reverseBytes(byte[] bytes)
Returns a copy of the given byte array in reverse order.
|
static byte[] |
reverseDwordBytes(byte[] bytes,
int trimLength)
Returns a copy of the given byte array with the bytes of each double-word (4 bytes) reversed.
|
static Date |
rollMockClock(int seconds)
Advances (or rewinds) the mock clock by the given number of seconds.
|
static Date |
rollMockClockMillis(long millis)
Advances (or rewinds) the mock clock by the given number of milliseconds.
|
static void |
setBitLE(byte[] data,
int index)
Sets the given bit in data to one, using little endian (not the same as Java native big endian)
|
static void |
setMockClock()
Sets the mock clock to the current time.
|
static void |
setMockClock(long mockClockSeconds)
Sets the mock clock to the given time (in seconds).
|
static void |
setMockSleep(boolean isEnable)
Enable or disable mock sleep.
|
static byte[] |
sha256hash160(byte[] input)
Calculates RIPEMD160(SHA256(input)).
|
static byte[] |
singleDigest(byte[] input,
int offset,
int length) |
static void |
sleep(long millis)
Sleep for a span of time, or mock sleep if enabled
|
static void |
uint32ToByteArrayBE(long val,
byte[] out,
int offset) |
static void |
uint32ToByteArrayLE(long val,
byte[] out,
int offset) |
static void |
uint32ToByteStreamLE(long val,
OutputStream stream) |
static void |
uint64ToByteArrayLE(long val,
byte[] out,
int offset) |
static void |
uint64ToByteStreamLE(BigInteger val,
OutputStream stream) |
public static final String BITCOIN_SIGNED_MESSAGE_HEADER
public static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES
public static final BaseEncoding HEX
public static volatile Date mockTime
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes)
BigInteger.toByteArray()
method isn't quite what we often need: it appends a
leading zero to indicate that the number is positive and may need padding.b
- the integer to format into a byte arraynumBytes
- the desired size of the resulting byte arraypublic static void uint32ToByteArrayBE(long val, byte[] out, int offset)
public static void uint32ToByteArrayLE(long val, byte[] out, int offset)
public static void uint64ToByteArrayLE(long val, byte[] out, int offset)
public static void uint32ToByteStreamLE(long val, OutputStream stream) throws IOException
IOException
public static void int64ToByteStreamLE(long val, OutputStream stream) throws IOException
IOException
public static void uint64ToByteStreamLE(BigInteger val, OutputStream stream) throws IOException
IOException
public static byte[] doubleDigest(byte[] input)
public static byte[] doubleDigest(byte[] input, int offset, int length)
public static byte[] singleDigest(byte[] input, int offset, int length)
public static byte[] doubleDigestTwoBuffers(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2)
public static boolean isLessThanUnsigned(long n1, long n2)
public static boolean isLessThanOrEqualToUnsigned(long n1, long n2)
public static byte[] reverseBytes(byte[] bytes)
public static byte[] reverseDwordBytes(byte[] bytes, int trimLength)
bytes
- length must be divisible by 4.trimLength
- trim output to this length. If positive, must be divisible by 4.public static long readUint32(byte[] bytes, int offset)
public static long readInt64(byte[] bytes, int offset)
public static long readUint32BE(byte[] bytes, int offset)
public static int readUint16BE(byte[] bytes, int offset)
public static byte[] sha256hash160(byte[] input)
public static BigInteger decodeMPI(byte[] mpi, boolean hasLength)
hasLength
- can be set to false if the given array is missing the 4 byte length fieldpublic static byte[] encodeMPI(BigInteger value, boolean includeLength)
includeLength
- indicates whether the 4 byte length field should be includedpublic static BigInteger decodeCompactBits(long compact)
The "compact" format is a representation of a whole number N using an unsigned 32 bit number similar to a floating point format. The most significant 8 bits are the unsigned exponent of base 256. This exponent can be thought of as "number of bytes of N". The lower 23 bits are the mantissa. Bit number 24 (0x800000) represents the sign of N. Therefore, N = (-1^sign) * mantissa * 256^(exponent-3).
Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn(). MPI uses the most significant bit of the first byte as sign. Thus 0x1234560000 is compact 0x05123456 and 0xc0de000000 is compact 0x0600c0de. Compact 0x05c0de00 would be -0x40de000000.
Bitcoin only uses this "compact" format for encoding difficulty targets, which are unsigned 256bit quantities. Thus, all the complexities of the sign bit and using base 256 are probably an implementation accident.
public static long encodeCompactBits(BigInteger value)
decodeCompactBits(long)
public static Date rollMockClock(int seconds)
public static Date rollMockClockMillis(long millis)
public static void setMockClock()
public static void setMockClock(long mockClockSeconds)
public static Date now()
public static long currentTimeMillis()
public static long currentTimeSeconds()
public static byte[] copyOf(byte[] in, int length)
public static byte[] appendByte(byte[] bytes, byte b)
public static byte[] parseAsHexOrBase58(String data)
public static boolean isWindows()
public static byte[] formatMessageForSigning(String message)
Given a textual message, returns a byte buffer formatted as follows:
[24] "Bitcoin Signed Message:\n" [message.length as a varint] message
public static boolean checkBitLE(byte[] data, int index)
public static void setBitLE(byte[] data, int index)
public static void sleep(long millis)
public static void setMockSleep(boolean isEnable)
public static void passMockSleep()
public static void finishMockSleep()
public static boolean isAndroidRuntime()
public static int maxOfMostFreq(int... items)
public static String getResourceAsString(URL url) throws IOException
IOException
Copyright © 2014. All rights reserved.