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.jwt.security.auth.PasswordEncryption.java

public String getMD5EncryptedPassword(String password) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(password.getBytes());/*from   w ww . j  a v a2  s . com*/

    byte byteData[] = md.digest();

    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();
}

From source file:Main.java

public static byte[] sha256(byte[] data) throws NoSuchAlgorithmException {
    return MessageDigest.getInstance("SHA-256").digest(data);
}

From source file:com.thinkgem.jeesite.modules.sso.util.Digests.java

/**
 * , ?md5sha1./*  w  ww  . j  a  v a 2  s  .co 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 boolean verifyChecksum(byte[] bytesWithChecksumm) {
    try {/*  w  w w . j a va 2s .  co m*/
        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 (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:ke.co.tawi.babblesms.server.utils.security.SecurityUtil.java

/**
 * Return the MD5 hahs of a String. It will work correctly for most 
 * strings. A word which does not work correctly is "michael" (check 
 * against online MD5 hash tools).   //  ww  w . j  a va  2  s .  c  om
 *
 * @param toHash plain text string to encryption
 * @return an md5 hashed string
 */
public static String getMD5Hash(String toHash) {
    String md5Hash = "";

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(toHash.getBytes(), 0, toHash.length());

        md5Hash = new BigInteger(1, md.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException while getting MD5 hash of '" + toHash + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    return md5Hash;
}

From source file:it.infn.mw.iam.util.ssh.RSAPublicKeyUtils.java

private static String buildMD5Fingerprint(String key) throws InvalidSshKeyException {

    String fingerprint = null;/*from w ww. j a  va 2  s  .  c  o  m*/

    try {

        byte[] decodedKey = Base64.getDecoder().decode(key);
        byte[] digest = MessageDigest.getInstance(MessageDigestAlgorithms.MD5).digest(decodedKey);
        fingerprint = Hex.encodeHexString(digest);

    } catch (Exception e) {

        throw new InvalidSshKeyException("Error during fingerprint generation: RSA key is not base64 encoded",
                e);
    }

    return fingerprint;
}

From source file:mx.com.pendulum.carga.util.Md5Converter.java

public static String getMD5Checksum(MultipartFile multipartFile) {
    InputStream is = null;// ww  w  . j av a 2s.  c o m
    try {
        is = new ByteArrayInputStream(multipartFile.getBytes());
        byte[] buffer = new byte[1024];
        MessageDigest digest = MessageDigest.getInstance("MD5");
        int numRead = 0;
        while (numRead != -1) {
            numRead = is.read(buffer);
            if (numRead > 0) {
                digest.update(buffer, 0, numRead);
            }
        }
        byte[] md5Bytes = digest.digest();
        return convertHashToString(md5Bytes);
    } catch (Exception e) {
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.omnigon.aem.handlebars.helpers.UniqueId.java

public static String generateUniqueId(String directoryPath) {
    byte[] bytesDirectoryPath = null;
    MessageDigest md = null;/*from w  ww . j a  v a2s  . c o  m*/
    try {
        bytesDirectoryPath = directoryPath.getBytes(CharEncoding.UTF_8);
        md = MessageDigest.getInstance(MESSAGE_DIGEST_TYPE);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
        return StringUtils.EMPTY;
    }
    md.reset();
    md.update(bytesDirectoryPath);
    String uniqueId = DatatypeConverter.printHexBinary(md.digest());
    return StringUtils.substring(uniqueId, 0, UNIQUE_ID_LENGTH);
}

From source file:com.idea.quickstart.common.security.Digests.java

/**
 * , ?md5sha1./*from ww w.  j  ava  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) {
    }
    return null;
}

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());/*from   www .  j  a  va 2  s .co m*/
        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");
            }
            buffer.append(Integer.toHexString(b));
        }
        word = buffer.toString();
    }
    return word;
}