Example usage for java.math BigInteger valueOf

List of usage examples for java.math BigInteger valueOf

Introduction

In this page you can find the example usage for java.math BigInteger valueOf.

Prototype

private static BigInteger valueOf(int val[]) 

Source Link

Document

Returns a BigInteger with the given two's complement representation.

Usage

From source file:Main.java

private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) {
    BigInteger beta = null;//from   ww  w.j a va2s. com
    if (p.mod(BigInteger.valueOf(4)).compareTo(BigInteger.valueOf(3)) == 0) {
        BigInteger k = p.shiftRight(2).add(ONE);
        beta = alpha.modPow(k, p);
    } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(5)) == 0) {
        System.out.println("p = 8 mod 5");
        BigInteger k = p.subtract(BigInteger.valueOf(5)).divide(BigInteger.valueOf(8));
        BigInteger gamma = alpha.multiply(BigInteger.valueOf(2)).modPow(k, p);
        BigInteger i = alpha.multiply(BigInteger.valueOf(2)).multiply(gamma.pow(2)).mod(p);
        beta = alpha.multiply(gamma).multiply(i.subtract(ONE)).mod(p);
    } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(1)) == 0) {
        beta = null;
        //TODO
        System.out.println("finding square root not fully implemented yet");
    }
    return beta;
}

From source file:Main.java

public static BigInteger byteArrayToBigInteger(byte[] b, int offset, int length) {
    if (b.length < offset + length) {
        throw new IllegalArgumentException("offset + length must be less than or equal to b.length");
    }/*from  ww w . j  av  a2 s.  co  m*/

    BigInteger value = BigInteger.valueOf(0);
    for (int i = 0; i < length; i++) {
        value = value.shiftLeft(8);
        value = value.add(BigInteger.valueOf(b[i + offset] & 0x000000ff));
    }
    return value;
}

From source file:Main.java

public static final BigInteger castToBigInteger(Object paramObject) {
    if (paramObject == null) {
        return null;
    }// w ww . jav a 2  s .  c  o m
    if ((paramObject instanceof BigInteger)) {
        return (BigInteger) paramObject;
    }
    if (((paramObject instanceof Float)) || ((paramObject instanceof Double))) {
        return BigInteger.valueOf(((Number) paramObject).longValue());
    }
    String str = paramObject.toString();
    if (str.length() == 0) {
        return null;
    }
    return new BigInteger(str);
}

From source file:Main.java

/**
 * Converts a BigInteger array into an integer array, reducing all
 * BigIntegers mod q./*from   ww  w  .  j  a v a2  s.c  o m*/
 *
 * @param q     -
 *              the modulus
 * @param input -
 *              the BigInteger array
 * @return the integer array
 */
public static int[] toIntArrayModQ(int q, BigInteger[] input) {
    BigInteger bq = BigInteger.valueOf(q);
    int[] result = new int[input.length];
    for (int i = 0; i < input.length; i++) {
        result[i] = input[i].mod(bq).intValue();
    }
    return result;
}

From source file:Main.java

public static BigInteger hashFnv1A(byte[] data) {
    final BigInteger INIT64 = new BigInteger("cbf29ce484222325", 16);
    final BigInteger PRIME64 = new BigInteger("100000001b3", 16);
    final BigInteger MOD64 = new BigInteger("2").pow(64);

    BigInteger hash = INIT64;//from ww w. j ava 2s  .  co m

    for (byte b : data) {
        hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
        hash = hash.multiply(PRIME64).mod(MOD64);
    }

    return hash;
}

From source file:Main.java

public static Date toDate(Instant instant) {
    BigInteger milis = BigInteger.valueOf(instant.getEpochSecond()).multiply(BigInteger.valueOf(1000));
    milis = milis.add(BigInteger.valueOf(instant.getNano()).divide(BigInteger.valueOf(1_000_000)));
    return new Date(milis.longValue());
}

From source file:Main.java

/**
 * @param value A number/* ww  w .ja va  2s  .com*/
 *
 * @return The number as a {@link BigInteger}.
 */
public static BigInteger asBigInteger(Number number) {
    if (number == null) {
        return null;
    }

    if (number instanceof BigInteger) {
        return (BigInteger) number;
    }

    return BigInteger.valueOf(number.longValue());
}

From source file:Main.java

public static boolean isGoodPrime(byte[] prime, int g) {
    if (!(g >= 2 && g <= 7)) {
        return false;
    }/*ww  w .  ja  va  2s .  c  o m*/

    if (prime.length != 256 || prime[0] >= 0) {
        return false;
    }

    BigInteger dhBI = new BigInteger(1, prime);

    if (g == 2) { // p mod 8 = 7 for g = 2;
        BigInteger res = dhBI.mod(BigInteger.valueOf(8));
        if (res.intValue() != 7) {
            return false;
        }
    } else if (g == 3) { // p mod 3 = 2 for g = 3;
        BigInteger res = dhBI.mod(BigInteger.valueOf(3));
        if (res.intValue() != 2) {
            return false;
        }
    } else if (g == 5) { // p mod 5 = 1 or 4 for g = 5;
        BigInteger res = dhBI.mod(BigInteger.valueOf(5));
        int val = res.intValue();
        if (val != 1 && val != 4) {
            return false;
        }
    } else if (g == 6) { // p mod 24 = 19 or 23 for g = 6;
        BigInteger res = dhBI.mod(BigInteger.valueOf(24));
        int val = res.intValue();
        if (val != 19 && val != 23) {
            return false;
        }
    } else if (g == 7) { // p mod 7 = 3, 5 or 6 for g = 7.
        BigInteger res = dhBI.mod(BigInteger.valueOf(7));
        int val = res.intValue();
        if (val != 3 && val != 5 && val != 6) {
            return false;
        }
    }

    String hex = bytesToHex(prime);
    if (hex.equals(
            "C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C3720FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F642477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B")) {
        return true;
    }

    BigInteger dhBI2 = dhBI.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(2));
    return !(!dhBI.isProbablePrime(30) || !dhBI2.isProbablePrime(30));
}

From source file:Main.java

private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) {
    final BigInteger ONE = BigInteger.ONE;
    final BigInteger TWO = BigInteger.valueOf(2);
    // Find a and m such that m is odd and this == 1 + 2**a * m
    BigInteger thisMinusOne = us.subtract(ONE);
    BigInteger m = thisMinusOne;//from w w  w . j a  v  a2s . c  o m
    int a = m.getLowestSetBit();
    m = m.shiftRight(a);

    // Do the tests
    if (rnd == null) {
        rnd = new SecureRandom();
    }
    for (int i = 0; i < iterations; i++) {
        // Generate a uniform random on (1, this)
        BigInteger b;
        do {
            b = new BigInteger(us.bitLength(), rnd);
        } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0);

        int j = 0;
        BigInteger z = b.modPow(m, us);
        while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
            if (j > 0 && z.equals(ONE) || ++j == a)
                return false;
            z = z.modPow(TWO, us);
        }
    }
    return true;
}

From source file:MainClass.java

public static X509Certificate generateV3Certificate(KeyPair pair)
        throws InvalidKeyException, NoSuchProviderException, SignatureException {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();

    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis() - 10000));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + 10000));
    certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

    certGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false));
    certGen.addExtension(X509Extensions.KeyUsage, true,
            new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
    certGen.addExtension(X509Extensions.ExtendedKeyUsage, true,
            new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));

    certGen.addExtension(X509Extensions.SubjectAlternativeName, false,
            new GeneralNames(new GeneralName(GeneralName.rfc822Name, "test@test.test")));

    return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}