Cryptography in Java. Class MessageDigest
Hello, Habr! I present to you the translation of the third article "Java MessageDigest" by Jakob Jenkov from a series of articles for beginners who want to learn the basics of cryptography in Java.
Table of contents:
- Cryptography
- Cipher
- Messagedigest
- Mac
- Signature
- Keypair
- Keygenerator
- KeyPairGenerator
- Keystore
- Keytool
- Certificate
- CertificateFactory
- CertPath
Java MessageDigest
The Java class MessageDigest represents a cryptographic hash function that can compute a message digest from binary data. When you receive a set of encrypted data, you cannot be sure that it has not been changed during transport. A message digest helps solve this problem.
To determine if the encrypted data was modified during transport, the sender must calculate the message digest from the data and send it along with the data. The other side, receiving encrypted data and a message digest, can recalculate the message digest from the data and check whether the calculated message digest matches the message digest received with the data. If the two message digests match, there is a chance that the encrypted data was not changed during transport.
There are several conditions that must be met in order for a message digest to be useful as a change detection mechanism. However, the exact conditions are part of a cryptographic theory that is not discussed in this article, but only explains how to use Java to receive a message digest in the MessageDigest class.
Creating an instance of MessageDigest
To instantiate the MessageDigest class, the static getInstance () method of the MessageDigest class is called. Here is an example of creating an instance of MessageDigest:
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
The string parameter passed to the getInstance () method determines the digest algorithm used for a particular message.
Message Digest Algorithms
The Java Cryptography API supports the following message digest algorithms (external cryptography providers can support more):
- MD2
- MD5
- SHA-1
- SHA-256
- SHA-384
- SHA-512
Not all of these algorithms are equally safe. At the time of writing, it is recommended that you use SHA-256 or higher to get the highest possible level of security.
Message Digest Calculation
By creating an instance of MessageDigest, you can use it to calculate the message digest. If you have one data block for calculating the message digest, use the digest () method . Here's what a message digest calculation from a single data block looks like:
byte[] data1 = "0123456789".getBytes("UTF-8");
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] digest = messageDigest.digest(data1);
If there are several data blocks to be included in the same message digest, call the update () method and end with a call to digest () . Here's how a message digest calculation from multiple data blocks looks like:
byte[] data1 = "0123456789".getBytes("UTF-8");
byte[] data2 = "abcdefghijklmnopqrstuvxyz".getBytes("UTF-8");
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(data1);
messageDigest.update(data2);
byte[] digest = messageDigest.digest();