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:nya.miku.wishmaster.api.AbstractLynxChanModule.java

private String computeFileMD5(File file) throws NoSuchAlgorithmException, IOException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.reset();//from ww  w .ja va2  s .  c  o  m

    FileInputStream fis = new FileInputStream(file);
    byte[] byteArray = new byte[1024];
    int bytesCount;
    while ((bytesCount = fis.read(byteArray)) != -1) {
        messageDigest.update(byteArray, 0, bytesCount);
    }
    fis.close();

    byte[] digest = messageDigest.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String md5Hex = bigInt.toString(16);
    if (md5Hex.length() < 32) {
        char[] head = new char[32 - md5Hex.length()];
        Arrays.fill(head, '0');
        md5Hex = new StringBuilder(32).append(head).append(md5Hex).toString();
    }
    return md5Hex;
}

From source file:com.trigger_context.Main_Service.java

public String calculateMD5(File updateFile) {
    MessageDigest digest;/*from w  w w .  j  ava  2 s .co  m*/
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        noti("Exception while getting Digest", e.toString());
        return null;
    }

    InputStream is;
    try {
        is = new FileInputStream(updateFile);
    } catch (FileNotFoundException e) {
        noti("Exception while getting FileInputStream", e.toString());
        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.toUpperCase();
    } catch (IOException e) {
        throw new RuntimeException("Unable to process file for MD5", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            noti("Exception on closing MD5 input stream", e.toString());
        }
    }
}

From source file:edu.harvard.i2b2.crc.dao.setfinder.querybuilder.temporal.TemporalQuery.java

/**
 * Generate Unique Id/*w  w  w  .  j  ava2  s  . c  o m*/
 * 
 * Returns a string with a guaranteed unique value. This method is used specifically to uniquely identify queries and 
 * panel groups within the context of the query
 * 
 * @return string with a guaranteed unique value
 */
protected String generateUniqueId() {
    UUID uniqueKey = UUID.randomUUID();
    String hexNum = uniqueKey.toString().replace("-", "");
    BigInteger big = new BigInteger(hexNum, 16);
    return big.toString(36);
}

From source file:org.apache.blur.command.BaseCommandManager.java

protected BigInteger checkContents(FileStatus fileStatus, FileSystem fileSystem) throws IOException {
    if (fileStatus.isDir()) {
        LOG.debug("Scanning directory [{0}].", fileStatus.getPath());
        BigInteger count = BigInteger.ZERO;
        Path path = fileStatus.getPath();
        FileStatus[] listStatus = fileSystem.listStatus(path);
        for (FileStatus fs : listStatus) {
            count = count.add(checkContents(fs, fileSystem));
        }/*  w  ww.  j av a 2s  .  c  om*/
        return count;
    } else {
        int hashCode = fileStatus.getPath().toString().hashCode();
        long modificationTime = fileStatus.getModificationTime();
        long len = fileStatus.getLen();
        BigInteger bi = BigInteger.valueOf(hashCode)
                .add(BigInteger.valueOf(modificationTime).add(BigInteger.valueOf(len)));
        LOG.debug("File path hashcode [{0}], mod time [{1}], len [{2}] equals file code [{3}].",
                Integer.toString(hashCode), Long.toString(modificationTime), Long.toString(len),
                bi.toString(Character.MAX_RADIX));
        return bi;
    }
}

From source file:org.ejbca.core.ejb.ca.store.CertificateStoreSessionBean.java

@Override
public String findUsernameByCertSerno(final Admin admin, final BigInteger serno, final String issuerdn) {
    if (log.isTraceEnabled()) {
        log.trace(">findUsernameByCertSerno(), serno: " + serno.toString(16) + ", issuerdn: " + issuerdn);
    }//w w w  .  ja  va  2  s .c om
    final String ret = CertificateData.findLastUsernameByIssuerDNSerialNumber(entityManager,
            CertTools.stringToBCDNString(issuerdn), serno.toString());
    if (log.isTraceEnabled()) {
        log.trace("<findUsernameByCertSerno(), ret=" + ret);
    }
    return ret;
}

From source file:org.ejbca.core.ejb.ca.store.CertificateStoreSessionBean.java

@Override
public boolean isRevoked(String issuerDN, BigInteger serno) {
    if (log.isTraceEnabled()) {
        log.trace(">isRevoked(), dn:" + issuerDN + ", serno=" + serno.toString(16));
    }//ww w. j a v a 2s  .  c  o  m
    // First make a DN in our well-known format
    String dn = CertTools.stringToBCDNString(issuerDN);
    boolean ret = false;
    try {
        Collection<CertificateData> coll = CertificateData.findByIssuerDNSerialNumber(entityManager, dn,
                serno.toString());
        if (coll.size() > 0) {
            if (coll.size() > 1) {
                String msg = intres.getLocalizedMessage("store.errorseveralissuerserno", issuerDN,
                        serno.toString(16));
                //adapter.log(admin, issuerDN.hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, null, LogConstants.EVENT_ERROR_DATABASE, msg);
                log.error(msg);
            }
            Iterator<CertificateData> iter = coll.iterator();
            while (iter.hasNext()) {
                CertificateData data = iter.next();
                // if any of the certificates with this serno is revoked, return true
                if (data.getStatus() == SecConst.CERT_REVOKED) {
                    ret = true;
                    break;
                }
            }
        } else {
            // If there are no certificates with this serial number, return true (=revoked). Better safe than sorry!
            ret = true;
            if (log.isTraceEnabled()) {
                log.trace("isRevoked() did not find certificate with dn " + dn + " and serno "
                        + serno.toString(16));
            }
        }
    } catch (Exception e) {
        throw new EJBException(e);
    }
    if (log.isTraceEnabled()) {
        log.trace("<isRevoked() returned " + ret);
    }
    return ret;
}

From source file:com.neusou.artsy.Flickr.java

public String createRequestSignature(String secret, Bundle data) throws NoSuchAlgorithmException {
    Set<String> keys = data.keySet();
    String[] keysArray = new String[keys.size()];
    keys.toArray(keysArray);/*from  www  .j a  v a 2s . c o  m*/
    Arrays.sort(keysArray);

    StringBuffer sb = new StringBuffer();
    sb.append(secret);
    for (String key : keysArray) {
        sb.append(key);
        sb.append(data.get(key));
    }

    MessageDigest md = MessageDigest.getInstance(SIGNATURE_DIGEST_ALGORITHM);
    md.update(sb.toString().getBytes());
    byte[] digested = md.digest();
    BigInteger bi = new BigInteger(1, digested);
    String signature = bi.toString(16);
    return signature;
}

From source file:edu.ucuenca.authorsdisambiguation.Distance.java

public String getMD5(String input) {
    try {/*from  ww  w .j  a va 2  s .  co 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:org.apache.blur.command.BaseCommandManager.java

protected void loadNewCommand(FileSystem fileSystem, FileStatus fileStatus, BigInteger hashOfContents)
        throws IOException {
    File file = new File(_tmpPath, UUID.randomUUID().toString());
    if (!file.mkdirs()) {
        LOG.error("Error while trying to create a tmp directory for loading a new command set from [{0}].",
                fileStatus.getPath());/*from ww w .  j  a v  a 2  s . c om*/
        return;
    }
    LOG.info("Copying new command with hash [{2}] set from [{0}] into [{1}].", fileStatus.getPath(),
            file.getAbsolutePath(), hashOfContents.toString(Character.MAX_RADIX));
    copyLocal(fileSystem, fileStatus, file);
    URLClassLoader loader = new URLClassLoader(getUrls(file).toArray(new URL[] {}));
    Enumeration<URL> resources = loader.getResources(META_INF_SERVICES_ORG_APACHE_BLUR_COMMAND_COMMANDS);
    loadCommandClasses(resources, loader, hashOfContents);
}

From source file:org.ejbca.core.ejb.ca.store.CertificateStoreSessionBean.java

@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Override/*from w  w w  .  j a va 2 s  .c o m*/
public void setRevokeStatus(Admin admin, String issuerdn, BigInteger serno, Date revokedate,
        Collection<Integer> publishers, int reason, String userDataDN) {
    if (log.isTraceEnabled()) {
        log.trace(">setRevokeStatus(),  issuerdn=" + issuerdn + ", serno=" + serno.toString(16) + ", reason="
                + reason);
    }
    try {
        Certificate certificate = findCertificateByIssuerAndSerno(admin, issuerdn, serno);
        setRevokeStatus(admin, certificate, revokedate, publishers, reason, userDataDN);
    } catch (FinderException e) {
        String msg = intres.getLocalizedMessage("store.errorfindcertserno", serno.toString(16));
        logSession.log(admin, issuerdn.hashCode(), LogConstants.MODULE_CA, new java.util.Date(), null, null,
                LogConstants.EVENT_ERROR_REVOKEDCERT, msg);
        throw new EJBException(e);
    }
    if (log.isTraceEnabled()) {
        log.trace("<setRevokeStatus(),  issuerdn=" + issuerdn + ", serno=" + serno.toString(16) + ", reason="
                + reason);
    }
}