Class BigNumber

java.lang.Object
br.net.dd.netherwingcore.common.cryptography.BigNumber

public final class BigNumber extends Object
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 Details

    • BigNumber

      public BigNumber()
      Default constructor initializes to zero.
    • BigNumber

      public BigNumber(BigNumber other)
      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

      public BigNumber(String hex)
      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

      public boolean setDecStr(String str)
    • setHexStr

      public boolean setHexStr(String str)
    • setRand

      public void setRand(int numBits)
    • assign

      public BigNumber assign(BigNumber other)
    • addAssign

      public BigNumber addAssign(BigNumber other)
    • subAssign

      public BigNumber subAssign(BigNumber other)
    • mulAssign

      public BigNumber mulAssign(BigNumber other)
    • divAssign

      public BigNumber divAssign(BigNumber other)
    • modAssign

      public BigNumber modAssign(BigNumber other)
    • shiftLeftAssign

      public BigNumber shiftLeftAssign(int n)
    • add

      public BigNumber add(BigNumber other)
    • sub

      public BigNumber sub(BigNumber other)
    • mul

      public BigNumber mul(BigNumber other)
    • div

      public BigNumber div(BigNumber other)
    • mod

      public BigNumber mod(BigNumber other)
    • shiftLeft

      public BigNumber shiftLeft(int n)
    • compareTo

      public int compareTo(BigNumber other)
    • isZero

      public boolean isZero()
    • isNegative

      public boolean isNegative()
    • exp

      public BigNumber exp(BigNumber exponent)
    • modExp

      public BigNumber modExp(BigNumber exponent, BigNumber modulus)
    • 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 null
      bufsize - the number of bytes to write; must be non-negative and no greater than buf.length
      littleEndian - 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 larger
      littleEndian - 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

      public String 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

      public String 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