本文整理汇总了Java中java.math.BigInteger.toByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.toByteArray方法的具体用法?Java BigInteger.toByteArray怎么用?Java BigInteger.toByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.toByteArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: byteConvert32Bytes
import java.math.BigInteger; //导入方法依赖的package包/类
public static byte[] byteConvert32Bytes(BigInteger n) {
byte tmpd[] = (byte[]) null;
if (n == null) {
return null;
}
if (n.toByteArray().length == 33) {
tmpd = new byte[32];
System.arraycopy(n.toByteArray(), 1, tmpd, 0, 32);
} else if (n.toByteArray().length == 32) {
tmpd = n.toByteArray();
} else {
tmpd = new byte[32];
for (int i = 0; i < 32 - n.toByteArray().length; i++) {
tmpd[i] = 0;
}
System.arraycopy(n.toByteArray(), 0, tmpd, 32 - n.toByteArray().length, n.toByteArray().length);
}
return tmpd;
}
示例2: bigIntegerToByteArray
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Takes a BigInteger value and returns its byte array representation filled
* with 0x00 bytes to achieve the block size length.
*
* @param value
* big integer to be converted
* @param blockSize
* block size to be achieved
* @param removeSignByte
* in a case the removeSignByte is set, the sign byte is removed
* (in case the byte array contains one)
* @return big integer represented in bytes, padded to a specific block size
*/
public static byte[] bigIntegerToByteArray(BigInteger value, int blockSize, boolean removeSignByte) {
byte[] array = value.toByteArray();
int remainder = array.length % blockSize;
byte[] result = array;
byte[] tmp;
if (removeSignByte && result[0] == 0x0) {
tmp = new byte[result.length - 1];
System.arraycopy(result, 1, tmp, 0, tmp.length);
result = tmp;
remainder = tmp.length % blockSize;
}
if (remainder > 0) {
// add zeros to fit size
tmp = new byte[result.length + blockSize - remainder];
System.arraycopy(result, 0, tmp, blockSize - remainder, result.length);
result = tmp;
}
return result;
}
示例3: SerializeBigInteger
import java.math.BigInteger; //导入方法依赖的package包/类
public static byte[] SerializeBigInteger(BigInteger BigInt) {
int bnlen = BigInt.bitLength() / 8;
byte[] large_int_b = new byte[bnlen];
Arrays.fill(large_int_b, (byte) 0);
int int_len = BigInt.toByteArray().length;
if (int_len == bnlen) {
large_int_b = BigInt.toByteArray();
} else if (int_len > bnlen) {
large_int_b = Arrays.copyOfRange(BigInt.toByteArray(), int_len
- bnlen, int_len);
} else if (int_len < bnlen) {
System.arraycopy(BigInt.toByteArray(), 0, large_int_b,
large_int_b.length - int_len, int_len);
}
return large_int_b;
}
示例4: decode
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* 解码
* <param name="input">要解码的字符串</param>
* <returns>返回解码后的字节数组</returns>
*/
public static byte[] decode(String input) {
BigInteger bi = BigInteger.ZERO;
for (int i = input.length() - 1; i >= 0; i--) {
int index = ALPHABET.indexOf(input.charAt(i));
if (index == -1) {
throw new IllegalArgumentException();
}
bi = bi.add(BASE.pow(input.length() - 1 - i).multiply(BigInteger.valueOf(index)));
}
byte[] bytes = bi.toByteArray();
boolean stripSignByte = bytes.length > 1 && bytes[0] == 0 && bytes[1] < 0;
int leadingZeros = 0;
for (; leadingZeros < input.length() && input.charAt(leadingZeros) == ALPHABET.charAt(0); leadingZeros++);
byte[] tmp = new byte[bytes.length - (stripSignByte ? 1 : 0) + leadingZeros];
System.arraycopy(bytes, stripSignByte ? 1 : 0, tmp, leadingZeros, tmp.length - leadingZeros);
return tmp;
}
示例5: test_calldatasize
import java.math.BigInteger; //导入方法依赖的package包/类
@Test
public void test_calldatasize() throws Exception {
BytecodeChunk chunk = createChunk(0,
new Opcode(Opcodes.CALLDATASIZE, null),
new Opcode(Opcodes.STOP, null)
);
BigInteger callData = new BigInteger("3f7a0270000000000000000000000000000000000000000000000000000000000000002c", 16);
byte[] callDataBytes = callData.toByteArray();
EVMEnvironment inputs = new EVMEnvironmentBuilder().setCallData(callDataBytes).build();
EVMState evmState = symExecute(new HashMap<Integer, BytecodeChunk>() {{
put(0, chunk);
}}, inputs);
EVMStack stack = evmState.getStack();
assertTrue(stack.size() == 1);
assertEquals(0x24, stack.pop().getIntData());
}
示例6: test_calldataload_long_size
import java.math.BigInteger; //导入方法依赖的package包/类
@Test
public void test_calldataload_long_size() throws Exception {
BytecodeChunk chunk = createChunk(0,
new Opcode(Opcodes.PUSH1, BigInteger.valueOf(0x0)),
new Opcode(Opcodes.CALLDATALOAD, null),
new Opcode(Opcodes.STOP, null)
);
BigInteger callData = new BigInteger("3f7a0270000000000000000000000000000000000000000000000000000000000000002c", 16);
BigInteger expectedCallData = new BigInteger("3f7a027000000000000000000000000000000000000000000000000000000000", 16);
byte[] callDataBytes = callData.toByteArray();
byte[] expectedCallDataBytes = expectedCallData.toByteArray();
EVMEnvironment inputs = new EVMEnvironmentBuilder().setCallData(callDataBytes).build();
EVMState evmState = symExecute(new HashMap<Integer, BytecodeChunk>() {{
put(0, chunk);
}}, inputs);
EVMStack stack = evmState.getStack();
assertTrue(stack.size() == 1);
assertTrue(Arrays.equals(expectedCallDataBytes, stack.pop().getBytes()));
}
示例7: pow
import java.math.BigInteger; //导入方法依赖的package包/类
public static BigInteger pow(BigInteger x, BigInteger y) {
if (y.compareTo(BigInteger.ZERO) < 0)
throw new IllegalArgumentException();
BigInteger z = x; // z will successively become x^2, x^4, x^8, x^16,
// x^32...
BigInteger result = BigInteger.ONE;
byte[] bytes = y.toByteArray();
for (int i = bytes.length - 1; i >= 0; i--) {
byte bits = bytes[i];
for (int j = 0; j < 8; j++) {
if ((bits & 1) != 0)
result = result.multiply(z);
// short cut out if there are no more bits to handle:
if ((bits >>= 1) == 0 && i == 0)
return result;
z = z.multiply(z);
}
}
return result;
}
示例8: createHeader
import java.math.BigInteger; //导入方法依赖的package包/类
private BlockHeader createHeader(Block newBlockParent, List<BlockHeader> uncles, List<Transaction> txs, BigInteger minimumGasPrice) {
final byte[] unclesListHash = HashUtil.sha3(BlockHeader.getUnclesEncodedEx(uncles));
final long timestampSeconds = this.getCurrentTimeInSeconds();
// Set gas limit before executing block
BigInteger minGasLimit = BigInteger.valueOf(miningConfig.getGasLimit().getMininimum());
BigInteger targetGasLimit = BigInteger.valueOf(miningConfig.getGasLimit().getTarget());
BigInteger parentGasLimit = new BigInteger(1, newBlockParent.getGasLimit());
BigInteger gasUsed = BigInteger.valueOf(newBlockParent.getGasUsed());
boolean forceLimit = miningConfig.getGasLimit().isTargetForced();
BigInteger gasLimit = gasLimitCalculator.calculateBlockGasLimit(parentGasLimit,
gasUsed, minGasLimit, targetGasLimit, forceLimit);
final BlockHeader newHeader = new BlockHeader(newBlockParent.getHash(),
unclesListHash,
coinbaseAddress,
new Bloom().getData(),
new byte[]{1},
newBlockParent.getNumber() + 1,
gasLimit.toByteArray(),
0,
timestampSeconds,
new byte[]{},
new byte[]{},
new byte[]{},
new byte[]{},
minimumGasPrice.toByteArray(),
CollectionUtils.size(uncles)
);
newHeader.setDifficulty(difficultyCalculator.calcDifficulty(newHeader, newBlockParent.getHeader()).toByteArray());
newHeader.setTransactionsRoot(Block.getTxTrie(txs).getHash());
return newHeader;
}
示例9: toIntegerBytes
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Returns a byte-array representation of a <code>BigInteger</code>
* without sign bit.
*
* @param bigInt <code>BigInteger</code> to be converted
* @return a byte array representation of the BigInteger parameter
*/
static byte[] toIntegerBytes(BigInteger bigInt) {
int bitlen = bigInt.bitLength();
// round bitlen
bitlen = ((bitlen + 7) >> 3) << 3;
byte[] bigBytes = bigInt.toByteArray();
if (((bigInt.bitLength() % 8) != 0) &&
(((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
return bigBytes;
}
// set up params for copying everything but sign bit
int startSrc = 0;
int len = bigBytes.length;
// if bigInt is exactly byte-aligned, just skip signbit in copy
if ((bigInt.bitLength() % 8) == 0) {
startSrc = 1;
len--;
}
int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
byte[] resizedBytes = new byte[bitlen / 8];
System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
return resizedBytes;
}
示例10: execute
import java.math.BigInteger; //导入方法依赖的package包/类
@Override
public void execute(Frame frame) {
OperandStack stack = frame.getOperandStack();
int value = stack.popInt();
BigInteger bi = new BigInteger(String.valueOf(value));
byte[] ba = new byte[4];
byte[] bia = bi.toByteArray();
System.arraycopy(bia, 0, ba, ba.length-bia.length, bia.length);
stack.pushInt(new BigInteger(ba).intValue());
}
示例11: toIntegerBytes
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Returns a byte-array representation of a <code>BigInteger</code>
* without sign bit.
*
* @param bigInt <code>BigInteger</code> to be converted
* @return a byte array representation of the BigInteger parameter
*/
static byte[] toIntegerBytes(BigInteger bigInt) {
int bitlen = bigInt.bitLength();
// round bitlen
bitlen = ((bitlen + 7) >> 3) << 3;
byte[] bigBytes = bigInt.toByteArray();
if(((bigInt.bitLength() % 8) != 0) &&
(((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
return bigBytes;
}
// set up params for copying everything but sign bit
int startSrc = 0;
int len = bigBytes.length;
// if bigInt is exactly byte-aligned, just skip signbit in copy
if((bigInt.bitLength() % 8) == 0) {
startSrc = 1;
len--;
}
int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
byte[] resizedBytes = new byte[bitlen / 8];
System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
return resizedBytes;
}
示例12: setQWordValue
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Sets the Value of the current metadata descriptor. <br>
* Using this method will change {@link #descriptorType}to
* {@link #TYPE_QWORD}
*
* @param value Value to set.
* @throws NumberFormatException on <code>null</code> values.
* @throws IllegalArgumentException on illegal values or values exceeding range.
*/
public void setQWordValue(final BigInteger value)
throws IllegalArgumentException {
if (value == null) {
throw new NumberFormatException("null");
}
if (BigInteger.ZERO.compareTo(value) > 0) {
throw new IllegalArgumentException(
"Only unsigned values allowed (no negative)");
}
if (MetadataDescriptor.QWORD_MAXVALUE.compareTo(value) < 0) {
throw new IllegalArgumentException(
"Value exceeds QWORD (64 bit unsigned)");
}
this.content = new byte[8];
final byte[] valuesBytes = value.toByteArray();
if (valuesBytes.length <= 8) {
for (int i = valuesBytes.length - 1; i >= 0; i--) {
this.content[valuesBytes.length - (i + 1)] = valuesBytes[i];
}
} else {
/*
* In case of 64-Bit set
*/
Arrays.fill(this.content, (byte) 0xFF);
}
this.descriptorType = TYPE_QWORD;
}
示例13: writeDecimal
import java.math.BigInteger; //导入方法依赖的package包/类
protected void writeDecimal(BigDecimal o) {
int scale = o.scale();
BigInteger bigint = JavaSystem.getUnscaledValue(o);
byte[] bytearr = bigint.toByteArray();
writeByteArray(bytearr);
writeInt(scale);
}
示例14: extractBytes
import java.math.BigInteger; //导入方法依赖的package包/类
private void extractBytes(byte[] encKey, int offSet, BigInteger bI)
{
byte[] val = bI.toByteArray();
if (val.length < 32)
{
byte[] tmp = new byte[32];
System.arraycopy(val, 0, tmp, tmp.length - val.length, val.length);
val = tmp;
}
for (int i = 0; i != 32; i++)
{
encKey[offSet + i] = val[val.length - 1 - i];
}
}
示例15: encrypt
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Perform actual encryption ,creates single array and updates the result
* after the encryption.
*
* @param input
* - the input in bytes
* @param inputOffset
* - the offset in input where the input starts always zero
* @param inputLenth
* - the input length
* @param output
* - the buffer for the result
* @param outputOffset
* - the offset in output where the result is stored
* @return the number of bytes stored in output
* @throws Exception
* throws if Plaintext m is not in Z_n , m should be less then n
*/
protected final int encrypt(byte[] input, int inputOffset, int inputLenth,
byte[] output, int outputOffset) throws Exception {
byte[] messageBytes = new byte[plaintextSize];
int inLenth = Math.min(plaintextSize, inputLenth);
System.arraycopy(input, inputOffset, messageBytes, 0, inLenth);
BigInteger m = new BigInteger(input);
// get the public key in order to encrypt
PaillierPublicKey key = (PaillierPublicKey) keyPaillier;
BigInteger g = key.getG();
BigInteger n = key.getN();
BigInteger nsquare = key.getNSquare();
BigInteger r = key.generateRandomRinZn(n,SECURE_RANDOM);
if (m.compareTo(BigInteger.ZERO) < 0 || m.compareTo(n) >= 0) {
throw new Exception(
"PaillierHomomorphicCipher.encryptBlock :Plaintext m is not in Z_n , m should be less then n");
}
BigInteger c = (g.modPow(m, nsquare).multiply(r.modPow(n, nsquare)))
.mod(nsquare);
byte[] cBytes = c.toByteArray();
System.arraycopy(cBytes, 0, output,ciphertextSize
- cBytes.length, cBytes.length);
return ciphertextSize;
}