Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Update the digest using the specified ByteBuffer.

Usage

From source file:Main.java

public static String cryptoStr2SHA1(String str) {
    try {// w  w  w .j av  a 2  s .co m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(str.getBytes());
        byte[] bytes = md.digest();
        return byte2Hex(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String encryptMD5(String password) {
    try {/* w  w w  . j a v a  2s  .co m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        byte byteData[] = md.digest();
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            String hex = Integer.toHexString(0xff & byteData[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static String getPwdHash(String password, String salt) {
    try {//from  ww w .j  a  va  2  s. c o  m
        String temp = password + salt;
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        md.update(temp.getBytes("UTF-8")); // Change this to "UTF-16" if needed
        byte[] digest = md.digest();
        return String.format("%0" + (digest.length * 2) + 'x', new BigInteger(1, digest));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String crypt(String senha) {
    try {/*from w  w w.  j  a  v  a  2s .  c  om*/
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(senha.getBytes());

        byte messageDigest[] = digest.digest();
        StringBuffer hexString = new StringBuffer();

        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return senha;
    }
}

From source file:Main.java

public static final String toMD5(final String toEncrypt) {
    try {/*  w  w w . j  a v  a  2  s  .  c o m*/
        final MessageDigest digest = MessageDigest.getInstance("md5");
        digest.update(toEncrypt.getBytes());
        final byte[] bytes = digest.digest();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X", bytes[i]));
        }
        return sb.toString().toLowerCase();
    } catch (Exception exc) {
        return null; // Impossibru!
    }
}

From source file:Main.java

public final static String MD5(String s) {
    try {/*from w  w w. jav  a2  s  .c  o  m*/
        byte[] btInput = s.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < md.length; i++) {
            int val = ((int) md[i]) & 0xff;
            if (val < 16)
                sb.append("0");
            sb.append(Integer.toHexString(val));
        }
        return sb.toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static String generateSHA1Hash(String textToBeHashed) {
    try {/*w w  w .  j  av a  2  s .  co  m*/
        MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
        messageDigest.update(textToBeHashed.getBytes());
        byte[] sha1hash = messageDigest.digest();
        return getHexString(sha1hash);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't generate SHA-1 hash for " + textToBeHashed);
    }
}

From source file:MD5.java

/**
 * MD5 BASE64 checksum for the specified input string.
 * // w  ww  .  j a va 2 s.c om
 * @param input -
 *          Specified input string
 * @return String - MD5 BASE64 sum
 */
public static String checkMD5(String input) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] enc = md.digest();
        String md5Sum = new sun.misc.BASE64Encoder().encode(enc);
        return md5Sum;
    } catch (NoSuchAlgorithmException nsae) {
        System.out.println(nsae.getMessage());
        return null;
    }
}

From source file:Main.java

public static byte[] SHA1(String text) {
    if (null == text) {
        return null;
    }//w w w  . j  a  v a2  s  .com
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        messageDigest.update(text.getBytes());
        return messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.dtolabs.rundeck.core.plugins.PluginUtils.java

public static String generateShaIdFromName(String pluginName) {
    MessageDigest digest = DigestUtils.getSha256Digest();
    digest.update(pluginName.getBytes());
    return bytesAsHex(digest.digest()).substring(0, 12);
}