Java SHA1 computeSha1AsHexString(String strToHash)

Here you can find the source of computeSha1AsHexString(String strToHash)

Description

Computes the SHA-1 hash value of the input string's UTF-8 representation and returns the result as a hex value in string form.

License

Apache License

Parameter

Parameter Description
strToHash The string to compute SHA-1 of.

Return

The SHA-1 hash value as a hex string.

Declaration

public static String computeSha1AsHexString(String strToHash) 

Method Source Code


//package com.java2s;
/*//w  w  w.ja  va2s .  c o  m
 * Copyright 2008 Google Inc.
 *
 * 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 com.google.common.base.Preconditions;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * Computes the SHA-1 hash value of the input string's UTF-8 representation and returns the result
     * as a hex value in string form.
     *
     * @param strToHash The string to compute SHA-1 of.
     * @return The SHA-1 hash value as a hex string.
     */
    public static String computeSha1AsHexString(String strToHash) {
        return computePartialSha1AsHexString(strToHash, 160);
    }

    /**
     * Computes the SHA-1 hash value of the input string's UTF-8 representation and returns the first
     * numBits bits of the result as a hex value in string form.
     *
     * @param strToHash The string to compute SHA-1 of.
     * @param numBits The number of bits worth to return. Must be a positive number at most 160 and
     *     divisible by 8 (since we process the result 8 bits at a time).
     * @return The partial SHA-1 hash value as a hex string.
     */
    public static String computePartialSha1AsHexString(String strToHash, int numBits) {

        Preconditions.checkArgument(numBits > 0 && numBits <= 160 && numBits % 8 == 0);
        int numBytes = numBits / 8;

        byte[] digestBytes;
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            digestBytes = md.digest(strToHash.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new AssertionError("Java should always have SHA-1.");
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError("Java should always have UTF-8.");
        }

        StringBuilder digestHexBuilder = new StringBuilder();
        for (int i = 0; i < numBytes; i++) {
            byte digestByte = digestBytes[i];
            String digestByteHex = Integer.toHexString(0xFF & digestByte);
            if (digestByteHex.length() == 1) {
                digestByteHex = "0" + digestByteHex; // pad to 2 digits
            }
            digestHexBuilder.append(digestByteHex);
        }
        return digestHexBuilder.toString();
    }
}

Related

  1. computeSHA1(byte[] ba)
  2. computeSha1(byte[] data)
  3. computeSHA1(ByteBuffer convertme, int offset, int len)
  4. computeSha1(ReadableByteChannel channel)
  5. computeSHA1Hash(byte[] data)
  6. digestSHA1(byte[] data)
  7. digestSHA1(String data)
  8. digestSHA1(String login, String pass)