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:Main.java

/**
 * Returns the lowercase string representation of the file's MD5 sum.
 *//*from  w ww  .j av a2 s  .com*/
public static String getMD5Sum(String file) throws IOException {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        is.close();
        byte[] md5sum = digest.digest();
        BigInteger bigInt = new BigInteger(1, md5sum);
        return bigInt.toString(16);
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
}

From source file:Main.java

public final static String md5(String value) {
    try {//from w w  w .ja v  a  2s.c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(value.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);

        while (md5.length() < 32) {
            md5 = "0" + md5;
        }
        return md5;

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String byteArrayToHexString(byte[] bytes) {
    int oldLength = bytes.length;
    byte[] positiveBytes = new byte[oldLength + 1];
    System.arraycopy(bytes, 0, positiveBytes, 1, oldLength);
    positiveBytes[0] = 1;//  w w  w  . j  ava  2 s .c  om
    BigInteger bigInt = new BigInteger(positiveBytes);
    String hex = bigInt.toString(16);
    return hex.substring(1);
}

From source file:Main.java

public static String getMD5(String input) {
    try {/*from  w w w  .  j  a v a  2  s . c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());
        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);
    }
}

From source file:Main.java

public static String getFileMD5(File file) {
    if (!file.isFile()) {
        return null;
    }//  w ww.ja v a 2 s  .c  o m
    MessageDigest digest = null;
    FileInputStream in = null;
    byte buffer[] = new byte[1024];
    int len;
    try {
        digest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        while ((len = in.read(buffer, 0, 1024)) != -1) {
            digest.update(buffer, 0, len);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    BigInteger bigInt = new BigInteger(1, digest.digest());
    return bigInt.toString(16);
}

From source file:Main.java

/**
 * generates MD5 of input string//www  .  ja v a  2s . c  om
 *
 * @param input string to be hashed
 * @return MD5 hash of string
 */
public static String getMD5(String input) {

    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.reset();
    m.update(input.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    //zero pad to get the full 32 chars.
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return hashtext;
}

From source file:Main.java

private static String md5(String origin) {
    try {// www .  j a  v  a 2s .c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(origin.getBytes("UTF-8"));
        BigInteger bi = new BigInteger(1, md.digest());

        return bi.toString(16);
    } catch (Exception e) {
        return getUuid();
    }
}

From source file:Main.java

public static String md5(String input) {
    String result = input;//from  ww  w .ja  v a2  s  . c  o m
    if (input != null) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            result = hash.toString(16);
            if ((result.length() % 2) != 0) {
                result = "0" + result;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String getFileMd5String(File file) {
    if (!file.isFile())
        throw new IllegalArgumentException("only file can calculate MD5 value!");

    MessageDigest digest;/*from ww w  .  j  a v  a  2  s .  com*/
    FileInputStream in = null;
    byte buffer[] = new byte[8192];
    int len;
    try {
        digest = MessageDigest.getInstance("MD5");
        in = new FileInputStream(file);
        while ((len = in.read(buffer)) != -1) {
            digest.update(buffer, 0, len);
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.openml.apiconnector.algorithms.Hashing.java

/**
 * Generates MD5 Hash of String//from w w w .j a  v  a 2s  .c o  m
 * 
 * @param input - A string to be hashed.
 * @return MD5(input)
 * @throws NoSuchAlgorithmException
 */
public static String md5(String input) throws NoSuchAlgorithmException {
    String result;
    MessageDigest md = MessageDigest.getInstance("MD5"); // or "SHA-1"
    md.update(input.getBytes());
    BigInteger hash = new BigInteger(1, md.digest());
    result = hash.toString(16);
    while (result.length() < 32) { // 40 for SHA-1
        result = "0" + result;
    }
    return result;
}