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.
This tool generates SHA-1 hash codes from the entered text.
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...
Major programming languages has inbuilt SHA-1 classes to generate and verify SHA-1 hash codes.
.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(); }
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 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.