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:com.thinkgem.jeesite.modules.sso.util.Digests.java

/**
 * , ?md5sha1.//from  w  w  w  . j a va2  s.c o  m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        return "".getBytes();
    }
}

From source file:Main.java

public static String getMD5Value(String str, String fix, String charsetName) {
    if (str != null && fix != null) {
        String formalString = str + fix;
        try {// ww  w . j a v a  2  s.c om
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            try {
                algorithm.update(formalString.getBytes(charsetName));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                Log.e("WxException", e.getMessage(), e);
                return null;
            }
            byte messageDigest[] = algorithm.digest();

            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, e);
            Log.e("WxException", e.getMessage(), e);
        }
    }
    return null;

}

From source file:com.microsoft.aad.adal4j.AsymmetricKeyCredential.java

private static byte[] getHash(final byte[] inputBytes)
        throws NoSuchAlgorithmException, CertificateEncodingException {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(inputBytes);
    return md.digest();

}

From source file:JavaCloud.Utils.java

public static String calcHash(String password, String seed) {
    try {/* w w  w.  j a va2s . c o m*/
        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();

        String tuple = password + seed;

        crypt.update(tuple.getBytes("UTF-8"));
        return new BigInteger(1, crypt.digest()).toString(16);
    } catch (Exception e) {
        return null;
    }
}

From source file:$.Digests.java

/**
     * , ?md5sha1.// w  w  w. ja  v a2  s.  c  o  m
     */
    private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);

            if (salt != null) {
                digest.update(salt);
            }

            byte[] result = digest.digest(input);

            for (int i = 1; i < iterations; i++) {
                digest.reset();
                result = digest.digest(result);
            }
            return result;
        } catch (GeneralSecurityException e) {
            throw Exceptions.unchecked(e);
        }
    }

From source file:net.sourceforge.jaulp.crypto.sha.Hasher.java

/**
 * Hash.//from  w  ww.  j a  v  a2  s.  c o m
 *
 * @param hashIt
 *            the hash it
 * @param salt
 *            the salt
 * @param hashAlgorithm
 *            the hash algorithm
 * @param charset
 *            the charset
 * @return the string
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the MessageDigest object fails.
 */
public static String hash(String hashIt, String salt, HashAlgorithm hashAlgorithm, Charset charset)
        throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithm());
    messageDigest.reset();
    messageDigest.update(salt.getBytes(charset));
    return new String(messageDigest.digest(hashIt.getBytes(charset)), charset);
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.DigestsHelper.java

/**
 * , ?md5sha1.//from  w  ww . jav a  2 s.  c o  m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionHelper.unchecked(e);
    }
}

From source file:edu.cwru.apo.Auth.java

public static Hex md5(String in) {
    MessageDigest digest;
    try {/*from www.  j a v a2  s  .  c om*/
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());

        byte messageDigest[] = digest.digest();
        return new Hex(messageDigest);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.anybuy.util.DigestUtil.java

/**
 * , ?md5sha1./* w  w  w .j a va  2  s.c  om*/
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        throw ExceptionUtil.unchecked(e);
    }
}

From source file:com.apporiented.hermesftp.utils.SecurityUtil.java

/**
 * Calculates based on the passed parameters an hash code and returns its BASE64 representation. The
 * used algorithm is prepended./*from ww  w.  ja v a2  s .c  o m*/
 * 
 * @param password The password to encode.
 * @param algorithm The algorithm to use (MD5 e.g.)
 * @return The encoded password as string.
 * @throws NoSuchAlgorithmException Passed algorithm is not supported.
 */
public static String digestPassword(String password, String algorithm) throws NoSuchAlgorithmException {
    if (password == null) {
        throw new IllegalArgumentException("No password passed");
    }
    String result = password.trim();
    if (algorithm == null) {
        return result;
    }
    MessageDigest digest = MessageDigest.getInstance(algorithm);
    digest.update(password.getBytes());
    byte[] digestedPassword = digest.digest();
    String base64password = new String(Base64.encodeBase64(digestedPassword));
    return ALG_START + algorithm + ALG_END + base64password;
}