Java SHA1 sha1(String input)

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

Description

sha

License

Apache License

Declaration

public static String sha1(String input) 

Method Source Code


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

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String ENCRY_MD5 = "md5";
    public static final String ENCRY_SHA = "sha-1";

    public static String sha1(String input) {
        return encrypt(input, ENCRY_SHA);
    }//  w  ww.j av a2 s  .  co m

    public static String encrypt(String input, String... algorithms) {
        String algorithm = ENCRY_MD5;
        if (input == null || "".equals(input.trim())) {
            throw new IllegalArgumentException("Please input the encrypted content !");
        }
        if (algorithms.length > 0) {
            algorithm = algorithms[0];
        }
        byte[] bytes = null;
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(input.getBytes("UTF8"));
            bytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return byte2hex(bytes);
    }

    private static String byte2hex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; ++i) {
            sb.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    }
}

Related

  1. sha1(String data)
  2. sha1(String data)
  3. sha1(String data)
  4. SHA1(String decript)
  5. sha1(String input)
  6. sha1(String input)
  7. sha1(String input)
  8. sha1(String input)
  9. sha1(String input)