mobile/android/base/sync/jpake/BigIntegerHelper.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/jpake/BigIntegerHelper.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.jpake;
     1.9 +
    1.10 +import java.math.BigInteger;
    1.11 +
    1.12 +public class BigIntegerHelper {
    1.13 +
    1.14 +  public static byte[] BigIntegerToByteArrayWithoutSign(BigInteger value) {
    1.15 +    byte[] bytes = value.toByteArray();
    1.16 +    if (bytes[0] == (byte) 0) {
    1.17 +      bytes = copyArray(bytes, 1, bytes.length - 1);
    1.18 +    }
    1.19 +    return bytes;
    1.20 +  }
    1.21 +
    1.22 +  private static byte[] copyArray(byte[] original, int start, int length) {
    1.23 +    byte[] copy = new byte[length];
    1.24 +    System.arraycopy(original, start, copy, 0,
    1.25 +        Math.min(original.length - start, length));
    1.26 +    return copy;
    1.27 +  }
    1.28 +
    1.29 +  /**
    1.30 +   * Convert an array of bytes to a non-negative big integer.
    1.31 +   */
    1.32 +  public static BigInteger ByteArrayToBigIntegerWithoutSign(byte[] array) {
    1.33 +    return new BigInteger(1, array);
    1.34 +  }
    1.35 +
    1.36 +  /**
    1.37 +   * Convert a big integer into hex string. If the length is not even, add an
    1.38 +   * '0' character in the beginning to make it even.
    1.39 +   */
    1.40 +  public static String toEvenLengthHex(BigInteger value) {
    1.41 +    String result = value.toString(16);
    1.42 +    if (result.length() % 2 != 0) {
    1.43 +      result = "0" + result;
    1.44 +    }
    1.45 +    return result;
    1.46 +  }
    1.47 +}

mercurial