Java SHA1 sha1DigestAsHex(String data)

Here you can find the source of sha1DigestAsHex(String data)

Description

Returns the SHA1 of the provided data

License

Apache License

Parameter

Parameter Description
data The data to calculate, such as the contents of a file

Return

The human-readable SHA1

Declaration

public static String sha1DigestAsHex(String data) 

Method Source Code

//package com.java2s;
/*/*www .ja v a  2 s.  c  o m*/
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private static final Charset UTF8_CHARSET = Charset.forName("UTF8");

    /**
     * Returns the SHA1 of the provided data
     * 
     * @param data The data to calculate, such as the contents of a file
     * @return The human-readable SHA1
     */
    public static String sha1DigestAsHex(String data) {
        byte[] dataBytes = getDigest("SHA").digest(
                data.getBytes(UTF8_CHARSET));
        return new String(encodeHex(dataBytes));
    }

    /**
     * Creates a new {@link MessageDigest} with the given algorithm. Necessary because {@code MessageDigest} is not
     * thread-safe.
     */
    private static MessageDigest getDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException ex) {
            throw new IllegalStateException(
                    "Could not find MessageDigest with algorithm \""
                            + algorithm + "\"", ex);
        }
    }

    private static char[] encodeHex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = HEX_CHARS[(0xF0 & data[i]) >>> 4];
            out[j++] = HEX_CHARS[0x0F & data[i]];
        }
        return out;
    }
}

Related

  1. SHA1Checksum(String filename)
  2. sha1Digest(byte[] bytes)
  3. sha1Digest(byte[] bytes)
  4. sha1Digest(final InputStream data)
  5. sha1Digest(String src)
  6. sha1Encode(byte[] content)
  7. SHA1Encode(String sourceString)
  8. sha1Hash()
  9. sha1hash(byte[] blob)