Java Code Examples for org.bouncycastle.util.BigIntegers

Following code examples demonstrate how to use org.bouncycastle.util.BigIntegersfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.bouncycastle.util.BigIntegersand various code implementation of this class.

    public static BigInteger calculateHash(BigInteger ... bigIntegers) {
        Digest digest = new KeccakDigest(256);
        DigestUtil.update(digest, bigIntegers);

        byte[] output = new byte[digest.getDigestSize()];
        digest.doFinal(output, 0);
        return BigIntegers.fromUnsignedByteArray(output);
    } 


    private static void update(Digest digest, BigInteger ... bigIntegers) {
        for (BigInteger bigInt : bigIntegers) {
            int lengthInBytes = BigIntUtil.roundUpToMultiple(bigInt.bitLength(), 256) / 8;
            byte[] encodedBigInt = BigIntegers.asUnsignedByteArray(lengthInBytes, bigInt);
            update(digest, encodedBigInt);
            Arrays.fill(encodedBigInt, (byte) 0);
        }
    } 

    }

    public BigInteger nextK()
    {
        int qBitLength = q.bitLength();

        BigInteger k;
        do
        {
            k = BigIntegers.createRandomBigInteger(qBitLength, random);
        }
        while (k.equals(ZERO) || k.compareTo(q) >= 0);

        return k;
    } 

    }

    BigInteger calculatePrivate(DHParameters dhParams, SecureRandom random)
    {
        BigInteger p = dhParams.getP();
        int limit = dhParams.getL();

        if (limit != 0)
        {
            return new BigInteger(limit, random).setBit(limit - 1);
        }

        BigInteger min = TWO;
        int m = dhParams.getM();
        if (m != 0)
        {
            min = ONE.shiftLeft(m - 1);
        }

        BigInteger max = p.subtract(TWO);
        BigInteger q = dhParams.getQ();
        if (q != null)
        {
            max = q.subtract(TWO);
        }

        return BigIntegers.createRandomInRange(min, max, random);
    } 

    }

    static BigInteger selectGenerator(BigInteger biginteger, BigInteger biginteger1, SecureRandom securerandom)
    {
        biginteger1 = biginteger.subtract(TWO);
        BigInteger biginteger2;
        do
        {
            biginteger2 = BigIntegers.createRandomInRange(TWO, biginteger1, securerandom).modPow(TWO, biginteger);
        } while (biginteger2.equals(ONE));
        return biginteger2;
    } 

Advertisement
Javadoc
BigInteger utilities.
Advertisement