HowToDoInJavaBy Lokesh Gupta |
Special note about line endings: Mac/Unix and Windows use different codes to separate lines. The tool on this page normalizes all line endings to a Line Feed ( ).Other tools are available online if you need hashes specifically with Windows line endings (Carriage Return + Line Feed: r ). The Get-FileHash cmdlet computes the hash value for a file by using a specified hash algorithm. A hash value is a unique value that corresponds to the content of the file. Rather than identifying the contents of a file by its file name, extension, or other designation, a hash assigns a unique value to the contents of a file.
Filed Under: Java I/OA checksum hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user provided content. In this post, we will learn to generate the checksum hash for files.
1. Why we may want to generate checksum hash for a file?
Any serious file providers provide a mechanism to have a checksum on their downloadable files. A checksum is a form of mechanism to ensure that the file we downloaded is properly downloaded. Checksum acts like a proof of validity of a file so if a file gets corrupted this checksum will change and thus letting us know that this is not the same file or file has been corrupted between transfer for any reason.
You can also create checksum of file to detect any possible change in file by third party e.g. license files. You provide licenses to clients which they may upload to your server. You can cross verify the checksum of file to verify that license file has not been modified after creation.
Read More : MD5, SHA, PBKDF2, BCrypt examples
2. How to generate checksum hash for a file
To create checksum for a file, you will need to read the content of file byte by byte in chunks; and then generate hash for it using below manner.
Generate Hash From A Key Lyrics
This function takes two arguments:
- The message digest algorithm’s implementation
- A file for which checksum needs to be generated
You can use above function as below to generate MD5 file checksum :
To generate SHA file checksum, use the function as below:
Drop me a comment if something needs more explanation.
Happy Learning !!