本文整理汇总了Java中org.spongycastle.util.BigIntegers.asUnsignedByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java BigIntegers.asUnsignedByteArray方法的具体用法?Java BigIntegers.asUnsignedByteArray怎么用?Java BigIntegers.asUnsignedByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.spongycastle.util.BigIntegers
的用法示例。
在下文中一共展示了BigIntegers.asUnsignedByteArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Env
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public Env(JSONObject env) {
String coinbase = env.get("currentCoinbase").toString();
String difficulty = env.get("currentDifficulty").toString();
String timestamp = env.get("currentTimestamp").toString();
String number = env.get("currentNumber").toString();
String gasLimit = Utils.parseUnidentifiedBase(env.get("currentGasLimit").toString());
Object previousHash = env.get("previousHash");
String prevHash = previousHash == null ? "" : previousHash.toString();
this.currentCoinbase = Utils.parseData(coinbase);
this.currentDifficulty = BigIntegers.asUnsignedByteArray(TestCase.toBigInt(difficulty) );
this.currentGasLimit = BigIntegers.asUnsignedByteArray(TestCase.toBigInt(gasLimit));
this.currentNumber = TestCase.toBigInt(number).toByteArray();
this.currentTimestamp = TestCase.toBigInt(timestamp).toByteArray();
this.previousHash = Utils.parseData(prevHash);
}
示例2: validate
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
private void validate() {
if (getNonce().length > HASH_LENGTH) throw new RuntimeException("Nonce is not valid");
if (receiveAddress != null && receiveAddress.length != 0 && receiveAddress.length != ADDRESS_LENGTH)
throw new RuntimeException("Receive address is not valid");
if (gasLimit.length > HASH_LENGTH)
throw new RuntimeException("Gas Limit is not valid");
if (gasPrice != null && gasPrice.length > HASH_LENGTH)
throw new RuntimeException("Gas Price is not valid");
if (value != null && value.length > HASH_LENGTH)
throw new RuntimeException("Value is not valid");
if (getSignature() != null) {
if (BigIntegers.asUnsignedByteArray(signature.r).length > HASH_LENGTH)
throw new RuntimeException("Signature R is not valid");
if (BigIntegers.asUnsignedByteArray(signature.s).length > HASH_LENGTH)
throw new RuntimeException("Signature S is not valid");
if (getSender() != null && getSender().length != ADDRESS_LENGTH)
throw new RuntimeException("Sender is not valid");
}
}
示例3: Env
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public Env(JSONObject env) {
String coinbase = env.get("currentCoinbase").toString();
String difficulty = env.get("currentDifficulty").toString();
String timestamp = env.get("currentTimestamp").toString();
String number = env.get("currentNumber").toString();
String gasLimit = Utils.parseUnidentifiedBase(env.get("currentGasLimit").toString());
Object previousHash = env.get("previousHash");
String prevHash = previousHash == null ? "" : previousHash.toString();
this.currentCoinbase = Hex.decode(coinbase);
this.currentDifficulty = BigIntegers.asUnsignedByteArray(TestCase.toBigInt(difficulty) );
this.currentGasLimit = BigIntegers.asUnsignedByteArray(TestCase.toBigInt(gasLimit));
this.currentNumber = TestCase.toBigInt(number).toByteArray();
this.currentTimestamp = TestCase.toBigInt(timestamp).toByteArray();
this.previousHash = Hex.decode(prevHash);
}
示例4: decryptAES
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Decrypt cipher by AES in SIC(also know as CTR) mode
*
* @param cipher -proper cipher
* @return decrypted cipher, equal length to the cipher.
* @deprecated should not use EC private scalar value as an AES key
*/
public byte[] decryptAES(byte[] cipher){
if (privKey == null) {
throw new MissingPrivateKeyException();
}
if (!(privKey instanceof BCECPrivateKey)) {
throw new UnsupportedOperationException("Cannot use the private key as an AES key");
}
AESFastEngine engine = new AESFastEngine();
SICBlockCipher ctrEngine = new SICBlockCipher(engine);
KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
ParametersWithIV params = new ParametersWithIV(key, new byte[16]);
ctrEngine.init(false, params);
int i = 0;
byte[] out = new byte[cipher.length];
while(i < cipher.length){
ctrEngine.processBlock(cipher, i, out, i);
i += engine.getBlockSize();
if (cipher.length - i < engine.getBlockSize())
break;
}
// process left bytes
if (cipher.length - i > 0){
byte[] tmpBlock = new byte[16];
System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
}
return out;
}
示例5: create
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public static Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, Integer chainId){
return new Transaction(BigIntegers.asUnsignedByteArray(nonce),
BigIntegers.asUnsignedByteArray(gasPrice),
BigIntegers.asUnsignedByteArray(gasLimit),
Hex.decode(to),
BigIntegers.asUnsignedByteArray(amount),
null,
chainId);
}
示例6: calcDistance
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public byte[] calcDistance(Peer toPeer) {
BigInteger aPeer = new BigInteger(getId());
BigInteger bPeer = new BigInteger(toPeer.getId());
BigInteger distance = aPeer.xor(bPeer);
return BigIntegers.asUnsignedByteArray(distance);
}
示例7: mine
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
/**
* Adds a nonce to given block which complies with the given difficulty
*
* For the PoC series, we use a simplified proof-of-work.
* This is not ASIC resistant and is meant merely as a placeholder.
* It utilizes the bare SHA3 hash function to secure the block chain by requiring
* the SHA3 hash of the concatenation of the nonce and the header’s SHA3 hash to be
* sufficiently low. It is formally defined as PoW:
*
* PoW(H, n) ≡ BE(SHA3(SHA3(RLP(H!n)) ◦ n))
*
* where:
* RLP(H!n) is the RLP encoding of the block header H, not including the
* final nonce component;
* SHA3 is the SHA3 hash function accepting an arbitrary length series of
* bytes and evaluating to a series of 32 bytes (i.e. 256-bit);
* n is the nonce, a series of 32 bytes;
* o is the series concatenation operator;
* BE(X) evaluates to the value equal to X when interpreted as a
* big-endian-encoded integer.
*
* @param newBlock without a valid nonce
* @param difficulty - the mining difficulty
* @return true if valid nonce has been added to the block
*/
public boolean mine(Block newBlock, byte[] difficulty) {
// eval(_root, _nonce) <= (bigint(1) << 256) / _difficulty; }
stop = false;
BigInteger max = BigInteger.valueOf(2).pow(255);
byte[] target = BigIntegers.asUnsignedByteArray(32,
max.divide(new BigInteger(1, difficulty)));
long newGasLimit = Math.max(125000,
(new BigInteger(1, newBlock.getGasLimit()).longValue() * (1024 - 1) + (newBlock.getGasUsed() * 6 / 5)) / 1024);
newBlock.getHeader().setGasLimit(BigInteger.valueOf(newGasLimit).toByteArray());
byte[] hash = sha3(newBlock.getEncodedWithoutNonce());
byte[] testNonce = new byte[32];
byte[] concat;
while (ByteUtil.increment(testNonce) && !stop) {
if (testNonce[31] == 0 && testNonce[30] == 0) {
System.out.println("mining: " + new BigInteger(1, testNonce));
}
if (testNonce[31] == 0)
sleep();
concat = Arrays.concatenate(hash, testNonce);
byte[] result = sha3(concat);
if (FastByteComparisons.compareTo(result, 0, 32, target, 0, 32) < 0) {
newBlock.setNonce(testNonce);
return true;
}
}
return false; // couldn't find a valid nonce
}
示例8: getRandomAltChain
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public static List<Block> getRandomAltChain(byte[] startParentHash, long startNumber, long length, int maxHeight){
List<Block> result = new ArrayList<>();
List<byte[]> lastHashes = new ArrayList<>();
lastHashes.add(startParentHash);
long lastIndex = startNumber;
Random rnd = new Random();
for (int i = 0; i < length; ++i){
List<byte[]> currentHashes = new ArrayList<>();
int curMaxHeight = maxHeight;
if (i == 0) curMaxHeight = 1;
for (int j = 0; j < curMaxHeight; ++j){
byte[] parentHash = lastHashes.get(rnd.nextInt(lastHashes.size()));
byte[] difficulty = BigIntegers.asUnsignedByteArray(new BigInteger(8, new Random()));
byte[] newHash = randomHash();
Block block = new Block(parentHash, newHash, null, null, difficulty, lastIndex, new byte[]{0}, 0, 0, null, null,
null, null, EMPTY_TRIE_HASH, randomHash(), null, null);
currentHashes.add(block.getHash());
result.add(block);
}
++lastIndex;
lastHashes.clear();
lastHashes.addAll(currentHashes);
}
return result;
}
示例9: create
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public static Transaction create(RskSystemProperties config, String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, byte[] decodedData) {
return new Transaction(BigIntegers.asUnsignedByteArray(nonce),
BigIntegers.asUnsignedByteArray(gasPrice),
BigIntegers.asUnsignedByteArray(gasLimit),
to != null ? Hex.decode(to) : null,
BigIntegers.asUnsignedByteArray(amount),
decodedData,
config.getBlockchainConfig().getCommonConstants().getChainId());
}
示例10: getRandomChain
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public static List<Block> getRandomChain(byte[] startParentHash, long startNumber, long length){
List<Block> result = new ArrayList<>();
byte[] lastHash = startParentHash;
long lastIndex = startNumber;
for (int i = 0; i < length; ++i){
byte[] difficulty = BigIntegers.asUnsignedByteArray(new BigInteger(8, new Random()));
byte[] newHash = randomHash();
Block block = new Block(lastHash, newHash, null, null, difficulty, lastIndex, new byte[] {0}, 0, 0, null, null,
null, null, EMPTY_TRIE_HASH, randomHash(), null, null);
++lastIndex;
lastHash = block.getHash();
result.add(block);
}
return result;
}
示例11: getRandomChain
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public static List<Block> getRandomChain(byte[] startParentHash, long startNumber, long length){
List<Block> result = new ArrayList<>();
byte[] lastHash = startParentHash;
long lastIndex = startNumber;
for (int i = 0; i < length; ++i){
byte[] difficutly = BigIntegers.asUnsignedByteArray(new BigInteger(8, new Random()));
byte[] newHash = randomHash();
Block block = new Block(lastHash, newHash, null, null, difficutly, lastIndex, new byte[] {0}, 0, 0, null, null,
null, null, EMPTY_TRIE_HASH, randomHash(), null, null, null, BigInteger.ZERO);
++lastIndex;
lastHash = block.getHash();
result.add(block);
}
return result;
}
示例12: setCumulativeGas
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public void setCumulativeGas(long cumulativeGas) {
this.cumulativeGas = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(cumulativeGas));
rlpEncoded = null;
}
示例13: processBlock
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public byte[] processBlock(
byte[] in,
int inOff,
int inLen,
byte[] macData)
throws InvalidCipherTextException
{
if (forEncryption)
{
if (keyPairGenerator != null)
{
EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
this.privParam = ephKeyPair.getKeyPair().getPrivate();
this.V = ephKeyPair.getEncodedPublicKey();
}
}
else
{
if (keyParser != null)
{
ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
try
{
this.pubParam = keyParser.readKey(bIn);
}
catch (IOException e)
{
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
}
int encLength = (inLen - bIn.available());
this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
}
}
// Compute the common value and convert to byte array.
agree.init(privParam);
BigInteger z = agree.calculateAgreement(pubParam);
byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
// Create input to KDF.
byte[] VZ;
// if (V.length != 0)
// {
// VZ = new byte[V.length + Z.length];
// System.arraycopy(V, 0, VZ, 0, V.length);
// System.arraycopy(Z, 0, VZ, V.length, Z.length);
// }
// else
{
VZ = Z;
}
// Initialise the KDF.
DerivationParameters kdfParam;
if (kdf instanceof MGF1BytesGeneratorExt) {
kdfParam = new MGFParameters(VZ);
} else {
kdfParam = new KDFParameters(VZ, param.getDerivationV());
}
kdf.init(kdfParam);
return forEncryption
? encryptBlock(in, inOff, inLen, macData)
: decryptBlock(in, inOff, inLen, macData);
}
示例14: setGasUsed
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
public void setGasUsed(long gasUsed) {
this.gasUsed = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(gasUsed));
}
示例15: testTransactionCreateContract
import org.spongycastle.util.BigIntegers; //导入方法依赖的package包/类
@Test
public void testTransactionCreateContract() {
// String rlp =
// "f89f808609184e72a0008203e8808203e8b84b4560005444602054600f60056002600a02010b0d630000001d596002602054630000003b5860066000530860056006600202010a0d6300000036596004604054630000003b5860056060541ca0ddc901d83110ea50bc40803f42083afea1bbd420548f6392a679af8e24b21345a06620b3b512bea5f0a272703e8d6933177c23afc79516fd0ca4a204aa6e34c7e9";
byte[] senderPrivKey = HashUtil.sha3("cow".getBytes());
byte[] nonce = BigIntegers.asUnsignedByteArray(BigInteger.ZERO);
byte[] gasPrice = Hex.decode("09184e72a000"); // 10000000000000
byte[] gas = Hex.decode("03e8"); // 1000
byte[] recieveAddress = null;
byte[] endowment = Hex.decode("03e8"); //10000000000000000"
byte[] init = Hex.decode
("4560005444602054600f60056002600a02010b0d630000001d596002602054630000003b5860066000530860056006600202010a0d6300000036596004604054630000003b586005606054");
Transaction tx1 = new Transaction(nonce, gasPrice, gas,
recieveAddress, endowment, init);
tx1.sign(ECKey.fromPrivate(senderPrivKey));
byte[] payload = tx1.getEncoded();
System.out.println(Hex.toHexString(payload));
Transaction tx2 = new Transaction(payload);
// tx2.getSender();
String plainTx1 = Hex.toHexString(tx1.getEncodedRaw());
String plainTx2 = Hex.toHexString(tx2.getEncodedRaw());
// Transaction tx = new Transaction(Hex.decode(rlp));
System.out.println("tx1.hash: " + Hex.toHexString(tx1.getHash()));
System.out.println("tx2.hash: " + Hex.toHexString(tx2.getHash()));
System.out.println();
System.out.println("plainTx1: " + plainTx1);
System.out.println("plainTx2: " + plainTx2);
System.out.println(Hex.toHexString(tx2.getSender()));
}