Class BigIntegerEncoding


  • public final class BigIntegerEncoding
    extends java.lang.Object
    Helper class with functions that encode and decode non-negative BigInteger to and from byte[].
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.math.BigInteger fromUnsignedBigEndianBytes​(byte[] bytes)
      Parses a BigInteger from a byte array using unsigned big-endian encoding.
      static byte[] toBigEndianBytes​(java.math.BigInteger n)
      Encodes a non-negative BigInteger into the minimal two's-complement representation in big-endian byte-order.
      static byte[] toBigEndianBytesOfFixedLength​(java.math.BigInteger n, int length)
      Encodes a non-negative BigInteger into a byte array of a specified length, using big-endian byte-order.
      static byte[] toUnsignedBigEndianBytes​(java.math.BigInteger n)
      Encodes a non-negative BigInteger into the minimal unsigned representation in big-endian byte-order.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toBigEndianBytes

        public static byte[] toBigEndianBytes​(java.math.BigInteger n)
        Encodes a non-negative BigInteger into the minimal two's-complement representation in big-endian byte-order.

        The most significant bit of the first byte is the sign bit, which is always 0 because the input number is non-negative. Because of that, the output is at the same time also an unsigned big-endian encoding that may have an additional zero byte at the beginning, and can be parsed with fromUnsignedBigEndianBytes(byte[]).

      • toUnsignedBigEndianBytes

        public static byte[] toUnsignedBigEndianBytes​(java.math.BigInteger n)
        Encodes a non-negative BigInteger into the minimal unsigned representation in big-endian byte-order.

        In the output, the first byte is never zero. BigInteger.ZERO is encoded as empty string.

      • toBigEndianBytesOfFixedLength

        public static byte[] toBigEndianBytesOfFixedLength​(java.math.BigInteger n,
                                                           int length)
                                                    throws java.security.GeneralSecurityException
        Encodes a non-negative BigInteger into a byte array of a specified length, using big-endian byte-order.

        See also RFC 8017, Sec. 4.2

        throws a GeneralSecurityException if the number is negative or length is too short.

        Throws:
        java.security.GeneralSecurityException
      • fromUnsignedBigEndianBytes

        public static java.math.BigInteger fromUnsignedBigEndianBytes​(byte[] bytes)
        Parses a BigInteger from a byte array using unsigned big-endian encoding.

        See also RFC 8017, Sec. 4.2