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:support.AuthManager.java

public static String md5Custom(String st) {
    MessageDigest messageDigest = null;
    byte[] digest = new byte[0];

    try {/*  w ww  .ja v  a  2  s .c  o  m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(st.getBytes(StandardCharsets.UTF_8));
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    BigInteger bigInt = new BigInteger(1, digest);
    String md5Hex = bigInt.toString(16);

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

    return md5Hex;
}

From source file:com.tlabs.eve.api.EveRequest.java

public static final String md5(EveRequest<? extends EveResponse> request) {
    String key = request.getClass().getSimpleName() + request.getPage();

    Map<String, String> params = request.getParameters();
    for (String p : params.keySet()) {
        key = appendKey(key, p + "=" + params.get(p));
    }/* www.  j  ava2  s  .  com*/
    try {
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(key.getBytes());
        return new String(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

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

public static String getDigestHashOldFunnyHexEncode(String str, String hashType) {
    if (UtilValidate.isEmpty(hashType))
        hashType = "SHA";
    if (str == null)
        return null;
    try {/*from  w  ww  .  j a va2s. c o  m*/
        MessageDigest messagedigest = MessageDigest.getInstance(hashType);
        byte strBytes[] = str.getBytes();

        messagedigest.update(strBytes);
        byte digestBytes[] = messagedigest.digest();
        int k = 0;
        char digestChars[] = new char[digestBytes.length * 2];

        for (int l = 0; l < digestBytes.length; l++) {
            int i1 = digestBytes[l];

            if (i1 < 0) {
                i1 = 127 + i1 * -1;
            }
            StringUtil.encodeInt(i1, k, digestChars);
            k += 2;
        }

        return new String(digestChars, 0, digestChars.length);
    } catch (Exception e) {
        Debug.logError(e, "Error while computing hash of type " + hashType, module);
    }
    return str;
}

From source file:com.innoq.liqid.utils.SHACrypt.java

public static String encrypt(final String plaintext) {
    MessageDigest md = null;
    try {/*from   w  w  w .  ja v a 2 s . c o m*/
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    try {
        md.update(plaintext.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage());
    }
    byte[] raw = md.digest();
    return Base64.encodeBase64String(raw);
}

From source file:ECToken3.java

public static final String decryptv3(String key, String input) throws java.io.UnsupportedEncodingException,
        java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException,
        java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException,
        javax.crypto.BadPaddingException, java.security.InvalidAlgorithmParameterException {

    //----------------------------------------------------
    // Base64 decode
    //----------------------------------------------------
    String result = null;//from   w ww . j  av a  2  s . co  m
    Base64 encoder = new Base64(true);
    byte[] inputBytes = encoder.decode(input.getBytes("ASCII"));

    //----------------------------------------------------
    // Get SHA-256 of key
    //----------------------------------------------------
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(key.getBytes("ASCII"));
    byte[] keyDigest = md.digest();

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| Decrypt\n");
    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| key:                   %s\n", key);
    //System.out.format("| token:                 %s\n", input);

    //----------------------------------------------------
    // Rip up the ciphertext
    //----------------------------------------------------
    byte[] ivBytes = new byte[12];
    ivBytes = Arrays.copyOfRange(inputBytes, 0, ivBytes.length);

    byte[] cipherBytes = new byte[inputBytes.length - ivBytes.length];
    cipherBytes = Arrays.copyOfRange(inputBytes, ivBytes.length, inputBytes.length);

    //----------------------------------------------------
    // Decrypt
    //----------------------------------------------------
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(keyDigest), MAC_SIZE_BITS, ivBytes));

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| iv:                    %s\n", bytesToHex(ivBytes));
    //System.out.format("| ciphertext:            %s\n", bytesToHex(Arrays.copyOfRange(cipherBytes, 0, cipherBytes.length - 16)));
    //System.out.format("| tag:                   %s\n", bytesToHex(Arrays.copyOfRange(cipherBytes, cipherBytes.length - 16, cipherBytes.length)));
    //System.out.format("+-------------------------------------------------------------\n");

    byte[] dec = new byte[cipher.getOutputSize(cipherBytes.length)];

    try {
        int res = cipher.processBytes(cipherBytes, 0, cipherBytes.length, dec, 0);
        cipher.doFinal(dec, res);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    //System.out.format("token: %s\n", new String(dec, "ASCII"));
    return new String(dec, "ASCII");
}

From source file:com.slimsmart.common.util.code.EncodeUtil.java

public static String SHA1Encode(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest m = MessageDigest.getInstance("sha-1");
    m.update(input.getBytes("UTF8"));
    byte s[] = m.digest();
    return hex(s);
}

From source file:Main.java

public static byte[] calculateMd5(byte[] binaryData) {
    MessageDigest messageDigest = null;
    try {/*from   www. j  a v  a  2 s  .c  o  m*/
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 algorithm not found.");
    }
    messageDigest.update(binaryData);
    return messageDigest.digest();

}

From source file:com.fabernovel.alertevoirie.webservice.HttpPostRequest.java

private static String sha1(String raw) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] bytes = raw.getBytes("UTF-8");
    MessageDigest md = MessageDigest.getInstance(MGF1ParameterSpec.SHA1.getDigestAlgorithm());
    md.update(bytes);
    byte[] digest = md.digest();

    return toHex(digest);
}

From source file:ECToken3.java

public static final String encryptv3(String key, String input) throws java.io.UnsupportedEncodingException,
        java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException,
        java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException,
        javax.crypto.BadPaddingException, java.security.InvalidAlgorithmParameterException {

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| Encrypt\n");
    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| key:                   %s\n", key);
    //System.out.format("| token:                 %s\n", input);

    //----------------------------------------------------
    // Get SHA-256 of key
    //----------------------------------------------------
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(key.getBytes("ASCII"));
    byte[] keyDigest = md.digest();

    //----------------------------------------------------
    // Get Random IV
    //----------------------------------------------------
    SecureRandom random = new SecureRandom();
    byte[] ivBytes = new byte[12];
    random.nextBytes(ivBytes);/*w w  w .j  a  v a 2s.c  o  m*/

    //----------------------------------------------------
    // Encrypt
    //----------------------------------------------------
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(true, new AEADParameters(new KeyParameter(keyDigest), MAC_SIZE_BITS, ivBytes));
    byte[] inputBytes = input.getBytes("ASCII");

    byte[] enc = new byte[cipher.getOutputSize(inputBytes.length)];

    try {
        int res = cipher.processBytes(inputBytes, 0, inputBytes.length, enc, 0);
        cipher.doFinal(enc, res);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    byte[] ivPlusCipherText = new byte[ivBytes.length + enc.length];
    System.arraycopy(ivBytes, 0, ivPlusCipherText, 0, ivBytes.length);
    System.arraycopy(enc, 0, ivPlusCipherText, ivBytes.length, enc.length);

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| iv:                    %s\n", bytesToHex(ivBytes));
    //System.out.format("| ciphertext:            %s\n", bytesToHex(Arrays.copyOfRange(enc, 0, enc.length - 16)));
    //System.out.format("| tag:                   %s\n", bytesToHex(Arrays.copyOfRange(enc, enc.length - 16, enc.length)));
    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| token:                 %s\n", bytesToHex(ivPlusCipherText));
    //System.out.format("+-------------------------------------------------------------\n");

    String result = null;
    byte[] temp = null;
    Base64 encoder = new Base64(0, temp, true);
    byte[] encodedBytes = encoder.encode(ivPlusCipherText);
    String encodedStr = new String(encodedBytes, "ASCII").trim();
    String encodedStrTrim = encodedStr.trim();
    return encodedStr.trim();
}

From source file:com.alkacon.opencms.commons.CmsStringCrypter.java

/**
 * Converts the given password to machine readable form.<p>
 * /*from w  w w  . ja v  a 2  s. c o m*/
 * @param password the password to convert to a machine readable key
 * @return the password in machine readable form
 */
private static byte[] getKey(String password) {

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(password.toString().getBytes());
        byte[] key = md5.digest();
        // now get the first 8 bytes
        byte[] finalKey = new byte[8];
        for (int i = 0; i <= 7; i++) {
            finalKey[i] = key[i];
        }
        return finalKey;
    } catch (NoSuchAlgorithmException ex) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_CREATE_KEY_0), ex);
        }
    }
    return null;
}