Example usage for java.math BigInteger BigInteger

List of usage examples for java.math BigInteger BigInteger

Introduction

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

Prototype

private BigInteger(byte[] magnitude, int signum) 

Source Link

Document

This private constructor is for internal use and assumes that its arguments are correct.

Usage

From source file:com.apexxs.neonblack.utilities.MD5Utilities.java

public static String getMD5Hash(String s) {
    String md5Hash = StringUtils.EMPTY;
    try {//from   w ww .  jav  a  2  s .c o m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        byte[] bytes = md.digest(s.getBytes("UTF-8"));
        md5Hash = String.format("%032x", new BigInteger(1, bytes));
    } catch (Exception ex) {
        logger.error("Error generating MD5 hash from " + s + "\n" + ex.getMessage());
    }
    return md5Hash;
}

From source file:com.odap.server.audit.ConfigHandler.java

private static String nextSessionId() {
    return new BigInteger(130, random).toString(16);
}

From source file:AppPackage.Session.java

public String generateToken() {
    return new BigInteger(130, random).toString(32);
}

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;//  w w  w  .  j  a  v a2 s  .co  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:com.honnix.yaacs.util.MD5.java

/**
 * Get MD5 sum of the input data.// ww  w .j  a  v a2  s. com
 * 
 * @param data
 *            input data used to generate MD5 sum
 * @return MD5 sum of the input data, or null if no MD5 algorithm could be
 *         found
 */
public static String getMD5Sum(String data) {
    String encodedData = null;

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.update(data.getBytes("US-ASCII"), 0, data.length());
        encodedData = new BigInteger(1, messageDigest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
        StringBuilder sb = new StringBuilder("No MD5 algorithm.").append(" Could not apply validation check.");

        LOG.error(sb.toString(), e);
    } catch (UnsupportedEncodingException e) {
        LOG.error("This should not happen anyway.", e);
    }

    return encodedData;
}

From source file:ke.co.tawi.babblesms.server.utils.security.SecurityUtil.java

/**
 * Return the MD5 hahs of a String. It will work correctly for most 
 * strings. A word which does not work correctly is "michael" (check 
 * against online MD5 hash tools).   //from   ww w. j av a  2  s  .co m
 *
 * @param toHash plain text string to encryption
 * @return an md5 hashed string
 */
public static String getMD5Hash(String toHash) {
    String md5Hash = "";

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(toHash.getBytes(), 0, toHash.length());

        md5Hash = new BigInteger(1, md.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException while getting MD5 hash of '" + toHash + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    return md5Hash;
}

From source file:function.Functions.java

protected static String MD5(String text) {
    String md5Text = "";
    try {/*from ww  w .  ja v a 2s  .  co  m*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        md5Text = new BigInteger(1, digest.digest((text).getBytes())).toString(16);
    } catch (Exception e) {
        System.out.println("Error in call to MD5");
    }

    if (md5Text.length() == 31) {
        md5Text = "0" + md5Text;
    }
    return md5Text;
}

From source file:Main.java

public static String getMd5Digest(String pInput) {
    try {//ww w.  j a  va 2 s  .c  o  m
        MessageDigest lDigest = MessageDigest.getInstance("MD5");
        lDigest.update(getBytes(pInput));
        BigInteger lHashInt = new BigInteger(1, lDigest.digest());
        return String.format("%1$032X", lHashInt);
    } catch (NoSuchAlgorithmException lException) {
        throw new RuntimeException(lException);
    }
}

From source file:com.l2jfree.util.HexUtil.java

/**
 * Transforms a legacy HexID into a byte array.
 * /*  w  w  w . j a  va2 s. co  m*/
 * @param string HexID
 * @return decoded byte array
 */
public static byte[] stringToHex(String string) {
    return new BigInteger(string, 16).toByteArray();
}

From source file:Main.java

/**
 * Utility method to check validity of the provided code. The version must be base 36 encoded.
 *
 * @param code the code string to check.
 * @param expectedLength the length the code must be.
 * @param codeVersionStartIndex the start index (INCLUSIVE) to get the code version from.
 * @param codeVersionEndIndex the end index (EXCLUSIVE) to get the code version from.
 * @param expectedVersion the version code that the code's version should be checked against.
 * @return true if the code passes the expected length and version checks.
 *///  ww w  .  j ava  2 s.  c o m
public static boolean isValidCode(@NonNull final String code, final int expectedLength,
        final int codeVersionStartIndex, final int codeVersionEndIndex, final int expectedVersion) {
    boolean isValid = true;

    if (code.length() != expectedLength) {
        isValid = false;
    } else {
        final String versionCode = code.substring(codeVersionStartIndex, codeVersionEndIndex);
        final BigInteger i = new BigInteger(versionCode, Character.MAX_RADIX);

        if (i.intValue() != expectedVersion) {
            isValid = false;
        }
    }

    return isValid;
}