Java Hash Code Calculate generateHash(String tcString)

Here you can find the source of generateHash(String tcString)

Description

Generates a SHA-1 hashcode from the input

License

Open Source License

Parameter

Parameter Description
tcString the string to hash

Return

the generated hashcode

Declaration

public static String generateHash(String tcString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from  w w  w. ja  v a2s .c  o  m
     * Generates a SHA-1 hashcode from the input
     * @param tcString the string to hash
     * @return the generated hashcode
     */
    public static String generateHash(String tcString) {
        try {
            MessageDigest loMD = MessageDigest.getInstance("SHA1");
            loMD.reset();
            byte[] laBuffer = tcString.getBytes();
            loMD.update(laBuffer);
            byte[] laDigest = loMD.digest();

            StringBuffer loReturn = new StringBuffer("");
            for (int i = 0, lnLength = laDigest.length; i < lnLength; i++) {
                loReturn.append(Integer.toString((laDigest[i] & 0xff) + 0x100, 16).substring(1));
            }
            return loReturn.toString();
        } catch (NoSuchAlgorithmException ex) {
            // SHA1 is built in so this will not happen
        }
        return "";
    }
}

Related

  1. generateHash(String item)
  2. generateHash(String plaintext)
  3. generateHash(String plainText, String hashType)
  4. generateHash(String serviceUri, String username, String password)
  5. generateHash(String target)
  6. generateHashSalt(int length)
  7. generateHashUUID(String digestData)
  8. hashCode(boolean b)
  9. hashCode(boolean b)