Java SHA1 sha1(String str)

Here you can find the source of sha1(String str)

Description

sha

License

Apache License

Declaration

public static String sha1(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;

public class Main {

    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String sha1(String str) {
        MessageDigest md;/*from   ww w  .ja  va 2  s.co  m*/
        try {
            md = MessageDigest.getInstance("sha1");
            md.update(str.getBytes());
            byte[] hash = md.digest();
            return toHexString(hash);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String toHexString(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }
}

Related

  1. sha1(String str)
  2. sha1(String str)
  3. sha1(String str)
  4. sha1(String str)
  5. sha1(String str)
  6. sha1(String string)
  7. sha1(String string)
  8. SHA1(String strs)
  9. sha1(String text)