Android SHA1 Hash Create computeHashSHA1(final String text)

Here you can find the source of computeHashSHA1(final String text)

Description

Returns a hash value for a string using SHA-1 algorithm

License

Apache License

Parameter

Parameter Description
text a string to compute a hash value for

Exception

Parameter Description
NoSuchAlgorithmException if hash algorithm is not available

Return

a uppercase hash value for the given text

Declaration

public static String computeHashSHA1(final String text)
        throws NoSuchAlgorithmException 

Method Source Code

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

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

public class Main {
    /**//from w w w . j a v a2s. c o m
     * Returns a hash value for a string using SHA-1 algorithm
     *
     * @param text a string to compute a hash value for
     * @return a uppercase hash value for the given text
     * @throws NoSuchAlgorithmException if hash algorithm is not available
     */
    public static String computeHashSHA1(final String text)
            throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(text.getBytes(), 0, text.length());

        // get sha-1 bytes
        byte hashData[] = md.digest();

        // create a hex string
        StringBuilder sb = new StringBuilder(hashData.length * 2);

        for (int i = 0; i < hashData.length; i++) {
            int b = (0xFF & hashData[i]);
            // if it is a single digit, make sure it have 0 in front (proper padding)
            if (b <= 0xF)
                sb.append('0');
            // add number to string
            sb.append(Integer.toHexString(b));
        }

        // hex string to uppercase
        return sb.toString().toUpperCase();
    }
}

Related

  1. sha1(String input)
  2. sha1(String ori)
  3. sha1(String ori)
  4. sha1(String s)
  5. sha1(String s)
  6. computeSHAHash(String password)
  7. getSHA1CertFingerprint(Context ctx)
  8. SHA1(String text)
  9. shaByte(String in)