Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:com.agiletec.plugins.jpuserreg.aps.system.services.userreg.util.ShaEncoder.java

public static String encodePassword(String password) throws NoSuchAlgorithmException {

    if (password != null) {

        MessageDigest digest = MessageDigest.getInstance("SHA");

        digest.update(password.getBytes());

        byte bytes[] = digest.digest();

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < bytes.length; i++) {

            int b = bytes[i] & 0xff;

            if (b < 16) {

                buffer.append("0");

            }//from   w  w  w. j  a  v  a 2s  . com

            buffer.append(Integer.toHexString(b));

        }

        password = buffer.toString();

    }

    return password;

}

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 {// w w  w. j  a  v  a 2  s.c o  m
            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:ch.windmobile.server.social.mongodb.util.AuthenticationServiceUtil.java

public static boolean validateSHA1(String email, Object password, String base64)
        throws NoSuchAlgorithmException {
    if (password == null) {
        throw new IllegalArgumentException("password cannot be null");
    }// w  w w .j a  va2s.c  om
    MessageDigest md = MessageDigest.getInstance("SHA1");
    String base = email + ":" + password.toString();
    byte[] result = md.digest(base.getBytes());
    byte[] data = Base64.decode(base64.getBytes());
    return MessageDigest.isEqual(data, result);
}

From source file:org.sglover.alfrescoextensions.common.HasherImpl.java

public HasherImpl() throws NoSuchAlgorithmException {
    md5 = MessageDigest.getInstance("MD5");
}

From source file:customerproject.customerutilities.Utilities.java

public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {

    MessageDigest md;//from w ww . j  a v  a  2  s.c  om
    md = MessageDigest.getInstance("MD5");
    byte[] md5hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5hash = md.digest();
    return convertToHex(md5hash);
}

From source file:elaborate.util.PasswordUtil.java

private static MessageDigest createDigester() {
    try {/*from www  .j  a v  a 2s.  co  m*/
        return MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.baidu.rigel.biplatform.ac.util.Md5Util.java

/**
 * * MD5?/* w w w  .  j  a v  a 2  s .  co m*/
 * 
 * @param rawPass 
 * @param salt ?
 * @return md5
 * @throws IllegalArgumentException ?
 */
public static String encode(String rawPass, Object salt) {
    if (StringUtils.isBlank(rawPass)) {
        throw new IllegalArgumentException("encode string can not be empty!");
    }
    String saltedPass = mergePasswordAndSalt(rawPass, salt);
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] digest = messageDigest.digest(saltedPass.getBytes());
        return new String(decodeByteArray(digest));
    } catch (NoSuchAlgorithmException e) {
        return rawPass;
    }
}

From source file:com.floreantpos.util.PasswordHasher.java

public static String hashPassword(String password) {
    byte[] passwordBytes = null;
    MessageDigest md = null;//from   w w w .  j  a  va2 s .  c o  m

    try {
        passwordBytes = password.getBytes("UTF-8"); //$NON-NLS-1$
    } catch (UnsupportedEncodingException e) {
        PosLog.error(PasswordHasher.class, e.getMessage());
    }

    try {
        md = MessageDigest.getInstance("SHA1"); //$NON-NLS-1$
    } catch (NoSuchAlgorithmException e) {
        PosLog.error(PasswordHasher.class, e.getMessage());
    }

    byte[] hashBytes = md.digest(passwordBytes);

    return Hex.encodeHexString(hashBytes);

}

From source file:de.topicmapslab.majortom.server.http.util.MD5Util.java

/**
 * Calculates the md5 sum of the given string
 * /*from  www  . j  a v  a2  s .c om*/
 * @param source the source string used to calculate
 * @return the String representing the MD5 sum
 * @throws NoSuchAlgorithmException  if the MD5 algorithm is not available
 */
public static final String calculateMD5(String source) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] bytes = digest.digest(source.getBytes());

        final int nBytes = bytes.length;
        char[] result = new char[2 * nBytes];

        int j = 0;
        for (int i = 0; i < nBytes; i++) {
            // Char for top 4 bits
            result[j++] = HEX[(0xF0 & bytes[i]) >>> 4];
            // Bottom 4
            result[j++] = HEX[(0x0F & bytes[i])];
        }

        return new String(result);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Could not calculate md5", e);
    }
}

From source file:StringUtil.java

/**
 * Encrypt password by using SHA-256 algorithm, encryptedPassword length is 32 bits
 * @param clearTextPassword/* ww w.j a v a 2 s .co  m*/
 * @return
 * @throws NoSuchAlgorithmException
 * reference http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html
 */
public static String getEncryptedPassword(String clearTextPassword) {

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(clearTextPassword.getBytes());
        return new sun.misc.BASE64Encoder().encode(md.digest());
    } catch (NoSuchAlgorithmException e) {
        //_log.error("Failed to encrypt password.", e);
    }
    return "";
}