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 byte[] getHash(String password) {
    MessageDigest digest = null;//from ww  w .  j  av  a2  s .  c om
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    }
    if (digest != null) {
        digest.reset();
        return digest.digest(password.getBytes());
    } else {
        return null;
    }

}

From source file:Main.java

public static byte[] computeSHA1(byte[] convertme, int offset, int len) {
    try {/*from w ww .j a v  a  2  s  .  c o m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(convertme, offset, len);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[20];
}

From source file:Main.java

public static String stringToMD5(String string) {
    byte[] hash;//w ww  .  j a  v a2  s .  co  m

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

private static byte[] getSHA1Digest(String data) throws IOException {
    byte[] bytes = null;
    try {/*from  w w  w. j a va 2s .  c  o m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        bytes = md.digest(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse.getMessage());
    }
    return bytes;
}

From source file:Main.java

public static String md5(final String in) {
    MessageDigest digest;//from ww  w  . j a v a 2s. c  o  m
    try {
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        final byte[] a = digest.digest();
        final int len = a.length;
        final 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 (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String sha1(String string) {
    byte[] hash;//  www  .  jav  a2 s.c o  m
    try {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(string.getBytes("UTF-8"));
        hash = sha1.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, SHA-1 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, SHA-1 UTF-8 should be supported?", e);
    }
    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

private static byte[] getSHA1Digest(String data) throws IOException {
    byte[] bytes = null;
    try {//from w ww. j  av a 2  s  .c  om
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        bytes = md.digest(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        throw new IOException(gse);
    }
    return bytes;
}

From source file:Main.java

public static String hashImgUrl(String imgUrl) throws NoSuchAlgorithmException {
    String imgKey = null;//from   w  w w.  j  ava  2 s  .c om
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();
    m.update(imgUrl.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    imgKey = bigInt.toString(16);
    while (imgKey.length() < 32)
        imgKey = "0" + imgKey;
    return imgKey;
}

From source file:Main.java

public static byte[] dataDigest(String in) {
    byte[] sum = null;

    try {/*from  www.  j  a  v  a2s  .  c  om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] bytes = in.getBytes("UTF8");
        sum = md.digest(bytes);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return sum;
}

From source file:Main.java

public static byte[] hash(String message) {
    MessageDigest cript = null;/*from  w ww  . jav a  2  s  .c  o m*/
    try {
        cript = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    cript.reset();
    try {
        cript.update(message.getBytes("utf8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return cript.digest();
}