Class BigNumber
java.lang.Object
br.net.dd.netherwingcore.common.cryptography.BigNumber
A BigNumber class that mimics the behavior of OpenSSL's BIGNUM,
with constructors and methods for setting values from various formats, performing arithmetic operations,
and converting to/from byte arrays and strings. This class is immutable and thread-safe.
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor initializes to zero.BigNumber(byte[] binary, boolean littleEndian) BigNumber(int vSigned32) Note: Java has no unsigned int, so we use long for the constructor to allow values up to 0xFFFFFFFF.BigNumber(long vUnsigned32) Note: Java has no unsigned int, so we use long for the constructor to allow values up to 0xFFFFFFFF.Copy constructor: since BigInteger is immutable, we can safely share the reference. -
Method Summary
Modifier and TypeMethodDescriptionasDecStr()Returns the decimal string representation of this BigNumber.longasHexStr()Returns the hexadecimal string representation of this BigNumber.intvoidgetBytes(byte[] buf, int bufsize, boolean littleEndian) Writes the unsigned big-endian (or little-endian) byte representation of this BigNumber into the provided buffer.intintbooleanbooleanisZero()voidsetBinary(byte[] bytes, boolean littleEndian) booleanvoidsetDword(int val) voidsetDwordUnsigned(long val) booleanvoidsetQword(long valUnsigned64) voidsetRand(int numBits) shiftLeft(int n) shiftLeftAssign(int n) byte[]toByteVector(int minSize, boolean littleEndian) Returns a byte array containing the unsigned big-endian (or little-endian) representation of this BigNumber, padded with leading zeros if necessary to reach at least minSize bytes.
-
Constructor Details
-
BigNumber
public BigNumber()Default constructor initializes to zero. -
BigNumber
Copy constructor: since BigInteger is immutable, we can safely share the reference.- Parameters:
other- the BigNumber to copy
-
BigNumber
public BigNumber(long vUnsigned32) Note: Java has no unsigned int, so we use long for the constructor to allow values up to 0xFFFFFFFF.- Parameters:
vUnsigned32- the unsigned 32-bit integer value to set; will be treated as unsigned, so values above 0x7FFFFFFF will produce positive BigNumbers
-
BigNumber
public BigNumber(int vSigned32) Note: Java has no unsigned int, so we use long for the constructor to allow values up to 0xFFFFFFFF.- Parameters:
vSigned32- the signed 32-bit integer value to set; will be treated as signed, so negative values will produce negative BigNumbers
-
BigNumber
- Parameters:
hex- the hexadecimal string to set; may optionally start with "0x" or "0X"; will be treated as a positive number regardless of leading zeros; must not be null or empty after trimming
-
BigNumber
public BigNumber(byte[] binary, boolean littleEndian)
-
-
Method Details
-
setDword
public void setDword(int val) -
setDwordUnsigned
public void setDwordUnsigned(long val) -
setQword
public void setQword(long valUnsigned64) -
setBinary
public void setBinary(byte[] bytes, boolean littleEndian) -
setDecStr
-
setHexStr
-
setRand
public void setRand(int numBits) -
assign
-
addAssign
-
subAssign
-
mulAssign
-
divAssign
-
modAssign
-
shiftLeftAssign
-
add
-
sub
-
mul
-
div
-
mod
-
shiftLeft
-
compareTo
-
isZero
public boolean isZero() -
isNegative
public boolean isNegative() -
exp
-
modExp
-
getNumBytes
public int getNumBytes() -
getNumBits
public int getNumBits() -
asDwordUnsigned
public long asDwordUnsigned() -
getBytes
public void getBytes(byte[] buf, int bufsize, boolean littleEndian) Writes the unsigned big-endian (or little-endian) byte representation of this BigNumber into the provided buffer. The number is treated as unsigned, and will be padded with leading zeros if necessary to fill the buffer. This mimics OpenSSL's BN_bn2bin and BN_bn2lebin behavior, which treat the number as unsigned.- Parameters:
buf- the buffer to write into; must not be nullbufsize- the number of bytes to write; must be non-negative and no greater than buf.lengthlittleEndian- whether to write the bytes in little-endian order (true) or big-endian order (false)- Throws:
IllegalArgumentException- if bufsize is invalid or too small to hold the number
-
toByteVector
public byte[] toByteVector(int minSize, boolean littleEndian) Returns a byte array containing the unsigned big-endian (or little-endian) representation of this BigNumber, padded with leading zeros if necessary to reach at least minSize bytes. This mimics OpenSSL's BN_bn2bin and BN_bn2lebin behavior, which treat the number as unsigned.- Parameters:
minSize- the minimum size of the output byte array; if the number requires more bytes, it will be largerlittleEndian- whether to return the bytes in little-endian order (true) or big-endian order (false)- Returns:
- a byte array containing the unsigned big-endian (or little-endian) representation of this BigNumber
-
asHexStr
Returns the hexadecimal string representation of this BigNumber. This mimics OpenSSL's BN_bn2hex behavior. Note: OpenSSL's BN_bn2hex returns uppercase without "0x" prefix, and no leading zeros.- Returns:
- the hexadecimal string representation of this BigNumber
-
asDecStr
Returns the decimal string representation of this BigNumber. This mimics OpenSSL's BN_bn2dec behavior. Note: OpenSSL's BN_bn2dec returns a string with a leading '-' for negative numbers, and no leading zeros for positive numbers.- Returns:
- the decimal string representation of this BigNumber
-