Java SHA1 digestSha1Hex(String source)

Here you can find the source of digestSha1Hex(String source)

Description

Hashes the source with SHA1 and returns the resulting hash as an hexadecimal string.

License

Apache License

Parameter

Parameter Description
source the text to hash.

Return

the SHA1 hash of the source, in hexadecimal form.

Declaration

public static String digestSha1Hex(String source) 

Method Source Code


//package com.java2s;
/*/*from w  w w  .j a v a 2 s. c  o m*/
 * Copyright (c) 2016 Couchbase, 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 java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class Main {
    /**
     * Hashes the source with SHA1 and returns the resulting hash as an hexadecimal string.
     *
     * @param source the text to hash.
     * @return the SHA1 hash of the source, in hexadecimal form.
     */
    public static String digestSha1Hex(String source) {
        String sha1 = "";
        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(source.getBytes("UTF-8"));
            sha1 = byteToHex(crypt.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sha1;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}

Related

  1. computeSHA1Hash(byte[] data)
  2. digestSHA1(byte[] data)
  3. digestSHA1(String data)
  4. digestSHA1(String login, String pass)
  5. digestSHA1(String text)
  6. doSHA1(byte[] buf)
  7. doSHA1(byte[] buf, int off, int len)
  8. generateSHA1(String message)
  9. generateSHA1ChecksumFile(String filename)