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

/**
 * generates MD5 of input string/*w  w w . ja v a 2 s.  c  o  m*/
 *
 * @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:D_common.E_ncript.java

public static String getMd5(String s) {
    try {//w  ww. j a  v a 2s.co  m
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        return new BigInteger(1, m.digest()).toString(16);
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

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;/* w  ww  .jav  a 2  s  .c  o  m*/
    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:Main.java

private static long readThreadAddres(String line) {
    int start = line.indexOf("0x"); //$NON-NLS-1$
    if (start < 0)
        return -1;
    return (new BigInteger(line.substring(start + 2), 16)).longValue();
}

From source file:Main.java

public static String bin2hex(byte[] data) {
    return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data));
}

From source file:Main.java

public static String getFileMD5(File file) {
    if (!file.isFile()) {
        return null;
    }// w  ww. j a  v  a 2s. c  om
    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

public static String checkSum(String path) {
    String checksum = null;/*from w w w.j a  va2  s . co  m*/
    try {
        FileInputStream fis = new FileInputStream(path);
        MessageDigest md = MessageDigest.getInstance("MD5");

        //Using MessageDigest update() method to provide input
        byte[] buffer = new byte[8192];
        int numOfBytesRead;
        while ((numOfBytesRead = fis.read(buffer)) > 0) {
            md.update(buffer, 0, numOfBytesRead);
        }
        byte[] hash = md.digest();
        checksum = new BigInteger(1, hash).toString(16); //don't use this, truncates leading zero
    } catch (IOException | NoSuchAlgorithmException ignored) {
    }
    assert checksum != null;
    return checksum.trim();
}

From source file:Main.java

/**
 * Generate a hash chain of a random number
 * @param r1/*from   ww  w. j  a  va2s . com*/
 * @param numHash
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 */
public static LinkedList<BigInteger> getHashChain(BigInteger r1, int numHash)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {

    LinkedList<BigInteger> hashChain = new LinkedList<BigInteger>();
    hashChain.add(r1);

    for (int i = 1; i < numHash; i++) {
        byte[] lastR = hashChain.getLast().toByteArray();
        hashChain.add(new BigInteger(1, SHA1(lastR)));
    }

    return hashChain;
}

From source file:Main.java

public static String getMD5EncryptedString(byte[] _encTarget) {
    String encTarget = "";
    try {//from  w  ww.j  a  v  a  2s  . c o m
        encTarget = new String(_encTarget, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        System.out.println("error converting byte[] to string");
    }
    MessageDigest mdEnc = null;
    try {
        mdEnc = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception while encrypting to md5");
    } // Encryption algorithm
    mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
    String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
    while (md5.length() < 32) {
        md5 = "0" + md5;
    }
    return md5;
}

From source file:Main.java

/**
 * Returns the lowercase string representation of the file's MD5 sum.
 *///from  w  w w  .ja  va 2s . c o m
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 "";
    }
}