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.lwes.util.NumberCodec.java

/**
 * Output a BigInteger in unsigned hexadecimal form, padding with zeroes.
 *
 * @param bi the BigInteger/*www.  j  av a  2 s  .  co m*/
 * @return a String representing the BigInteger.
 */
public static String toHexString(BigInteger bi) {
    if (bi == null) {
        return "";
    }
    return bi.toString(16); // 16-bit radix
}

From source file:org.opendatakit.briefcase.util.FileSystemUtils.java

public static final String getMd5Hash(File file) {
    try {//from  w ww .j a  v  a 2s  .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) {
            logger.error("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) {
        logger.error("MD5 calculation failed: " + e.getMessage());
        return null;

    } catch (FileNotFoundException e) {
        logger.error("No File: " + e.getMessage());
        return null;
    } catch (IOException e) {
        logger.error("Problem reading from file: " + e.getMessage());
        return null;
    }

}

From source file:org.occiware.mart.server.servlet.utils.Utils.java

/**
 * Create a MD5 hash.//  w  ww  . ja  v a  2s  . co  m
 *
 * @param bytes (array of bytes).
 * @return
 */
private static String getMd5Digest(byte[] bytes) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return "1";
        // throw new RuntimeException("MD5 cryptographic algorithm is not
        // available.", e);
    }
    byte[] messageDigest = md.digest(bytes);
    BigInteger number = new BigInteger(1, messageDigest);
    // prepend a zero to get a "proper" MD5 hash value
    return "0" + number.toString(16);
}

From source file:Gestores.GestorHash.java

public String md5(String plaintext) {
    String hashtext = "";
    try {/*from ww  w  .  j a  v a2s  . c o  m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(plaintext.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        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;
        }
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(GestorHash.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hashtext;
}

From source file:org.cruxframework.crux.core.server.rest.state.MD5ETagHandlerImpl.java

@Override
public String generateEtag(UriInfo uri, String content) {
    if (StringUtils.isEmpty(content)) {
        return null;
    }//from w ww. ja v a 2s  . co m

    try {
        byte[] bytes = content.getBytes("UTF-8");
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] hashBytes = digest.digest(bytes);
        BigInteger bigInt = new BigInteger(1, hashBytes);
        String hashtext = bigInt.toString(16);
        return hashtext;
    } catch (Exception e) {
        logger.error("Error generating etag with MD5 algorithm. Ignoring Etag for this resource ["
                + uri.getPath() + "]...", e);
    }
    return null;
}

From source file:org.awknet.commons.security.Encryption.java

/**
 * <p>//from   w  w w .j  av  a  2 s  .c o m
 * This function encrypt the password with MD5 method.
 * </p>
 * 
 * @param value
 *            : a password to be encrypt.
 * @return A encrypted password.
 * @since SIGERAR v1.1 - April/2008.
 * @throws NoSuchAlgorithmException
 */
public String genericEncryptMD5(String value) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    BigInteger hash = new BigInteger(1, md.digest(value.getBytes()));
    String encryptedValue = hash.toString(16);
    if (encryptedValue.length() % 2 != 0) {
        encryptedValue = "0" + encryptedValue;
    }
    return encryptedValue;
}

From source file:com.jim.im.offline.service.OfflineMessageServiceImpl.java

@Override
public Collection<ImMessage> getOfflineMessages(Collection<String> topics, BigInteger lastMessageId)
        throws ImParamException {
    return offlineMessageRepo.findByTopicNameIn(topics, new ObjectId(lastMessageId.toString(16)));
}

From source file:com.appzone.sim.services.handlers.PhoneRegistrationServiceHandler.java

public String getMD5(String input) {
    try {//from w  ww . j  a  va2 s .c  om
        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:com.saasovation.identityaccess.resource.AbstractResource.java

protected String userETag(User aUser) {

    int hashCode = aUser.hashCode() + aUser.person().hashCode();

    try {//from w ww . ja  v  a 2 s.  c  o m
        // change this algorithm as needed
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(Integer.toString(hashCode).getBytes("UTF-8"));
        BigInteger digestValue = new BigInteger(1, messageDigest.digest());
        String strongHash = digestValue.toString(16);

        return strongHash;

    } catch (Throwable t) {
        return Integer.toString(hashCode);
    }
}

From source file:your.app.AuthenticationServlet.java

/**
 * Create JSFS token.// w w w  .j  a  v a  2  s .co  m
 * @param userName User name
 * @param remoteAddr Remote address
 * @return MD5 hash of user + address + secret
 */
private String createToken(String userName, String remoteAddr) {
    if (log.isDebugEnabled())
        log.debug("createToken(userName=" + userName + ", remoteAddr=" + remoteAddr);
    String token = null;
    if (userName != null && userName.length() != 0) {
        String plaintext = userName + "*" + remoteAddr + "*" + secret;

        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.reset();
            m.update(plaintext.getBytes());
            byte[] digest = m.digest();
            BigInteger bigInt = new BigInteger(1, digest);
            token = bigInt.toString(16);
        } catch (NoSuchAlgorithmException e) {
            log.error("Create token failed", e);
        }
    }
    if (log.isDebugEnabled())
        log.debug(")createToken=" + token);
    return token;
}