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:org.mascherl.example.service.LoginService.java

public static String sha256(String value) {
    MessageDigest messageDigest = createMessageDigest();
    messageDigest.update(value.getBytes(StandardCharsets.UTF_8));
    byte[] digest = messageDigest.digest();
    byte[] base64Digest = Base64.getEncoder().encode(digest);
    return new String(base64Digest, StandardCharsets.UTF_8);
}

From source file:Main.java

/**
 * Calculation md5 hash of string//from ww  w . j  ava 2 s  .c o m
 * 
 * @param s - string for hash calculation
 * @return md5 hash of input string
 */
public static String MD5Hash(String s) {
    int sHash = s.hashCode();

    String result = md5Cache.get(sHash);

    if (result != null)
        return result;

    try {
        if (MD5 == null)
            MD5 = MessageDigest.getInstance("MD5");

        MessageDigest alg = (MessageDigest) MD5.clone();
        alg.update(s.getBytes());

        StringBuffer hexString = new StringBuffer(32);

        for (byte b : alg.digest())
            hexString.append(intToHexChars(0xFF & b));

        result = hexString.toString();
    } catch (Exception e) {
    }

    if (result == null)
        result = String.valueOf(s.hashCode());
    md5Cache.put(sHash, result);

    return result;
}

From source file:com.ykun.commons.utils.security.MD5Utils.java

/**
 * MD532??// w  ww  .  ja va2  s  .  c o m
 *
 * @param str
 * @return
 */
public static String md5(String str) {
    StringBuilder result = new StringBuilder();
    try {
        MessageDigest md5 = MessageDigest.getInstance(DIGEST);
        md5.update(str.getBytes(CHARSET_UTF8));
        byte[] b = md5.digest();
        for (int i = 0; i < b.length; ++i) {
            int x = b[i] & 0xFF;
            int h = x >>> 4;
            int l = x & 0x0F;
            result.append((char) (h + ((h < 10) ? '0' : 'a' - 10)));
            result.append((char) (l + ((l < 10) ? '0' : 'a' - 10)));
        }
    } catch (Exception e) {
        logger.error("md5 error:", e);
        throw new RuntimeException(e);
    }
    return result.toString();
}

From source file:com.beginner.core.utils.MD5.java

public static String md5(String str) {
    try {/*from  ww w.j a  v  a  2s.c o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte b[] = md.digest();

        int i;

        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        str = buf.toString();
    } catch (Exception e) {
        e.printStackTrace();

    }
    return str;
}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String md5Encrypt(String texto) {
    String encripted = null;/*from w w  w. ja  v a  2s .  co  m*/
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(texto.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        encripted = hash.toString(16);
    } catch (NoSuchAlgorithmException e) {
        log.error(e);
    }
    return encripted;
}

From source file:Main.java

public static String md5(String input) {
    String res = "";

    try {//ww w . ja v  a2s  . co m
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(input.getBytes());
        byte[] md5 = algorithm.digest();
        String tmp = "";
        for (int i = 0; i < md5.length; i++) {
            tmp = (Integer.toHexString(0xFF & md5[i]));
            if (tmp.length() == 1) {
                res += "0" + tmp;
            } else {
                res += tmp;
            }
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

    return res;
}

From source file:com.agiletec.plugins.jpnewsletter.aps.system.services.newsletter.util.ShaEncoder.java

public static String encodeWord(String word) throws NoSuchAlgorithmException {
    if (word != null) {
        MessageDigest digest = MessageDigest.getInstance("SHA");
        digest.update(word.getBytes());
        byte bytes[] = digest.digest();
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i] & 0xff;
            if (b < 16) {
                buffer.append("0");
            }/*w w  w  . j ava 2 s.  c  o  m*/
            buffer.append(Integer.toHexString(b));
        }
        word = buffer.toString();
    }
    return word;
}

From source file:Main.java

private static String getCertificateSHA1(X509Certificate certificate)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
    byte[] der = certificate.getEncoded();
    messageDigest.update(der);
    byte[] digest = messageDigest.digest();
    return hexify(digest);
}

From source file:Main.java

public static byte[] generateRequestToken(byte[] countryCode, byte[] phoneNumber)
        throws NoSuchAlgorithmException {

    String signature = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFDASBgNVBAcTC1NhbnRhIENsYXJhMRYwFAYDVQQKEw1XaGF0c0FwcCBJbmMuMRQwEgYDVQQLEwtFbmdpbmVlcmluZzEUMBIGA1UEAxMLQnJpYW4gQWN0b24wHhcNMTAwNjI1MjMwNzE2WhcNNDQwMjE1MjMwNzE2WjB8MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEUMBIGA1UEBxMLU2FudGEgQ2xhcmExFjAUBgNVBAoTDVdoYXRzQXBwIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRQwEgYDVQQDEwtCcmlhbiBBY3RvbjCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKBgQDRGYtLgWh7zyRtQainJfCpiaUbzjJuhMgo4fVWZIvXHaSHBU1t5w//S0lDK2hiqkj8KpMWGywVov9eZxZy37V26dEqr/c2m5qZ0E+ynSu7sqUD7kGx/zeIcGT0H+KAVgkGNQCo5Uc0koLRWYHNtYoIvt5R3X6YZylbPftF/8ayWTALBgcqhkjOOAQDBQADLwAwLAIUAKYCp0d6z4QQdyN74JDfQ2WCyi8CFDUM4CaNB+ceVXdKtOrNTQcc0e+t";
    String classesMd5 = "P3b9TfNFCkkzPoVzZnm+BA=="; // 2.11.395 [*]

    byte[] key2 = Base64.decode(
            "/UIGKU1FVQa+ATM2A0za7G2KI9S/CwPYjgAbc67v7ep42eO/WeTLx1lb1cHwxpsEgF4+PmYpLd2YpGUdX/A2JQitsHzDwgcdBpUf7psX1BU=",
            Base64.DEFAULT);/*w  w w . j a  v a2 s  . c o m*/

    byte[] data = concat(
            concat(Base64.decode(signature, Base64.DEFAULT), Base64.decode(classesMd5, Base64.DEFAULT)),
            phoneNumber);

    byte[] opad = new byte[64];
    byte[] ipad = new byte[64];
    for (int i = 0; i < opad.length; i++) {
        opad[i] = 0x5C;
        ipad[i] = 0x36;
    }

    for (int i = 0; i < 64; i++) {
        opad[i] = (byte) ((opad[i] & 0xFF) ^ (key2[i] & 0xFF));
        ipad[i] = (byte) ((ipad[i] & 0xFF) ^ (key2[i] & 0xFF));
    }

    data = concat(ipad, data);

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(data);
    byte[] firstHash = md.digest();

    data = concat(opad, firstHash);
    md = MessageDigest.getInstance("SHA-1");
    md.update(data);
    byte[] secondHash = md.digest();

    return Base64.encode(secondHash, Base64.DEFAULT | Base64.NO_WRAP);
}

From source file:com.cirrus.utils.EncryptionUtils.java

private static String makePasswordHash(final String value, final String salt) {
    try {/*  w  w w .  ja  v  a  2  s.  c  o m*/

        final String saltedAndHashed = value + "," + salt;
        final MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(saltedAndHashed.getBytes());
        final byte hashedBytes[] = (new String(digest.digest(), "UTF-8")).getBytes();
        final Base64 base64 = new Base64();
        return Arrays.toString(base64.encode(hashedBytes)) + '_' + salt;
    } catch (final NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 is not available", e);
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 unavailable?  Not a chance", e);
    }
}