Java SHA1 SHA1(String str)

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

Description

SHA

License

Artistic License

Declaration

public static String SHA1(String str) 

Method Source Code


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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String SHA1(String str) {
        return encrypt(str, "SHA1");
    }//from w  w  w  .j  av  a2s  .co m

    public static String encrypt(String str, String enc) {
        MessageDigest md = null;
        String result = null;
        byte[] bt = str.getBytes();
        try {
            md = MessageDigest.getInstance(enc);
            md.update(bt);
            result = bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return result;
    }

    private static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;

        for (byte bt : bts) {
            tmp = (Integer.toHexString(bt & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
}

Related

  1. sha1(String s)
  2. sha1(String src)
  3. sha1(String src)
  4. sha1(String src)
  5. sha1(String srcStr)
  6. SHA1(String str)
  7. sha1(String str)
  8. sha1(String str)
  9. sha1(String str)