Example usage for java.math BigInteger toString

List of usage examples for java.math BigInteger toString

Introduction

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

Prototype

public String toString(int radix) 

Source Link

Document

Returns the String representation of this BigInteger in the given radix.

Usage

From source file:org.opendatakit.briefcase.reused.UncheckedFiles.java

public static Optional<String> getMd5Hash(Path file) {
    try {/*from   ww w .ja  va  2s.c o m*/
        // CTS (6/15/2010) : stream file through digest instead of handing
        // it the
        // byte[]
        MessageDigest md = MessageDigest.getInstance("MD5");
        int chunkSize = 256;

        byte[] chunk = new byte[chunkSize];

        // Get the size of the file
        long lLength = Files.size(file);

        if (lLength > Integer.MAX_VALUE) {
            log.error("File is too large");
            return Optional.empty();
        }

        int length = (int) lLength;

        InputStream is = Files.newInputStream(file);

        int l = 0;
        for (l = 0; l + chunkSize < length; l += chunkSize) {
            is.read(chunk, 0, chunkSize);
            md.update(chunk, 0, chunkSize);
        }

        int remaining = length - l;
        if (remaining > 0) {
            is.read(chunk, 0, remaining);
            md.update(chunk, 0, remaining);
        }
        byte[] messageDigest = md.digest();

        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);
        while (md5.length() < 32)
            md5 = "0" + md5;
        is.close();
        return Optional.of(md5);

    } catch (NoSuchAlgorithmException e) {
        log.error("MD5 calculation failed", e);
        return Optional.empty();
    } catch (FileNotFoundException e) {
        log.error("No File", e);
        return Optional.empty();
    } catch (IOException e) {
        log.error("Problem reading from file", e);
        return Optional.empty();
    }

}

From source file:ID.java

public static String getHexString(byte[] bytes) {
    // This method cannot change even if it's wrong.
    BigInteger bigInteger = BigInteger.ZERO;
    int shift = 0;
    for (int i = bytes.length; --i >= 0;) {
        BigInteger contrib = BigInteger.valueOf(bytes[i] & 0xFF);
        contrib = contrib.shiftLeft(shift);
        bigInteger = bigInteger.add(contrib);
        shift += 8;//from   ww w  .  j a  v  a 2 s.c om
    }
    return bigInteger.toString(16).toUpperCase();
}

From source file:Main.java

public static String getMD5(String str) {
    if (str != null) {
        try {/*w  w w  .  j a v a  2  s  .  c  o m*/
            final BigInteger bigInt;
            if (sMD5Digest == null) {
                sMD5Digest = MessageDigest.getInstance("MD5");
            }
            synchronized (sMD5Digest) {
                sMD5Digest.reset();
                bigInt = new BigInteger(1, sMD5Digest.digest(str.getBytes("UTF-8")));
            }
            String hash = bigInt.toString(16);
            while (hash.length() < 32) {
                hash = "0" + hash;
            }
            return hash;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    return null;
}

From source file:com.board.games.handler.discuz.DiscuzPokerLoginServiceImpl.java

private static synchronized String getMD5(String input) {
    try {/*from  w ww  .  j av a2s  .  c o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes("UTF-8"));
        BigInteger number = new BigInteger(1, messageDigest);
        String hashtext = number.toString(16);
        // Now we need to zero pad it if you actually want the full 32
        // chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:eu.europa.ejusticeportal.dss.applet.model.token.CertificateDisplayUtils.java

private static String formatSerialNumber(BigInteger bi) {
    if (bi == null) {
        return "";
    }/*ww  w . j a  v a  2s .co m*/

    String sn = bi.toString(16);
    char[] chars = sn.toUpperCase().toCharArray();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < chars.length; i++) {
        sb.append(chars[i]);
        if ((i + 1) % 2 == 0 && i < chars.length - 1) {
            sb.append(' ');
        }
    }
    return sb.toString();
}

From source file:com.becapps.easydownloader.utils.Utils.java

public static String calculateMD5(File file) {
    MessageDigest digest;/*w  ww  .jav  a 2s  .  com*/
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.e(DEBUG_TAG, "Exception while getting Digest", e);
        return null;
    }

    InputStream is;
    try {
        is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        Log.e(DEBUG_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(DEBUG_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(DEBUG_TAG, "Exception on closing MD5 input stream", e);
        }
    }
}

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

public static String calculateMD5(File file) {
    MessageDigest digest;/*from w  w w  .j  a  v a  2s  . 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:com.board.games.handler.discuz.DiscuzPokerLoginServiceImpl.java

private static synchronized String getMD5New(String input) {
    // please note that we dont use digest, because if we
    // cannot get digest, then the second time we have to call it
    // again, which will fail again
    MessageDigest digest = null;// w ww.j a va2s . c  om

    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (digest == null)
        return input;

    // now everything is ok, go ahead
    try {
        digest.update(input.getBytes("UTF-8"));
    } catch (java.io.UnsupportedEncodingException ex) {
        ex.printStackTrace();
    }

    final StringBuilder sbMd5Hash = new StringBuilder();

    final byte data[] = digest.digest();

    /*           for (byte element : data) {
            sbMd5Hash.append(Character.forDigit((element >> 4) & 0xf, 16));
            sbMd5Hash.append(Character.forDigit(element & 0xf, 16));
            }
    $%6e!df fek@&^$345M
    pkrGlr1Test
                    
            return sbMd5Hash.toString();
    */ //final byte[] md5Digest = md.digest(password.getBytes());

    final BigInteger md5Number = new BigInteger(1, data);
    final String md5String = md5Number.toString(16);
    return md5String;

}

From source file:ac.elements.parser.SimpleDBConverter.java

/**
 * Encodes real integer value into a string by offsetting and zero-padding
 * number up to the specified number of digits. Use this encoding method if
 * the data range set includes both positive and negative values.
 * /*from   w  w w.j a  v  a  2s . c om*/
 * com.xerox.amazonws.sdb.DataUtils
 * 
 * @param number
 *            int to be encoded
 * @return string representation of the int
 */
private static String encodeInt(int number) {
    int maxNumDigits = BigInteger.valueOf(Integer.MAX_VALUE).subtract(BigInteger.valueOf(Integer.MIN_VALUE))
            .toString(RADIX).length();
    long offsetValue = Integer.MIN_VALUE;

    BigInteger offsetNumber = BigInteger.valueOf(number).subtract(BigInteger.valueOf(offsetValue));
    String longString = offsetNumber.toString(RADIX);
    int numZeroes = maxNumDigits - longString.length();
    StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
    for (int i = 0; i < numZeroes; i++) {
        strBuffer.insert(i, '0');
    }
    strBuffer.append(longString);
    return strBuffer.toString();
}

From source file:ac.elements.parser.SimpleDBConverter.java

/**
 * Encodes real long value into a string by offsetting and zero-padding
 * number up to the specified number of digits. Use this encoding method if
 * the data range set includes both positive and negative values.
 * //from   ww  w. j a v a 2 s  . co  m
 * com.xerox.amazonws.sdb.DataUtils
 * 
 * @param number
 *            short to be encoded
 * @return string representation of the short
 */
private static String encodeShort(short number) {
    int maxNumDigits = BigInteger.valueOf(Short.MAX_VALUE).subtract(BigInteger.valueOf(Short.MIN_VALUE))
            .toString(RADIX).length();
    long offsetValue = Short.MIN_VALUE;

    BigInteger offsetNumber = BigInteger.valueOf(number).subtract(BigInteger.valueOf(offsetValue));
    String longString = offsetNumber.toString(RADIX);
    int numZeroes = maxNumDigits - longString.length();
    StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
    for (int i = 0; i < numZeroes; i++) {
        strBuffer.insert(i, '0');
    }
    strBuffer.append(longString);
    return strBuffer.toString();
}