Description
Converts a BigInt (What is stored in Hydra-keystore and what our version of SSSS is using...) to a padded byte-array.
License
Open Source License
Parameter
Parameter | Description |
---|
bi | The big-int to be parsed |
length | The desired length |
Exception
Parameter | Description |
---|
Exception | an exception |
Return
byte[] including the bigint in padded format.
Declaration
public static byte[] BigIntToPaddedByteArray(BigInteger bi, int length)
throws Exception
Method Source Code
//package com.java2s;
/*/*from w ww.ja v a 2s.c o m*/
* Copyright (C) 2012 Helsinki Institute of Physics, University of Helsinki
* All rights reserved. See the copyright.txt in the distribution for a full
* listing of individual contributors.
* 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;
import java.util.Arrays;
public class Main {
/**
* Converts a BigInt (What is stored in Hydra-keystore and what our version of SSSS is using...) to a padded
* byte-array. Which is compatible with JCE SecretKeySpecs
*
* @param bi The big-int to be parsed
* @param length The desired length
* @return byte[] including the bigint in padded format.
* @throws Exception
*/
public static byte[] BigIntToPaddedByteArray(BigInteger bi, int length)
throws Exception {
byte[] biAsBytes = bi.toByteArray();
return paddedByteArray(biAsBytes, length);
}
/**
* Pads a byte-array with zeroes to a specific length. Used to make sure that reconstructed keys are extended to a
* JCE-compatible key-size if the randomly generated key has many zero-bits in the beginning.
*
* @param original The original array to be padded
* @param length The desired length in bytes.
* @return The key padded with zero-bits
* @throws Exception
*/
public static byte[] paddedByteArray(byte[] original, int length)
throws Exception {
// ok, it might be easier to just use this (System.arraycopy) function directly,
// and leave the tool unused
byte[] returnArray = new byte[length];
int difference = length - original.length;
// checks that the length is longer than original array
if (difference < 0) {
throw new Exception(
"New array length must be longer than original length.");
} else {
Arrays.fill(returnArray, (byte) 00);
System.arraycopy(original, 0, returnArray, difference,
original.length);
return returnArray;
}
}
}
Related
- bigIntegerToUnsignedByteArray(BigInteger a, int len)
- bigIntToArray(BigInteger data)
- bigIntToHash(BigInteger keyValue)
- bigIntToHex(BigInteger big)
- bigIntToIpV6(BigInteger argInt)
- bigIntToSortableBytes(BigInteger bigInt, int bigIntSize, byte[] result, int offset)
- biToHex(BigInteger bi)
- byteConvert32Bytes(BigInteger n)
- concat(BigInteger left, BigInteger right)