michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.jpake; michael@0: michael@0: import java.math.BigInteger; michael@0: michael@0: public class BigIntegerHelper { michael@0: michael@0: public static byte[] BigIntegerToByteArrayWithoutSign(BigInteger value) { michael@0: byte[] bytes = value.toByteArray(); michael@0: if (bytes[0] == (byte) 0) { michael@0: bytes = copyArray(bytes, 1, bytes.length - 1); michael@0: } michael@0: return bytes; michael@0: } michael@0: michael@0: private static byte[] copyArray(byte[] original, int start, int length) { michael@0: byte[] copy = new byte[length]; michael@0: System.arraycopy(original, start, copy, 0, michael@0: Math.min(original.length - start, length)); michael@0: return copy; michael@0: } michael@0: michael@0: /** michael@0: * Convert an array of bytes to a non-negative big integer. michael@0: */ michael@0: public static BigInteger ByteArrayToBigIntegerWithoutSign(byte[] array) { michael@0: return new BigInteger(1, array); michael@0: } michael@0: michael@0: /** michael@0: * Convert a big integer into hex string. If the length is not even, add an michael@0: * '0' character in the beginning to make it even. michael@0: */ michael@0: public static String toEvenLengthHex(BigInteger value) { michael@0: String result = value.toString(16); michael@0: if (result.length() % 2 != 0) { michael@0: result = "0" + result; michael@0: } michael@0: return result; michael@0: } michael@0: }