Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

In this page you can find the example usage for java.security MessageDigest digest.

Prototype

public byte[] digest(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:ca.ualberta.physics.cssdp.util.HashUtils.java

/**
 * From a password, a number of iterations and a salt, returns the
 * corresponding digest//from  w  w  w.j  a v  a 2 s  . c  o  m
 * 
 * @param iterationNb
 *            int The number of iterations of the algorithm
 * @param password
 *            String The password to encrypt
 * @param salt
 *            byte[] The salt
 * @return byte[] The digested password
 * @throws NoSuchAlgorithmException
 *             If the algorithm doesn't exist
 */
public static byte[] getHash(int iterationNb, String password, byte[] salt) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(salt);
        byte[] input = digest.digest(password.getBytes("UTF-8"));
        for (int i = 0; i < iterationNb; i++) {
            digest.reset();
            input = digest.digest(input);
        }
        return input;
    } catch (NoSuchAlgorithmException nsa) {
        throw Throwables.propagate(nsa);
    } catch (UnsupportedEncodingException e) {
        throw Throwables.propagate(e);
    }
}

From source file:net.blogracy.model.hashes.Hashes.java

public static String hash(String text) throws NoSuchAlgorithmException {
    MessageDigest digester = MessageDigest.getInstance("SHA-1");
    byte[] digest = digester.digest(text.getBytes());
    String result = base32.encodeAsString(digest);
    return result;
}

From source file:Main.java

/**
 * Returns an MD-5 digest of the database encryption password.
 * <blockquote>This algorithm performs an MD-5 hash on a UTF-8 representation of the password.</blockquote>
 *
 * @param pass The plain encryption password
 *
 * @return The database encryption password digest
 *
 * @throws NoSuchAlgorithmException If the platform doesn't support MD-5
 */// w  w w. j a  va 2 s  . co m
public static byte[] passToDigest(char[] pass) throws NoSuchAlgorithmException {
    // FIXME: Enhance security for this method
    Charset cs = Charset.forName("UTF-8");
    MessageDigest md = MessageDigest.getInstance("MD5");
    ByteBuffer bbuf = cs.encode(CharBuffer.wrap(pass));
    byte[] bpass = Arrays.copyOf(bbuf.array(), bbuf.remaining());

    return md.digest(bpass);
}

From source file:org.trustedanalytics.cfbroker.store.zookeeper.service.utils.ZookeeperTestUtils.java

public static void createDir(ZookeeperCredentials credentials, String path) throws Exception {

    CuratorFramework tempClient = getNewTempClient(credentials.getConnectionString());

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] authDigest = md
            .digest(String.format("%s:%s", credentials.getUsername(), credentials.getPassword()).getBytes());
    String authEncoded = new String(Base64.encode(authDigest));
    ImmutableList<ACL> acl = ImmutableList.of(new ACL(ZooDefs.Perms.ALL,
            new Id("digest", String.format("%s:%s", credentials.getUsername(), authEncoded))));

    tempClient.create().creatingParentsIfNeeded().withACL(acl).forPath(path);

    tempClient.close();/*from   w w w  .  j a  v a 2s.c  o  m*/
}

From source file:Main.java

public static String stringGetMD5String(String s) {
    byte[] data;// ww  w .  j  a  v  a2  s .c o  m
    MessageDigest md5;

    try {
        data = s.getBytes("UTF-8");
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        return null;
    }

    byte[] hash = md5.digest(data);
    StringBuilder hashStr = new StringBuilder();

    for (byte byteValue : hash) {
        hashStr.append(String.format("%02x", byteValue));
    }

    return hashStr.toString();
}

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:org.rivetlogic.utils.IntegrationUtils.java

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

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

    BigInteger number = new BigInteger(1, dg);

    return number;
}

From source file:Main.java

public static String convertStringToMd5(String valor) {

    MessageDigest mDigest;
    StringBuffer sb;//  w w w  . j av a 2s .  c  om

    if (valor == "") {
        return null;
    }
    try {
        mDigest = MessageDigest.getInstance("MD5");
        byte[] valorMD5 = mDigest.digest(valor.getBytes("UTF-8"));
        sb = new StringBuffer();

        for (byte b : valorMD5) {
            sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        Logger.getLogger(MessageDigest.class.getName()).log(Level.SEVERE, null, ex);
        Logger.getLogger(StringBuffer.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:net.firejack.platform.core.utils.SecurityHelper.java

/**
 * return hash/* w w w  . j a v  a 2s  . com*/
 *
 * @param value value to hash
 * @return hashed value
 */
public static String hash(String value) {
    String result;
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] hash = digest.digest(value.getBytes());
        result = asHex(hash);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:com.google.gerrit.server.mail.FromAddressGeneratorProvider.java

private static String hashOf(String data) {
    try {/*from  w w  w.  j a va2 s. c  o  m*/
        MessageDigest hash = MessageDigest.getInstance("MD5");
        byte[] bytes = hash.digest(data.getBytes(Charsets.UTF_8));
        return Base64.encodeBase64URLSafeString(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("No MD5 available", e);
    }
}