SHA-1 Hash Code Generator

Share:

This hash code tool generates SHA-1 hash codes for the given string. Read further to get the source code for the SHA-1 hash code in C#.Net, Java and PHP.

*


About SHA-1 Hash Generator Tool

This tool generates SHA-1 hash codes from the entered text.

Steps for using this tool:

  1. Enter or paste your text in the first text box and click Generate SHA-1 Hash Code button.
  2. The SHA-1 hash code for the entered text appears in the second box.

SHA-1 Cryptographic Hash Algorithm

SHA-1 Hash Code Cryptography

SHA-1 Cryptography

SHA-1 is otherwise called as Secure Hash Algorithm 1. SHA-1 calculates hash codes for texts or file streams and generates a 160 bit hash code. This hash code will be in 40 digit hexadecimal number. The length of this hexadecimal hash code will be the same even if the input is 2 character string or 10,000 character text or 1GB file. For example, the SHA1 hash code for www.mytecbits.com is 74424e213bf77268c0abccb832762241f89f579d.

SHA-1 was found vulnerable to attacks. So, nowadays high security applications are using SHA-2 hashing algorithm. Still SHA-1 is the most commonly used encryption algorithm. SHA-1 hashing is used for cryptography and data integrity verifications like data validation, file validation, etc...

Uses of SHA-1 Hashing Algorithm

  1. The best example is to verify the file downloaded from internet. While downloading a file from internet there are possibility of the file corrupted due to internet disconnections ot the file got tampered with. To verify the authenticity of the downloaded file, the SHA-1 hash code of the downloaded file can be verified with the SHA-1 hash code generated from the source file. Thus any tampering or data loss can be identified.
  2. Generating SHA-1 hash code for strings and texts will be useful for password encryption, validating the authenticity of email content, etc..
  3. SHA-1 is used as cryptography algorithm for SSH, SSL,... protocols.

Programming SHA-1 Hash Generator

Major programming languages has inbuilt SHA-1 classes to generate and verify SHA-1 hash codes.

.Net

.Net has System.Security.Cryptography.SHA1 abstract class. This class has ComputeHash(Byte[]), ComputeHash(Stream) and ComputeHash(Byte[], Int32, Int32) methods which can be used to generate SHA-1 hash codes.

A simple .Net C# class method which takes a string as input and returns SHA-1 hash code.

// SHA-1 Hash Code Generator Method
public static string SHA1Generator(string strInput)
{
	SHA1 sha1 = new SHA1CryptoServiceProvider();

        //provide the string in byte format to the ComputeHash method. 
	//This method returns the SHA-1 hash code in byte array
	byte[] arrHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(strInput));

        // use a Stringbuilder to append the bytes from the array to create a SHA-1 hash code string.
	StringBuilder sbHash = new StringBuilder();

        // Loop through byte array of the hashed code and format each byte as a hexadecimal code.
	for (int i = 0; i < arrHash.Length; i++)
	{
		sbHash.Append(arrHash[i].ToString("x2"));
	}

        // Return the hexadecimal SHA-1 hash code string.
	return sbHash.ToString();
}

Java

In Java you can use the MessageDigest abstract class to generate the SHA-1 hash code for a string

A simple java class method which takes a string as input and returns SHA-1 hash code.

import java.math.BigInteger;
import java.security.MessageDigest;
 
public class SHA1 {
    public static String GenerateHash(String input) {
        MessageDigest objSHA = MessageDigest.getInstance("SHA-1");
        byte[] bytSHA = objSHA.digest(input.getBytes());
        BigInteger intNumber = new BigInteger(1, bytSHA);
        String strHashCode = intNumber.toString(16);
		
        // pad with 0 if the hexa digits are less then 40.
        while (strHashCode.length() < 40) {
            strHashCode = "0" + strHashCode;
        }
        return strHashCode;
    }
}

PHP

PHP has sha1() function to calculate MD5 hash code.

<?php
$str = "Hello";
echo sha1($str);
?>

Page Last Modified On: Jul 23, 2021


Disclaimer: We took every effort to provide higher level of accuracy in the calculators, converters and tools we have added to www.MyTecBits.com Tools section. But, we cannot give any guarantee or can be held responsible for any errors, defects, faults or mistakes in any of the calculators, converters or tools. Please see detailed terms of use and liability disclaimer in Terms of Use Page.

Follow Us