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.stickyd.services.impl.UserLoginServiceImpl.java

public static String md5(String s) {
    MessageDigest md5;/*from   ww  w.j  av  a2s.  c o  m*/
    try {
        md5 = MessageDigest.getInstance("MD5");
        md5.update(s.getBytes());
        return new BigInteger(1, md5.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.gmu.uav.RadioSecurity.java

public static String toHex(byte[] array) {
    BigInteger bi = new BigInteger(1, array);
    String hex = bi.toString(16);
    int paddingLength = (array.length * 2) - hex.length();
    if (paddingLength > 0)
        return String.format("%0" + paddingLength + "d", 0) + hex;
    else/*from   ww  w  .  j  a  v a2  s.  c o  m*/
        return hex;
}

From source file:com.hobba.hobaserver.services.security.ChallengeUtil.java

public String getChallenge(String kid, long expirationTime) {
    HobaKeys hk = new HobaKeys();
    HobaKeysFacadeREST hkfrest = new HobaKeysFacadeREST();
    HobaChallengesFacadeREST hcfrest = new HobaChallengesFacadeREST();
    hk = hkfrest.findHKIDbyKID(kid);//from  www .j  av  a  2  s .c om

    SecureRandom random = new SecureRandom();
    String rand = new BigInteger(130, random).toString(32);

    HobaChallenges hc = new HobaChallenges();

    hc.setIdKeys(hk);
    hc.setChalenge(rand);
    if (expirationTime > 0) {
        Date date = new Date(new Date().getTime() + (expirationTime * 100));
        hc.setExpiration(date);
    }
    hc.setExpiration(null);
    hc.setIsValid(Boolean.TRUE);
    hcfrest.create(hc);

    return new String(Base64.encodeBase64(rand.getBytes()));
}

From source file:fr.bmartel.speedtest.test.SpeedTestServerTest.java

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

From source file:crow.util.Util.java

/**
 *  {@code md5}/*from   w ww.j  a  v a 2 s . c om*/
 * 
 * @param str
 *             {@code md5}
 * @return md5() null str md5?
 */
public static String md5(String str) {
    MessageDigest messageDigest = null;
    String digest = null;
    if ((str != null) && (str.length() > 0)) {
        try {
            (messageDigest = MessageDigest.getInstance("MD5")).update(str.getBytes());
            digest = String.format("%032X", new Object[] { new BigInteger(1, (messageDigest).digest()) });
        } catch (Exception e) {
            digest = str.substring(0, 32);
        }
    }
    return digest;
}

From source file:com.github.robozonky.integrations.zonkoid.ZonkoidConfirmationProvider.java

static String md5(final String secret) throws NoSuchAlgorithmException {
    final MessageDigest mdEnc = MessageDigest.getInstance("MD5");
    mdEnc.update(secret.getBytes(Defaults.CHARSET));
    return new BigInteger(1, mdEnc.digest()).toString(16);
}

From source file:com.github.feribg.audiogetter.helpers.Utils.java

public static String calculateMD5(File file) {
    MessageDigest digest;/*w  w  w  . j  a v a 2 s . c  o m*/
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.e(App.TAG, "Exception while getting Digest", e);
        return null;
    }

    InputStream is;
    try {
        is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        Log.e(App.TAG, "Exception while getting FileInputStream", e);
        return null;
    }

    byte[] buffer = new byte[8192];
    int read;
    try {
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        String output = bigInt.toString(16);
        // Fill to 32 chars
        output = String.format("%32s", output).replace(' ', '0');
        return output;
    } catch (IOException e) {
        //throw new RuntimeException("Unable to process file for MD5", e);
        Log.e(App.TAG, "Unable to process file for MD5", e); //TODO check if actually avoid FC
        return "00000000000000000000000000000000"; // fictional bad MD5: needed without "throw new RuntimeException"
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(App.TAG, "Exception on closing MD5 input stream", e);
        }
    }
}

From source file:org.openlmis.fulfillment.domain.Base36EncodedOrderNumberGenerator.java

/**
 * Generates unique number for given order.
 *//*www. j  a va 2 s. c om*/
public String generate(Order order) {
    String id = order.getExternalId().toString();
    String base36Id = new BigInteger(id.replace("-", ""), 16).toString(36).toUpperCase();
    return base36Id.substring(0, 8);
}

From source file:business.security.SecureTokenGenerator.java

/**
 * Generates a password that is valid for the {@link PasswordValidator}.
 *
 * Rationale behind this function://from w w w. ja  va 2s  . c  om
 * - Generate a long enough secure pseudo random string that contains at
 *   least a number, a letter and a 'special character'.
 * - The number, letter and special character are explicitely chosen
 *   (one letter, one special character and a random number between 0 and 1024).
 * - A long enough secure random string is generated by using the
 *   {@link SecureRandom} generator and {@link BigInteger} to generate a
 *   very large random number (in base 32 encoding). Because theoretically
 *   this could be not long enough (in number of characters), the
 *   {@link BCryptPasswordEncoder} is applied to that number as well.
 * - All these values are concatenated.
 *
 * Note: for the alphanumerical part, probably {@link UUID.randomUUID()}
 * would suffice as well.
 *
 * @return the password.
 */
public static String generatePassword() {
    String special = specials[rng.nextInt(specials.length)];
    String letter = letters[rng.nextInt(letters.length)];
    String number = new BigInteger(10, rng).toString(10);
    String alphanumerical = passwordEncoder.encode(generateToken());
    return alphanumerical + special + letter + number;
}

From source file:com.chumbok.aauth.otp.TOTP.java

/**
 * This method converts a HEX string to Byte[]
 *
 * @param hex/*w  w  w  . jav  a  2  s  . co  m*/
 *            : the HEX string
 *
 * @return: a byte array
 */

private static byte[] hexStr2Bytes(String hex) {
    // Adding one byte to get the right conversion
    // Values starting with "0" can be converted
    byte[] bArray = new BigInteger("10" + hex, 16).toByteArray();

    // Copy all the REAL bytes, not the "first"
    byte[] ret = new byte[bArray.length - 1];
    for (int i = 0; i < ret.length; i++)
        ret[i] = bArray[i + 1];
    return ret;
}