Class Utils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.common.io.BaseEncodingHex encoding used throughout the framework.static final intMax initial size of variable length arrays and ArrayLists that could be attacked.static final com.google.common.base.JoinerJoiner for concatenating words with a space inbetween.static final com.google.common.base.SplitterSplitter for splitting words on whitespaces. -
Constructor Summary
Constructors -
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 booleancheckBitLE(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 longReturns the current time in milliseconds since the epoch, or a mocked out equivalent.static longReturns the current time in seconds since the epoch, or a mocked out equivalent.static StringdateTimeFormat(long dateTime) Formats a given date+time value to an ISO 8601 string.static StringdateTimeFormat(Date dateTime) Formats a given date+time value to an ISO 8601 string.static BigIntegerdecodeCompactBits(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 BigIntegerdecodeMPI(byte[] mpi, boolean hasLength) MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.static longencodeCompactBits(BigInteger value) static byte[]encodeMPI(BigInteger value, boolean includeLength) MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.static voidint64ToByteArrayLE(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 voidint64ToByteStreamLE(long val, OutputStream stream) Write 8 bytes to the output stream as signed 64-bit integer in little endian format.static booleanstatic booleanisLinux()static booleanisMac()static booleanstatic booleanstatic booleanstatic Datenow()Returns the current time, or a mocked out equivalent.static longreadInt64(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 intreadUint16(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 intreadUint16BE(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 intParse 2 bytes from the stream as unsigned 16-bit integer in little endian format.static longreadUint32(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 longreadUint32BE(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 longParse 4 bytes from the stream as unsigned 32-bit integer in little endian format.static voidClears the mock clock and sleepstatic byte[]reverseBytes(byte[] bytes) Returns a copy of the given byte array in reverse order.static DaterollMockClock(int seconds) Advances (or rewinds) the mock clock by the given number of seconds.static DaterollMockClockMillis(long millis) Advances (or rewinds) the mock clock by the given number of milliseconds.static voidsetBitLE(byte[] data, int index) Sets the given bit in data to one, using little endian (not the same as Java native big endian)static voidSets the mock clock to the current time.static voidsetMockClock(long mockClockSeconds) Sets the mock clock to the given time (in seconds).static byte[]sha256hash160(byte[] input) Calculates RIPEMD160(SHA256(input)).static Stringstatic voiduint16ToByteArrayLE(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 voiduint16ToByteStreamBE(int val, OutputStream stream) Write 2 bytes to the output stream as unsigned 16-bit integer in big endian format.static voiduint16ToByteStreamLE(int val, OutputStream stream) Write 2 bytes to the output stream as unsigned 16-bit integer in little endian format.static voiduint32ToByteArrayBE(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 voiduint32ToByteArrayLE(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 voiduint32ToByteStreamBE(long val, OutputStream stream) Write 4 bytes to the output stream as unsigned 32-bit integer in big endian format.static voiduint32ToByteStreamLE(long val, OutputStream stream) Write 4 bytes to the output stream as unsigned 32-bit integer in little endian format.static voiduint64ToByteStreamLE(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
-