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:eds.component.encryption.EncryptionUtility.java

public static String hashText(String text, EncryptionType type) {
    byte[] hash;/*www .  j a va 2  s.  c om*/
    MessageDigest md;
    try {
        //md = MessageDigest.getInstance("SHA-256");
        md = MessageDigest.getInstance(type.toString());
        hash = md.digest(text.getBytes("UTF-8"));
        String hashedText = new String(Hex.encodeHex(hash));//String.format("%032x", new BigInteger(hash));;//String.format("%032x", Arrays.toString(hash));

        return hashedText;
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
        throw new RuntimeException("No such algorithm or encoding method: " + ex.getMessage());
    }
}

From source file:de.hybris.platform.b2b.punchout.services.impl.AsymmetricManager.java

/**
 * Generates a hash using asymmetric encryption.
 * //from  w ww  .  j  a v  a2  s.  c  om
 * @param unsecureText
 *           The text to be hashed.
 * @param salt
 *           The salt used to defend against dictionary and rainbow table attacks.
 * @return The hash.
 */
public static String getHash(final String unsecureText, final String salt) {
    try {
        final MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
        digest.update(unsecureText.getBytes(CHAR_SET));
        digest.update(salt.getBytes(CHAR_SET));
        final byte[] byteData = digest.digest();

        //convert the byte to hex format method 1
        final 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();
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        // should never happen
        LOG.error("System was unable to generate the hash.", e);
    }
    return null;
}

From source file:apm.common.security.Digests.java

/**
 * , ?md5sha1./*from   ww 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 Exceptions.unchecked(e);
    }
}

From source file:com.njmd.framework.utils.EncodeUtil.java

/**
 * MD5?//from   w  ww .j ava  2s  .  c  om
 */
public static String encodeMD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    try {
        byte[] strTemp = s.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        byte[] md = mdTemp.digest();
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (Exception e) {
        return null;
    }
}

From source file:cn.zhuqi.mavenssh.ext.util.Coder.java

/**
 * SHA/*w w  w.  j av  a 2  s .c  om*/
 *
 * @param data
 * @return
 * @throws Exception
 */
public static byte[] encryptSHA(byte[] data) throws Exception {
    MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
    sha.update(data);
    return sha.digest();
}

From source file:Main.java

public static boolean verifyChecksum(byte[] bytesWithChecksumm) {
    try {/* ww  w .  j  a va 2s  . com*/
        if (bytesWithChecksumm == null || bytesWithChecksumm.length < 5) {
            return false;
        }
        MessageDigest digestSha = MessageDigest.getInstance("SHA-256");
        digestSha.update(bytesWithChecksumm, 0, bytesWithChecksumm.length - 4);
        byte[] first = digestSha.digest();
        byte[] calculatedDigest = digestSha.digest(first);
        boolean checksumValid = true;
        for (int i = 0; i < 4; i++) {
            if (calculatedDigest[i] != bytesWithChecksumm[bytesWithChecksumm.length - 4 + i]) {
                checksumValid = false;
            }
        }
        return checksumValid;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.modoop.core.security.utils.Digests.java

/**
 * , ?md5sha1.//www.  j av  a2  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 ExceptionUtils.unchecked(e);
    }
}

From source file:com.fortinet.qcdb.AscenLinkKey.java

private String do_md5(String sn, String func, int argc, String[] argv) {
    try {/*ww  w.j  a va  2  s  . c  o  m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update("DEMO_TTL".getBytes());
        m.update(sn.getBytes());
        m.update("*%#($a3EB".getBytes());
        m.update(func.getBytes());
        for (int i = 0; i < argc; i++)
            m.update(argv[i].getBytes());

        Base32 b32 = new Base32();
        byte[] k = b32.encode(m.digest());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 25; i++) {
            if (i > 0 && i % 5 == 0)
                sb.append("-");
            sb.append((char) k[i]);
        }

        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:edu.mit.media.funf.security.HashUtil.java

public static MessageDigest getMessageDigest() {
    if (instance == null) {
        try {/*from   w ww  .j a v  a  2s  .  c o m*/
            instance = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            Log.e(LogUtil.TAG, "HashUtil no SHA alghrithom", e);
            return null;
        }
    }
    return instance;
}

From source file:CryptPassword.java

/**
 * Generates the MD5 hash from a given string.
 * //from w  ww  .  ja v  a2 s. co m
 * @param inputString
 *            is the input String
 * @return the MD5 from the string used.
 */
public static String getMD5Hash(String inputString) {
    byte buf[] = inputString.getBytes();
    StringBuffer hexString = new StringBuffer();
    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(buf);
        byte[] digest = algorithm.digest();
        for (int i = 0; i < digest.length; i++) {
            hexString.append(pad(Integer.toHexString(0xFF & digest[i]), 2));

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hexString.toString();
}