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 getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {//from w  ww . j av a  2  s . com
        messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.reset();

        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException caught!");
    } catch (UnsupportedEncodingException e) {
    }

    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }
    return md5StrBuff.substring(0, 24).toString().toUpperCase();
}

From source file:Main.java

public static String hash(String pass) {
    MessageDigest md = null;
    try {/*from w  ww .j av a  2s  . c  o  m*/
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md.update(pass.getBytes());
    byte byteData[] = md.digest();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();

}

From source file:biblivre3.utils.TextUtils.java

public static String encodePassword(String password) {
    if (password == null) {
        throw new ExceptionUser("Password is null");
    }/*from   www  .  j av a  2 s  .  co m*/
    if (password.trim().length() == 0) {
        throw new ExceptionUser("Password is empty");
    }
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(password.getBytes("UTF-8"));
        byte[] raw = md.digest();
        return new String((new Base64()).encode(raw));
    } catch (Exception e) {
        throw new ExceptionUser(e.getMessage());
    }
}

From source file:io.wcm.caravan.pipeline.cache.couchbase.impl.CouchbaseCacheAdapter.java

private static String calculateHash(String message) {

    try {//from w  w  w .j av  a 2s .c o  m
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(message.getBytes(CharEncoding.UTF_8));
        byte[] digestBytes = digest.digest();

        return javax.xml.bind.DatatypeConverter.printHexBinary(digestBytes).toLowerCase();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        throw new RuntimeException("Failed to create sha1 Hash from " + message, ex);
    }
}

From source file:Main.java

public static String md5Summary(String str) {

    if (str == null) {
        return null;
    }/*from   w  w  w  . j  ava 2 s  . c  om*/

    MessageDigest messageDigest = null;

    try {
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("utf-8"));
    } catch (NoSuchAlgorithmException e) {

        return str;
    } catch (UnsupportedEncodingException e) {
        return str;
    }

    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }

    return md5StrBuff.toString();
}

From source file:com.magic.util.HashCrypt.java

public static String getDigestHash(String str, String code, String hashType) {
    if (str == null)
        return null;
    try {//  ww  w .  j  av a 2 s  . c  om
        byte codeBytes[] = null;

        if (code == null)
            codeBytes = str.getBytes();
        else
            codeBytes = str.getBytes(code);
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);

        messagedigest.update(codeBytes);
        byte digestBytes[] = messagedigest.digest();
        char[] digestChars = Hex.encodeHex(digestBytes);
        ;
        return "{" + hashType + "}" + new String(digestChars, 0, digestChars.length);
    } catch (Exception e) {
        throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
    }
}

From source file:altermarkive.uploader.Report.java

private static String hash(String text) {
    String hash = null;/*from   w  w w  . j a v  a2s. com*/
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes());
        BigInteger integer = new BigInteger(1, md.digest());
        hash = String.format("%1$032X", integer);
    } catch (NoSuchAlgorithmException exception) {
        String trace = Log.getStackTraceString(exception);
        String message = String.format("MD5 is not available:\n%s", trace);
        Log.e(TAG, message);
    }
    return hash;
}

From source file:edu.sjsu.cohort6.esp.common.CommonUtils.java

private static String generateMD5Hash(String plaintext) throws NoSuchAlgorithmException {
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();//from www .j  a v a2  s  .co  m
    m.update(plaintext.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String 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;
    }
    return hashtext;
}

From source file:Main.java

public static String sha1(String strSrc) {
    MessageDigest md = null;
    String strDes = "";

    byte[] bt = strSrc.getBytes();
    try {//from   w  w w . ja  va 2  s. c  o m
        md = MessageDigest.getInstance("SHA-1");
        md.update(bt);
        byte[] encryptStr = md.digest();
        String tmp = null;
        for (int i = 0; i < encryptStr.length; i++) {
            tmp = (Integer.toHexString(encryptStr[i] & 0xFF));
            if (tmp.length() == 1) {
                strDes += "0";
            }
            strDes += tmp;
        }
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Invalid algorithm.");
        return null;
    }
    return strDes;
}

From source file:com.gwtcx.server.util.Security.java

public static String md5(final String text) {
    String hashword = null;/*  w  w w . j a v  a  2 s .c  om*/

    try {
        final MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(text.getBytes());
        final BigInteger hash = new BigInteger(1, md5.digest());
        hashword = hash.toString(16);
    } catch (final NoSuchAlgorithmException nsae) {
    }

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

    return hashword;
}