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:op.tools.SYSTools.java

public static String hashword(String password) {
    String hashword = null;/*from  ww w  . j  av a2  s  . co m*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(password.getBytes());
        BigInteger hash = new BigInteger(1, md5.digest());
        hashword = hash.toString(16);
        if (hashword.length() == 31) {
            hashword = "0" + hashword;
        }
    } catch (NoSuchAlgorithmException nsae) {
        // ignore
    }
    return hashword;
}

From source file:org.callimachusproject.behaviours.AuthenticationManagerSupport.java

private String hash(String text, String algorithm) throws NoSuchAlgorithmException {
    byte[] hash = MessageDigest.getInstance(algorithm).digest(text.getBytes());
    BigInteger bi = new BigInteger(1, hash);
    String result = bi.toString(16);
    if (result.length() % 2 != 0) {
        return "0" + result;
    }//from  ww w.ja  v  a2s  .  c o m
    return result;
}

From source file:org.rifidi.edge.epcglobal.aleread.EPCDataContainerAdapter.java

/**
 * Helper method for creating a string that can be consumed by tdt.
 * // w  w w .  j av a2  s  . com
 * @param mem
 * @param length
 * @return
 */
private String createStringFromMem(BigInteger mem, int length) {
    String memString = mem.toString(2);
    int fill = length - memString.length();
    StringBuilder buildy = new StringBuilder(memString);
    // big integer swallows leading zeroes, reattech 'em
    while (fill > 0) {
        buildy.insert(0, "0");
        fill--;
    }
    return buildy.toString();
}

From source file:info.ajaxplorer.synchro.SyncJob.java

public static String computeMD5(File f) {
    MessageDigest digest;/*from   w  w w .j a v a  2  s  .  com*/
    try {
        digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
        return "";
    }
    InputStream is;
    try {
        is = new FileInputStream(f);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        return "";
    }
    byte[] buffer = new byte[8192];
    int read = 0;
    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);
        if (output.length() < 32) {
            // PAD WITH 0
            while (output.length() < 32)
                output = "0" + output;
        }
        return output;
    } catch (IOException e) {
        //throw new RuntimeException("Unable to process file for MD5", e);
        return "";
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            //throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
            return "";
        }
    }
}

From source file:com.googlecode.fascinator.authentication.internal.InternalAuthentication.java

/**
 * Password encryption method//from  w w  w  .jav  a2s .  c om
 * 
 * @param password Password to be encrypted
 * @return encrypted password
 * @throws AuthenticationException if fail to encrypt
 */
private String encryptPassword(String password) throws AuthenticationException {
    byte[] passwordBytes = password.getBytes();

    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(passwordBytes);

        byte messageDigest[] = algorithm.digest();
        BigInteger number = new BigInteger(1, messageDigest);

        password = number.toString(16);
        if (password.length() == 31) {
            password = "0" + password;
        }
    } catch (Exception e) {
        throw new AuthenticationException("Internal password encryption failure: " + e.getMessage());
    }
    return password;
}

From source file:org.springframework.jdbc.repo.impl.jdbc.RawPropertiesRepoImplTest.java

@Test
public void testLongValue() {
    BigInteger num = new BigInteger(500000, RANDOMIZER);
    final String longString = num.toString(8);

    TestEntry e = new TestEntry();
    repo.setProperties(e, new Transformer<TestEntry, Map<String, ?>>() {
        @Override/*from  w w w .ja  va 2  s. c  o m*/
        public Map<String, ?> transform(TestEntry input) {
            return Collections.<String, Serializable>singletonMap(PROP_NAME, longString);
        }
    });

    Map<String, Serializable> props = repo.getProperties(e.getId());
    assertEquals("Mismatched long value", longString, props.get(PROP_NAME));
}

From source file:com.otterca.persistence.dao.X509CertificateDaoDatastore.java

/**
 * Generate standard key.//from   w ww.j a  va  2  s. co  m
 * 
 * @param issuerDN
 * @param serialNumber
 * @return
 */
public Key generateKey(String issuerDN, BigInteger serialNumber) {
    return KeyFactory.createKey(KIND, issuerDN + ":" + serialNumber.toString(16));
}

From source file:org.mule.module.pubnub.PubNubModule.java

private String md5(String input) {
    try {//ww  w .j  av a 2  s .  c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String hashtext = number.toString(16);

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

        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloud.test.stress.StressTestDirectAttach.java

public static String createMD5Password(String password) {
    MessageDigest md5;//  w ww . j a va 2s. c  o  m

    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new CloudRuntimeException("Error", e);
    }

    md5.reset();
    BigInteger pwInt = new BigInteger(1, md5.digest(password.getBytes()));

    // make sure our MD5 hash value is 32 digits long...
    StringBuffer sb = new StringBuffer();
    String pwStr = pwInt.toString(16);
    int padding = 32 - pwStr.length();
    for (int i = 0; i < padding; i++) {
        sb.append('0');
    }
    sb.append(pwStr);
    return sb.toString();
}

From source file:com.searchtechnologies.aspire.components.heritrixconnector.HeritrixScanner.java

/**
 * Calculate the MD5 sum for the content of a given InputStream
 * @param contentSize the content size/*  w w w .ja  va 2s .co  m*/
 * @param is InputStream of the content to calculate
 * @return A String representation of the MD5 sum
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
protected static String computeMD5(InputStream is, MutableInt contentSize)
        throws NoSuchAlgorithmException, IOException {
    try {
        MessageDigest digest = null;
        digest = MessageDigest.getInstance("MD5");

        byte[] buffer = new byte[1024];
        int len;

        // Read the input stream and update the digest 
        while ((len = is.read(buffer)) > -1) {
            digest.update(buffer);
            contentSize.add(len);
        }

        // Convert the message digest into a string representation 
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}