Java SHA1 sha1(final String data)

Here you can find the source of sha1(final String data)

Description

Returns the SHA-1 of the specified string

License

Apache License

Parameter

Parameter Description
data A string value

Return

The SHA-1 value

Declaration

public static final String sha1(final String data) 

Method Source Code

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

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

public class Main {
    /**//from  ww w  .j ava2  s.  c o  m
     * Returns the SHA-1 of the specified string
     *
     * @param data
     *            A string value
     * @return The SHA-1 value
     */
    public static final String sha1(final String data) {
        if (null == data) {
            throw new IllegalArgumentException("null string");
        }

        try {
            final MessageDigest md = MessageDigest.getInstance("SHA-1");
            return toHexString(md.digest(data.getBytes()));
        } catch (final NoSuchAlgorithmException e) {
            return null;
        }
    }

    /**
     * Returns the specified data as hex sequence
     *
     * @param data
     *            The data
     * @return a hex string
     */
    public static String toHexString(byte[] data) {
        final int n = data.length;
        final StringBuilder hex = new StringBuilder();

        for (int i = 0; i < n; i++) {
            final byte b = data[i];
            hex.append(String.format("%02x", b & 0xff));
        }

        return hex.toString();
    }
}

Related

  1. sha1(File file)
  2. sha1(File file)
  3. sha1(File sourceFile)
  4. sha1(final byte[] bytes)
  5. sha1(final File file)
  6. sha1(final String str)
  7. sha1(final String string)
  8. SHA1(final String text)
  9. sha1(final String text)