• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.net.utils;
17 
18 import java.math.BigInteger;
19 
20 /** BigIntegerUtils provides utility methods for doing Key Exchange. */
21 public final class BigIntegerUtils {
22 
23     /**
24      * Converts the unsigned Hex String to a positive BigInteger.
25      *
26      * @param hexString Hex representation of an unsigned value
27      * @return the argument converted to BigInteger by an unsigned conversion
28      */
unsignedHexStringToBigInteger(String hexString)29     public static BigInteger unsignedHexStringToBigInteger(String hexString) {
30         return new BigInteger(hexString, 16);
31     }
32 
33     /**
34      * Converts the unsigned byte array to a positive BigInteger.
35      *
36      * @param byteArray byte array that represents an unsigned value
37      * @return the argument converted to BigInteger by an unsigned conversion
38      */
unsignedByteArrayToBigInteger(byte[] byteArray)39     public static BigInteger unsignedByteArrayToBigInteger(byte[] byteArray) {
40         return new BigInteger(1/** positive */, byteArray);
41     }
42 
43     /**
44      * Returns a byte array containing the unsigned representation of this BigInteger. Zero-pad on
45      * the left of byte array to desired size.
46      *
47      * @param bigInteger input BigInteger
48      * @param size size of the output byte array
49      * @return the byte array containing the unsigned representation of this BigInteger.
50      */
bigIntegerToUnsignedByteArray(BigInteger bigInteger, int size)51     public static byte[] bigIntegerToUnsignedByteArray(BigInteger bigInteger, int size) {
52         byte[] byteArrayWithSignBit = bigInteger.toByteArray();
53         int len = byteArrayWithSignBit.length;
54         byte[] output = new byte[size];
55 
56         // {@link BigInteger} provides method {@link toByteArray} that returns a byte array
57         // containing the two's-complement representation of this BigInteger (minimum number of
58         // bytes required to represent this BigInteger, including at least one sign bit). This
59         // method first remove additional byte that caused by this sign bit and zero-pad on the left
60         // of byte array to desired size.
61         if (bigInteger.bitLength() % 8 == 0) {
62             len = len - 1;
63             System.arraycopy(byteArrayWithSignBit, 1, output, size - len, len);
64         } else {
65             System.arraycopy(byteArrayWithSignBit, 0, output, size - len, len);
66         }
67 
68         return output;
69     }
70 }
71