Class Utils
-
Field Summary
Modifier and TypeFieldDescriptionstatic final com.google.common.io.BaseEncoding
Hex encoding used throughout the framework.static final int
Max initial size of variable length arrays and ArrayLists that could be attacked.static final com.google.common.base.Joiner
Joiner for concatenating words with a space inbetween.static final com.google.common.base.Splitter
Splitter for splitting words on whitespaces. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
bigIntegerToBytes
(BigInteger b, int numBytes) The regularBigInteger.toByteArray()
includes the sign bit of the number and might result in an extra byte addition.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 long
Returns the current time in milliseconds since the epoch, or a mocked out equivalent.static long
Returns the current time in seconds since the epoch, or a mocked out equivalent.static String
dateTimeFormat
(long dateTime) Formats a given date+time value to an ISO 8601 string.static String
dateTimeFormat
(Date dateTime) Formats a given date+time value to an ISO 8601 string.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 long
encodeCompactBits
(BigInteger value) static byte[]
encodeMPI
(BigInteger value, boolean includeLength) MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.static void
int64ToByteArrayLE
(long val, byte[] out, int offset) Write 8 bytes to the byte array (starting at the offset) as signed 64-bit integer in little endian format.static void
int64ToByteStreamLE
(long val, OutputStream stream) Write 8 bytes to the output stream as signed 64-bit integer in little endian format.static boolean
static boolean
isLinux()
static boolean
isMac()
static boolean
static boolean
static boolean
static Date
now()
Returns the current time, or a mocked out equivalent.static long
readInt64
(byte[] bytes, int offset) Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format.static int
readUint16
(byte[] bytes, int offset) Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.static int
readUint16BE
(byte[] bytes, int offset) Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format.static int
Parse 2 bytes from the stream as unsigned 16-bit integer in little endian format.static long
readUint32
(byte[] bytes, int offset) Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.static long
readUint32BE
(byte[] bytes, int offset) Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.static long
Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format.static void
Clears the mock clock and sleepstatic byte[]
reverseBytes
(byte[] bytes) Returns a copy of the given byte array in reverse order.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
Sets the mock clock to the current time.static void
setMockClock
(long mockClockSeconds) Sets the mock clock to the given time (in seconds).static byte[]
sha256hash160
(byte[] input) Calculates RIPEMD160(SHA256(input)).static String
static void
uint16ToByteArrayLE
(int val, byte[] out, int offset) Write 2 bytes to the byte array (starting at the offset) as unsigned 16-bit integer in little endian format.static void
uint16ToByteStreamBE
(int val, OutputStream stream) Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format.static void
uint16ToByteStreamLE
(int val, OutputStream stream) Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format.static void
uint32ToByteArrayBE
(long val, byte[] out, int offset) Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in big endian format.static void
uint32ToByteArrayLE
(long val, byte[] out, int offset) Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in little endian format.static void
uint32ToByteStreamBE
(long val, OutputStream stream) Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format.static void
uint32ToByteStreamLE
(long val, OutputStream stream) Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format.static void
uint64ToByteStreamLE
(BigInteger val, OutputStream stream) Write 8 bytes to the output stream as unsigned 64-bit integer in little endian format.
-
Field Details
-
SPACE_JOINER
public static final com.google.common.base.Joiner SPACE_JOINERJoiner for concatenating words with a space inbetween. -
WHITESPACE_SPLITTER
public static final com.google.common.base.Splitter WHITESPACE_SPLITTERSplitter for splitting words on whitespaces. -
HEX
public static final com.google.common.io.BaseEncoding HEXHex encoding used throughout the framework. Use with HEX.encode(byte[]) or HEX.decode(CharSequence). -
MAX_INITIAL_ARRAY_LENGTH
public static final int MAX_INITIAL_ARRAY_LENGTHMax initial size of variable length arrays and ArrayLists that could be attacked. Avoids this attack: Attacker sends a msg indicating it will contain a huge number (eg 2 billion) elements (eg transaction inputs) and forces bitcoinj to try to allocate a huge piece of the memory resulting in OutOfMemoryError.- See Also:
-
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
bigIntegerToBytes
The regular
BigInteger.toByteArray()
includes the sign bit of the number and might result in an extra byte addition. This method removes this extra byte.Assuming only positive numbers, it's possible to discriminate if an extra byte is added by checking if the first element of the array is 0 (0000_0000). Due to the minimal representation provided by BigInteger, it means that the bit sign is the least significant bit 0000_0000 . Otherwise the representation is not minimal. For example, if the sign bit is 0000_0000, then the representation is not minimal due to the rightmost zero.
- Parameters:
b
- the integer to format into a byte arraynumBytes
- the desired size of the resulting byte array- Returns:
- numBytes byte long array.
-
uint16ToByteArrayLE
public static void uint16ToByteArrayLE(int val, byte[] out, int offset) Write 2 bytes to the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. -
uint32ToByteArrayLE
public static void uint32ToByteArrayLE(long val, byte[] out, int offset) Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. -
uint32ToByteArrayBE
public static void uint32ToByteArrayBE(long val, byte[] out, int offset) Write 4 bytes to the byte array (starting at the offset) as unsigned 32-bit integer in big endian format. -
int64ToByteArrayLE
public static void int64ToByteArrayLE(long val, byte[] out, int offset) Write 8 bytes to the byte array (starting at the offset) as signed 64-bit integer in little endian format. -
uint16ToByteStreamLE
Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format.- Throws:
IOException
-
uint16ToByteStreamBE
Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format.- Throws:
IOException
-
uint32ToByteStreamLE
Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format.- Throws:
IOException
-
uint32ToByteStreamBE
Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format.- Throws:
IOException
-
int64ToByteStreamLE
Write 8 bytes to the output stream as signed 64-bit integer in little endian format.- Throws:
IOException
-
uint64ToByteStreamLE
Write 8 bytes to the output stream as unsigned 64-bit integer in little endian format.- Throws:
IOException
-
readUint16
public static int readUint16(byte[] bytes, int offset) Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in little endian format. -
readUint32
public static long readUint32(byte[] bytes, int offset) Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in little endian format. -
readInt64
public static long readInt64(byte[] bytes, int offset) Parse 8 bytes from the byte array (starting at the offset) as signed 64-bit integer in little endian format. -
readUint32BE
public static long readUint32BE(byte[] bytes, int offset) Parse 4 bytes from the byte array (starting at the offset) as unsigned 32-bit integer in big endian format. -
readUint16BE
public static int readUint16BE(byte[] bytes, int offset) Parse 2 bytes from the byte array (starting at the offset) as unsigned 16-bit integer in big endian format. -
readUint16FromStream
Parse 2 bytes from the stream as unsigned 16-bit integer in little endian format. -
readUint32FromStream
Parse 4 bytes from the stream as unsigned 32-bit integer in little endian format. -
reverseBytes
public static byte[] reverseBytes(byte[] bytes) Returns a copy of the given byte array in reverse order. -
sha256hash160
public static byte[] sha256hash160(byte[] input) Calculates RIPEMD160(SHA256(input)). This is used in Address calculations. -
decodeMPI
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).- Parameters:
hasLength
- can be set to false if the given array is missing the 4 byte length field
-
encodeMPI
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).- Parameters:
includeLength
- indicates whether the 4 byte length field should be included
-
decodeCompactBits
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.
-
encodeCompactBits
- See Also:
-
rollMockClock
Advances (or rewinds) the mock clock by the given number of seconds. -
rollMockClockMillis
Advances (or rewinds) the mock clock by the given number of milliseconds. -
setMockClock
public static void setMockClock()Sets the mock clock to the current time. -
setMockClock
public static void setMockClock(long mockClockSeconds) Sets the mock clock to the given time (in seconds). -
resetMocking
public static void resetMocking()Clears the mock clock and sleep -
now
Returns the current time, or a mocked out equivalent. -
currentTimeMillis
public static long currentTimeMillis()Returns the current time in milliseconds since the epoch, or a mocked out equivalent. -
currentTimeSeconds
public static long currentTimeSeconds()Returns the current time in seconds since the epoch, or a mocked out equivalent. -
dateTimeFormat
Formats a given date+time value to an ISO 8601 string.- Parameters:
dateTime
- value to format, as a Date
-
dateTimeFormat
Formats a given date+time value to an ISO 8601 string.- Parameters:
dateTime
- value to format, unix time (ms)
-
checkBitLE
public 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) -
setBitLE
public 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) -
isAndroidRuntime
public static boolean isAndroidRuntime() -
isOpenJDKRuntime
public static boolean isOpenJDKRuntime() -
isOracleJavaRuntime
public static boolean isOracleJavaRuntime() -
isLinux
public static boolean isLinux() -
isWindows
public static boolean isWindows() -
isMac
public static boolean isMac() -
toString
-