public void setCumulativeGas(long cumulativeGas) { this.cumulativeGas = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(cumulativeGas)); rlpEncoded = null; }
/** * Generate from the exponents. */ public static RSAKeyPair fromExponents(byte[] e, byte[] p, byte[] q) { final BigInteger eInt = BigIntegers.fromUnsignedByteArray(e), pInt = BigIntegers.fromUnsignedByteArray(p), qInt = BigIntegers.fromUnsignedByteArray(q), nInt = pInt.multiply(qInt), mInt = pInt.subtract(BigInteger.ONE).multiply(qInt.subtract(BigInteger.ONE)), dInt = eInt.modInverse(mInt), dPInt = dInt.remainder(pInt.subtract(BigInteger.ONE)), dQInt = dInt.remainder(qInt.subtract(BigInteger.ONE)), qInvInt = qInt.modInverse(pInt); final byte[] n = BigIntegers.asUnsignedByteArray(nInt), d = BigIntegers.asUnsignedByteArray(dInt), dP = BigIntegers.asUnsignedByteArray(dPInt), dQ = BigIntegers.asUnsignedByteArray(dQInt), qInv = BigIntegers.asUnsignedByteArray(qInvInt); return new RSAKeyPair(n, e, d, p, q, dP, dQ, qInv); } }
/** * Return a value that can be used as x1 or x3 during round 1. * <p> * The returned value is a random value in the range <tt>[0, q-1]</tt>. */ public static BigInteger generateX1( BigInteger q, SecureRandom random) { BigInteger min = ZERO; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
/** * Hidden (package-private) initializer, for internal/unittest usage. */ void init(byte[] e, byte[] p, byte[] q, HashType oaepHashType, HashType mgf1HashType, SecureRandom rng) { final RSAKeyPair keyPair = RSAKeyPair.fromExponents(e, p, q); final RSAPrivateCrtKeyParameters privParameters = new RSAPrivateCrtKeyParameters( BigIntegers.fromUnsignedByteArray(keyPair.n), BigIntegers.fromUnsignedByteArray(keyPair.e), BigIntegers.fromUnsignedByteArray(keyPair.d), BigIntegers.fromUnsignedByteArray(keyPair.p), BigIntegers.fromUnsignedByteArray(keyPair.q), BigIntegers.fromUnsignedByteArray(keyPair.dP), BigIntegers.fromUnsignedByteArray(keyPair.dQ), BigIntegers.fromUnsignedByteArray(keyPair.qInv)); final AsymmetricBlockCipher decryptor = makeDecryptor(mgf1HashType); // Private key goes together with its public key. final RSAOAEPPublicKey publicKey = new RSAOAEPPublicKey(); publicKey.init(keyPair.n, keyPair.e, oaepHashType, mgf1HashType, rng); state = new RSAOAEPPrivateKey.State(decryptor, privParameters, publicKey, oaepHashType, mgf1HashType, rng); resetDecryptor(); }
/** * Generate a new key pair, with all options specified. * * @param bitStrength bit strength of the key, e.g. 2048 * @param e RSA public exponent * @param certainty RSA key generation certainty * @param mgf1HashType The type of the hash(digest) function used for OAEP MGF1 hash generation. */ public void generate(int bitStrength, byte[] e, int certainty, HashType oaepHashType, HashType mgf1HashType) { final RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator(); keyGen.init(new RSAKeyGenerationParameters( BigIntegers.fromUnsignedByteArray(e), new SecureRandom(), bitStrength, certainty)); final AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair(); final RSAPrivateCrtKeyParameters privateKey = (RSAPrivateCrtKeyParameters) keyPair.getPrivate(); if (mgf1HashType == null) { mgf1HashType = DEFAULT_MGF1_HASH; } // Don't worry we are passing thread-unsafe hash and mgf1Hash Digest instances: // init() will clone them anyway. init(e, BigIntegers.asUnsignedByteArray(privateKey.getP()), BigIntegers.asUnsignedByteArray(privateKey.getQ()), oaepHashType, mgf1HashType, new SecureRandom()); }
private static BigInteger generatePrivateKey(BigInteger q, SecureRandom random) { // TODO Prefer this method? (change test cases that used fixed random) // B.1.1 Key Pair Generation Using Extra Random Bits // BigInteger c = new BigInteger(q.bitLength() + 64, random); // return c.mod(q.subtract(ONE)).add(ONE); // B.1.2 Key Pair Generation by Testing Candidates return BigIntegers.createRandomInRange(ONE, q.subtract(ONE), random); }
/** * Hidden (package-private) initializer, for internal/unittest usage. */ void init(byte[] n, byte[] e, HashType oaepHashType, HashType mgf1HashType, SecureRandom rng) { final RSAKeyParameters pubParameters = new RSAKeyParameters( false, BigIntegers.fromUnsignedByteArray(n), BigIntegers.fromUnsignedByteArray(e)); state = new State(makeEncryptor(mgf1HashType), pubParameters, oaepHashType, mgf1HashType, rng); resetEncryptor(); }
public void setGasUsed(long gasUsed) { this.gasUsed = BigIntegers.asUnsignedByteArray(BigInteger.valueOf(gasUsed)); rlpEncoded = null; }
private BigInteger generateRandomElement(BigInteger p, SecureRandom random) { return BigIntegers.createRandomInRange(ONE, p.subtract(ONE), random); }
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); }
/** * Return a value that can be used as x2 or x4 during round 1. * <p> * The returned value is a random value in the range <tt>[1, q-1]</tt>. */ public static BigInteger generateX2( BigInteger q, SecureRandom random) { BigInteger min = ONE; BigInteger max = q.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
/** * @deprecated use {@link Transaction#create(String, BigInteger, BigInteger, BigInteger, BigInteger, Integer)} instead */ public static Transaction create(String to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit){ return new Transaction(BigIntegers.asUnsignedByteArray(nonce), BigIntegers.asUnsignedByteArray(gasPrice), BigIntegers.asUnsignedByteArray(gasLimit), Hex.decode(to), BigIntegers.asUnsignedByteArray(amount), null); }
private BigInteger generateRandomElement(BigInteger p, SecureRandom random) { return BigIntegers.createRandomInRange(ONE, p.subtract(ONE), random); }
public byte[] getPowBoundary() { return BigIntegers.asUnsignedByteArray(32, BigInteger.ONE.shiftLeft(256).divide(getDifficultyBI())); }
public static BigInteger generatePrivateValue(Digest digest, BigInteger N, BigInteger g, SecureRandom random) { int minBits = Math.min(256, N.bitLength() / 2); BigInteger min = ONE.shiftLeft(minBits - 1); BigInteger max = N.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
private static byte[] toBytes(Object input) { if (input instanceof byte[]) { return (byte[]) input; } else if (input instanceof String) { String inputString = (String) input; return inputString.getBytes(); } else if (input instanceof Long) { Long inputLong = (Long) input; return (inputLong == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputLong)); } else if (input instanceof Integer) { Integer inputInt = (Integer) input; return (inputInt == 0) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(BigInteger.valueOf(inputInt)); } else if (input instanceof BigInteger) { BigInteger inputBigInt = (BigInteger) input; return (inputBigInt.equals(BigInteger.ZERO)) ? ByteUtil.EMPTY_BYTE_ARRAY : asUnsignedByteArray(inputBigInt); } else if (input instanceof Value) { Value val = (Value) input; return toBytes(val.asObj()); } throw new RuntimeException("Unsupported type: Only accepting String, Integer and BigInteger for now"); }
public static BigInteger generatePrivateValue(BigInteger N, BigInteger g, SecureRandom random) { int minBits = Math.min(256, N.bitLength() / 2); BigInteger min = ONE.shiftLeft(minBits - 1); BigInteger max = N.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
public static byte[] encodeBigInteger(BigInteger srcBigInteger) { if (srcBigInteger.compareTo(BigInteger.ZERO) < 0) throw new RuntimeException("negative numbers are not allowed"); if (srcBigInteger.equals(BigInteger.ZERO)) return encodeByte((byte) 0); else return encodeElement(asUnsignedByteArray(srcBigInteger)); }
public static BigInteger generatePrivateValue(Digest digest, BigInteger N, BigInteger g, SecureRandom random) { int minBits = Math.min(256, N.bitLength() / 2); BigInteger min = ONE.shiftLeft(minBits - 1); BigInteger max = N.subtract(ONE); return BigIntegers.createRandomInRange(min, max, random); }
private void sign() { flags |= SIGNATURE_FLAG; byte[] forSig = hash(); ECKey.ECDSASignature signature = from.sign(forSig); byte v; if (signature.v == 27) v = 0; else if (signature.v == 28) v = 1; else throw new RuntimeException("Invalid signature: " + signature); this.signature = merge(BigIntegers.asUnsignedByteArray(32, signature.r), BigIntegers.asUnsignedByteArray(32, signature.s), new byte[]{v}); }