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

public static String cryptoStr2MD5(String str) {
    try {/*w ww .j  ava2 s . co m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] bytes = md.digest();
        return byte2Hex(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] createDigest(String passcode, long t1, double q1)
        throws IOException, NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(passcode.getBytes());/*from  w w w .j  av a 2 s.  c o  m*/
    ByteBuffer bb = ByteBuffer.allocate(16); //8 bytes for long and double each
    bb.putLong(t1);
    bb.putDouble(q1);
    md.update(bb);
    return md.digest();
}

From source file:Main.java

public static String cryptoStr2SHA1(String str) {
    try {// www  .  j a v  a  2 s .  c o  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(str.getBytes());
        byte[] bytes = md.digest();
        return byte2Hex(bytes);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    text = "l;jfa@#" + text + "fads@)*3 ";
    MessageDigest md;/* w w w .j  av  a 2s  .  c om*/
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
}

From source file:Main.java

public final static String MD5(String s) throws Exception {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    byte[] strTemp = s.getBytes();
    MessageDigest mdTemp = MessageDigest.getInstance("MD5");
    mdTemp.update(strTemp);/*from www. ja  v  a2s .  c  om*/
    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);
}

From source file:Sha1.java

public static String sha1(String text) {
    try {/*from w w w  .ja v a 2 s .c om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        md.update(text.getBytes("iso-8859-1"), 0, text.length());

        return convertToHex(md.digest());
    } catch (Exception e) {
        throw new Exception(format("Problem hashing %s", text), e);
    }
}

From source file:Main.java

public static String md5(String str) {
    if (TextUtils.isEmpty(str)) {
        return null;
    }//from  w w  w  . j av  a2 s .  c o  m
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(str.getBytes());
        return encodeHex(messageDigest.digest());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:MainClass.java

static void performOutputTest() throws Exception {
    MessageDigest md = MessageDigest.getInstance("SHA");
    FileOutputStream fout = new FileOutputStream("sha-results.txt");
    DigestOutputStream out = new DigestOutputStream(fout, md);
    byte[] b = "testCase".getBytes();
    out.write(b, 0, b.length);/*from   ww w  .  j a  v a  2  s .c o  m*/
    md = out.getMessageDigest();
    String s = new String(md.digest());
    System.out.println("Calculated result: " + s);
}

From source file:Main.java

public static String str2md5(String str) {
    try {/*  w w w  . ja  va  2 s  . c o m*/
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(HEX_DIGITS[b >> 4 & 0xf]);
            hexString.append(HEX_DIGITS[b & 0xf]);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static final String MD5(String paramString) {
    char[] arrayOfChar1 = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };
    try {//from w w w .  ja  v a  2s .c  o  m
        byte[] arrayOfByte1 = paramString.getBytes();
        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
        localMessageDigest.update(arrayOfByte1);
        byte[] arrayOfByte2 = localMessageDigest.digest();
        int i = arrayOfByte2.length;
        char[] arrayOfChar2 = new char[i * 2];
        int j = 0;
        int k = 0;
        while (true) {
            if (j >= i)
                return new String(arrayOfChar2);
            int m = arrayOfByte2[j];
            int n = k + 1;
            arrayOfChar2[k] = arrayOfChar1[(0xF & m >>> 4)];
            k = n + 1;
            arrayOfChar2[n] = arrayOfChar1[(m & 0xF)];
            j++;
        }
    } catch (Exception localException) {
        localException.printStackTrace();
    }
    return null;
}