Java SHA1 sha1(String src)

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

Description

Calculates the SHA-1 digest and returns the value as a 40 character hex string.

License

Apache License

Parameter

Parameter Description
src a string to digest

Return

SHA-1 digest as a hex string

Declaration

public static String sha1(String src) 

Method Source Code


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

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static MessageDigest md;

    /**/*from   www.j  a v a 2  s.com*/
     * Calculates the SHA-1 digest and returns the value as a 40 character hex
     * string.
     * 
     * @param src
     *            a string to digest
     * @return SHA-1 digest as a hex string
     */
    public static String sha1(String src) {
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        md.update(src.getBytes(StandardCharsets.UTF_8));

        return String.format("%040x", new BigInteger(1, md.digest()));
    }
}

Related

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