Java BigInteger Calculate encodeMPI(BigInteger value, boolean includeLength)

Here you can find the source of encodeMPI(BigInteger value, boolean includeLength)

Description

MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function.

License

Apache License

Parameter

Parameter Description
includeLength indicates whether the 4 byte length field should be included

Declaration

public static byte[] encodeMPI(BigInteger value, boolean includeLength) 

Method Source Code

//package com.java2s;
/**//  ww w. ja va  2s. c  o  m
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigInteger;

public class Main {
    /**
     * 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).
     * @param includeLength indicates whether the 4 byte length field should be included
     */
    public static byte[] encodeMPI(BigInteger value, boolean includeLength) {
        if (value.equals(BigInteger.ZERO)) {
            if (!includeLength)
                return new byte[] {};
            else
                return new byte[] { 0x00, 0x00, 0x00, 0x00 };
        }
        boolean isNegative = value.signum() < 0;
        if (isNegative)
            value = value.negate();
        byte[] array = value.toByteArray();
        int length = array.length;
        if ((array[0] & 0x80) == 0x80)
            length++;
        if (includeLength) {
            byte[] result = new byte[length + 4];
            System.arraycopy(array, 0, result, length - array.length + 3, array.length);
            uint32ToByteArrayBE(length, result, 0);
            if (isNegative)
                result[4] |= 0x80;
            return result;
        } else {
            byte[] result;
            if (length != array.length) {
                result = new byte[length];
                System.arraycopy(array, 0, result, 1, array.length);
            } else
                result = array;
            if (isNegative)
                result[0] |= 0x80;
            return result;
        }
    }

    public static void uint32ToByteArrayBE(long val, byte[] out, int offset) {
        out[offset + 0] = (byte) (0xFF & (val >> 24));
        out[offset + 1] = (byte) (0xFF & (val >> 16));
        out[offset + 2] = (byte) (0xFF & (val >> 8));
        out[offset + 3] = (byte) (0xFF & (val >> 0));
    }
}

Related

  1. encodeBigInt(OutputStream out, BigInteger x)
  2. encodeBigIntNoTypeCode(BigInteger value)
  3. encodeCompactBits(BigInteger value)
  4. encodeFloat(BigInteger j, int e)
  5. encodeInteger(BigInteger i, OutputStream os)
  6. encodeToBase62(BigInteger num)
  7. encodeToString(final BigInteger input)
  8. encodeURL(BigInteger id)
  9. equal(BigInteger int1, BigInteger int2)