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) throws NoSuchAlgorithmException 

Method Source Code

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

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

public class Main {
    private static final String EMPTY = "";

    public static String sha1(String input) throws NoSuchAlgorithmException {
        MessageDigest mDigest = MessageDigest.getInstance("SHA1");
        byte[] result = mDigest.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
        }/*  w  w w .  j av  a2 s . c om*/
        return sb.toString();
    }

    public static String substring(String str, int start) {
        if (str == null) {
            return null;
        }

        // handle negatives, which means last n characters
        if (start < 0) {
            start = str.length() + start; // remember start is negative
        }

        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return EMPTY;
        }

        return str.substring(start);
    }
}

Related

  1. sha1(String input)
  2. sha1(String input)
  3. sha1(String input)
  4. sha1(String input)
  5. sha1(String input)
  6. sha1(String input)
  7. sha1(String input)
  8. sha1(String input)
  9. sha1(String input, String encoding)