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:Main.java

private static String md5(String in) {
    MessageDigest digest;/*from   w ww  .j a  va 2s  .co  m*/
    try {
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Utils.java

public static String digest(String token) throws GeneralSecurityException, IOException {
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    return byteArrayToHexStr(sha1.digest(token.getBytes("UTF-8")));
}

From source file:Main.java

public static String signature(String source) {

    try {//from  w  ww  .  ja v a2  s . c  o  m
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.reset();
        md.update(source.getBytes());

        byte[] mdbytes = md.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
            String hex = Integer.toHexString(0xff & mdbytes[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();

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

From source file:Main.java

public static String getMD5(String input) {
    try {//ww  w . j ava  2  s.c o m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String hashtext = number.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String generateSHA1Hash(String textToBeHashed) {
    try {/*  w w  w .j  a v  a  2 s.c  om*/
        MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
        messageDigest.update(textToBeHashed.getBytes());
        byte[] sha1hash = messageDigest.digest();
        return getHexString(sha1hash);
    } catch (Exception e) {
        throw new RuntimeException("Couldn't generate SHA-1 hash for " + textToBeHashed);
    }
}

From source file:Main.java

public static String generateMD5Hash(String textToBeHashed) {
    try {//w w  w.j a  va 2 s  .  c o  m
        MessageDigest messageDigest = MessageDigest.getInstance(MD5);
        messageDigest.update(textToBeHashed.getBytes());
        byte[] messageDigestByte = messageDigest.digest();
        StringBuffer MD5Hash = new StringBuffer();
        String h;
        for (int i = 0; i < messageDigestByte.length; ++i) {
            h = Integer.toHexString((0xFF & messageDigestByte[i]) | 0x100).substring(1, 3);
            MD5Hash.append(h);
        }
        return MD5Hash.toString();
    } catch (Exception e) {
        throw new RuntimeException("Couldn't generate MD5 hash for " + textToBeHashed);
    }
}

From source file:Main.java

private static MessageDigest getMD5() throws NoSuchAlgorithmException {
    return MessageDigest.getInstance(ALGORIGTHM_MD5);
}

From source file:Main.java

public static String getMD5(String inStr) {
    MessageDigest md5;//  w w w . j a va 2 s . c o m
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

    char[] charArray = inStr.toCharArray();
    byte[] byteArray = new byte[charArray.length];
    for (int i = 0; i < charArray.length; i++) {
        byteArray[i] = (byte) charArray[i];
    }
    byte[] md5Bytes = md5.digest(byteArray);

    StringBuilder hexValue = new StringBuilder();
    for (byte md5Byte : md5Bytes) {
        int val = ((int) md5Byte) & 0xff;
        if (val < 16) {
            hexValue.append("0");
        }
        hexValue.append(Integer.toHexString(val));
    }
    return hexValue.toString();
}

From source file:Main.java

public static String sha512(String what_to_encode) {
    final MessageDigest sha512;
    try {//from   w ww.java  2 s . c o  m
        sha512 = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        return "404";
    }
    sha512.update(what_to_encode.getBytes());
    byte byteData[] = sha512.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 final static String md5(String value) {
    try {//www. j  a  v  a 2s .c om
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(value.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String md5 = number.toString(16);

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

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