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

public static String getMd5Hash(File file) {
    try {/*w  w  w .  ja  va2 s. co  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 = file.length();

        if (lLength > Integer.MAX_VALUE) {
            Log.e(t, "File " + file.getName() + "is too large");
            return null;
        }

        int length = (int) lLength;

        InputStream is = null;
        is = new FileInputStream(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 md5;

    } catch (NoSuchAlgorithmException e) {
        Log.e("MD5", e.getMessage());
        return null;

    } catch (FileNotFoundException e) {
        Log.e("No Cache File", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Problem reading from file", e.getMessage());
        return null;
    }

}

From source file:org.jboss.arquillian.ajocado.utils.URLUtils.java

/**
 * Gets a MD5 digest of some resource obtains as input stream from connection to URL given by URL string.
 *
 * @param url of the resource/*from   w ww .j  ava 2 s .c  om*/
 * @return MD5 message digest of resource
 * @throws IOException when connection to URL fails
 */
public static String resourceMd5Digest(String url) throws IOException {
    URLConnection connection = new URL(url).openConnection();

    InputStream in = connection.getInputStream();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException ex) {
        throw new IllegalStateException("MD5 hashing is unsupported", ex);
    }
    byte[] buffer = new byte[MD5_BUFFER_SIZE];
    int read = 0;

    while ((read = in.read(buffer)) > 0) {
        digest.update(buffer, 0, read);
    }

    byte[] md5sum = digest.digest();
    BigInteger bigInt = new BigInteger(1, md5sum);
    return bigInt.toString(HEX_RADIX);
}

From source file:org.apache.hadoop.hbase.manual.utils.HBaseUtils.java

/**
 * Returns the bytes corresponding to the BigInteger
 * @param bigInteger// w  w  w . j  a v a 2  s.  c o  m
 * @return byte corresponding to input BigInteger
 */
private static byte[] convertToByte(BigInteger bigInteger) {
    String bigIntegerString = bigInteger.toString(16);
    bigIntegerString = StringUtils.leftPad(bigIntegerString, rowComparisonLength, '0');
    return Bytes.toBytes(bigIntegerString);
}

From source file:com.wirecard.ezlinkwebservices.util.TerminalUtil.java

public static synchronized String strXor(String s1, String s2) {
    System.out.println("strXor=" + s1 + "  " + s2);
    BigInteger one = new BigInteger(s1, 16);
    BigInteger two = new BigInteger(s2, 16);
    BigInteger three = one.xor(two);
    String s3 = three.toString(16);
    System.out.println(s3);/* ww  w . j  a  v a 2 s.c  om*/
    return s3;
}

From source file:mitm.common.util.MiscArrayUtils.java

/**
 * Encodes the byte array to a String consisting of all readable characters.
 *///w  w w . jav a 2s  . c  om
public static String toMaxRadix(byte[] bytes) {
    Check.notNull(bytes, "bytes");

    /*
     * We need to make sure that the BigInteger will be positive and that any starting zero (0) bytes
     * are not removed.
     */
    byte[] pos = ArrayUtils.addAll(new byte[] { 1 }, bytes);

    BigInteger bigInt = new BigInteger(pos);

    return bigInt.toString(Character.MAX_RADIX);
}

From source file:edu.sjsu.cohort6.esp.common.CommonUtils.java

private static String generateMD5Hash(String plaintext) throws NoSuchAlgorithmException {
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();//w  w  w.j a  v a2  s . c  om
    m.update(plaintext.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.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;
}

From source file:edu.mit.media.funf.HashUtil.java

public static String oneWayHashString(String msg) {
    MessageDigest md = getMessageDigest();
    synchronized (md) {
        if (msg == null || "".equals(msg)) {
            return "";
        } else if (md == null) {
            return "NO SHA";
        } else {/* ww w.jav  a 2 s.c  o  m*/
            byte[] msgDigest = md.digest(msg.getBytes());
            BigInteger number = new BigInteger(1, msgDigest);
            return number.toString(16);
        }
    }
}

From source file:org.rivetlogic.utils.IntegrationUtils.java

public static String MD5(String str) throws NoSuchAlgorithmException {
    MessageDigest digest = MessageDigest.getInstance("MD5");

    byte[] dg = digest.digest(str.getBytes());

    BigInteger number = new BigInteger(1, dg);

    return number.toString(16);
}

From source file:BitLottoVerify.java

public static String printBytesInHex(byte[] d) {
    StringBuffer s = new StringBuffer(d.length * 2);
    BigInteger bi = new BigInteger(1, d);
    s.append(bi.toString(16));
    while (s.length() < d.length * 2) {
        s.insert(0, '0');
    }/*from  w ww .  j a v  a 2s. c  o m*/
    return s.toString();

}

From source file:com.gwtcx.server.util.Security.java

public static String md5(final String text) {
    String hashword = null;//w  w  w  .j  a  v a  2s  . c  o m

    try {
        final MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(text.getBytes());
        final BigInteger hash = new BigInteger(1, md5.digest());
        hashword = hash.toString(16);
    } catch (final NoSuchAlgorithmException nsae) {
    }

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

    return hashword;
}