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: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.
 * /*  w ww.  j ava  2  s.  c o  m*/
 * com.xerox.amazonws.sdb.DataUtils
 * 
 * @param number
 *            long to be encoded
 * @return string representation of the long
 */
private static String encodeLong(long number) {
    int maxNumDigits = BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.valueOf(Long.MIN_VALUE))
            .toString(RADIX).length();
    long offsetValue = Long.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:CryptPassword.java

/**
 * @param username//ww  w  .j a  v  a  2  s . c  o m
 * @param realm
 * @param md5Password
 * @return is the Digest password valid with the given real
 */
public static boolean isMD5DigestValid(String username, String realm, String md5Password) {
    byte b[];
    try {
        b = MessageDigest.getInstance("MD5").digest((username + ":" + realm + ":" + md5Password).getBytes());
    } catch (NoSuchAlgorithmException e) {
        return false;
    }
    BigInteger bi = new BigInteger(b);
    String s = bi.toString(16);
    if (s.length() % 2 != 0) {
        s = "0" + s; // String s is the encrypted password
    }
    return s.equals(md5Password);
}

From source file:fr.sedona.volley.manager.HttpImageLoader.java

public static String md5(String s) {
    if (s == null) {
        return null;
    }/*from w  w  w .j av  a  2 s .c o m*/
    try {
        byte messageDigest[] = MessageDigest.getInstance("MD5").digest(s.getBytes());
        // Create Hex String
        BigInteger bi = new BigInteger(1, messageDigest);
        String result = bi.toString(16);
        if (result.length() % 2 != 0)
            result = (new StringBuilder("0")).append(result).toString();

        return result;

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

From source file:fr.sedona.volley.manager.HttpImageLoader.java

public static String md5(ByteArrayOutputStream s) {
    if (s == null) {
        return null;
    }//from w ww . j ava  2 s. co m
    try {
        byte messageDigest[] = MessageDigest.getInstance("MD5").digest(s.toByteArray());
        // Create Hex String
        BigInteger bi = new BigInteger(1, messageDigest);
        String result = bi.toString(16);
        if (result.length() % 2 != 0)
            result = (new StringBuilder("0")).append(result).toString();

        return result;

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

From source file:org.iransoil.collect.android.utilities.FileUtils.java

@SuppressLint("LongLogTag")
public static String getMd5Hash(File file) {
    try {/*w w w.  j av a  2 s . c om*/
        // 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:graphene.util.fs.FileUtils.java

public static String getMD5String(final byte[] bytes) {
        final BigInteger bigInt = new BigInteger(1, bytes);
        final String hashtext = bigInt.toString(16);
        return hashtext;

    }// w w  w.j  a  va  2s.  com

From source file:com.nloko.android.Utils.java

public static String getMd5Hash(byte[] input) {
    if (input == null) {
        throw new IllegalArgumentException("input");
    }//  www .  ja  v  a 2  s .c om

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input);
        BigInteger number = new BigInteger(1, messageDigest);
        //String md5 = number.toString(16);
        StringBuffer md5 = new StringBuffer();
        md5.append(number.toString(16));

        while (md5.length() < 32) {
            //md5 = "0" + md5;
            md5.insert(0, "0");
        }

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

From source file:eu.europeana.uim.sugarcrmclient.internal.helpers.ClientUtils.java

/**
 * Encrypts a given String into a MD5 format.
 * // ww w.  ja v a 2s .  c o m
 * @param value
 *            The string to be encrypted
 * @return the encrypted String
 */
public static String md5(String value) {
    // MessageDigest mdEnc;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(value.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String hashtext = number.toString(16);
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:com.matze5800.paupdater.Functions.java

public static String calculateMD5(File updateFile) {
    String DEBUG_TAG = "md5calc";
    MessageDigest digest;//from  ww  w.  j a v a 2 s  . c o m
    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(updateFile);
    } 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);
        output = String.format("%32s", output).replace(' ', '0');
        return output;
    } catch (IOException e) {
        throw new RuntimeException("Unable to process file for MD5", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(DEBUG_TAG, "Exception on closing MD5 input stream", e);
        }
    }
}

From source file:com.grarak.kerneladiutor.utils.Utils.java

private static String calculateMD5(File updateFile) {
    MessageDigest digest;//from  w  w w.ja  v a2s .co  m
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Exception while getting digest", e);
        return null;
    }

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