Java SHA1 sha1(String string)

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

Description

Hashes the given string using the SHA-1 algorithm.

License

Open Source License

Declaration

public static byte[] sha1(String string) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

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

public class Main {
    /** Hashes the given {@code string} using the SHA-1 algorithm. */
    public static byte[] sha1(String string) {
        return hash("SHA-1", string);
    }/*from   ww  w.  j a v  a2s . c  om*/

    /** Hashes the given {@code string} using the given {@code algorithm}. */
    public static byte[] hash(String algorithm, String string) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException ex) {
            throw new IllegalArgumentException(String.format("[%s] isn't a valid hash algorithm!", algorithm), ex);
        }

        byte[] bytes;
        try {
            bytes = string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException(ex);
        }

        return digest.digest(bytes);
    }
}

Related

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