message digest takes an arbitrary amount of input data creates a short, messageversion of the data, sometimes called a digital fingerprint, secure hash, or cryptographichash Write a simple JAVA program that creates message digest from a file.describe its methods under java. Security.Key
Question
message digest takes an arbitrary amount of input data creates a short, messageversion of the data, sometimes called a digital fingerprint, secure hash, or cryptographichash Write a simple JAVA program that creates message digest from a file.describe its methods under java. Security.Key
Solution
Sure, here is a simple Java program that creates a message digest from a file using the java.security.MessageDigest class.
import java.io.*;
import java.security.*;
public class FileDigest {
public static void main(String[] args) throws Exception {
// Check if file name is provided
if (args.length != 1) {
System.err.println("Usage: java FileDigest <file name>");
return;
}
// Get file input stream for reading the file content
FileInputStream fis = new FileInputStream(args[0]);
// Create MessageDigest instance for MD5
MessageDigest md = MessageDigest.getInstance("MD5");
// Read file content in bytes
byte[] byteArray = new byte[1024];
int bytesCount = 0;
while ((bytesCount = fis.read(byteArray)) != -1) {
md.update(byteArray, 0, bytesCount);
};
// Close the input stream
fis.close();
// Get the hash's bytes
byte[] bytes = md.digest();
// Convert bytes to Hexadecimal
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
// Get complete hashed password in hex format
String fileDigest = sb.toString();
// Print the digest
System.out.println("Digest(in hex format):: " + fileDigest);
}
}
This program uses the MessageDigest class from the java.security package. The MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
Here are the methods used in the program:
-
MessageDigest.getInstance(String algorithm): This method returns a MessageDigest object that implements the specified digest algorithm. -
update(byte[] input): This method updates the digest using the specified array of bytes. -
digest(): This method completes the hash computation by performing final operations such as padding. It returns the array of bytes for the resulting hash value. -
FileInputStream.read(byte[] b): This method reads up tob.lengthbytes of data from this input stream into an array of bytes. It returns the total number of bytes read into the buffer, or-1if there is no more data because the end of the file has been reached. -
FileInputStream.close(): This method closes this file input stream and releases any system resources associated with the stream.
Similar Questions
t the correct answerWhich stream does Java application uses to read data from a source, it may be a file, an array, peripheral device or socket?OptionsInputStreamInput/OutputStreamOutputStreamNone of these
What is the purpose of the System.out.println() method in Java?Points:1I don't knowReads input from the consoleNone of the abovePrints a message to a filePrints a message to the console
Which stream does Java application uses to read data from a source, it may be a file, an array, peripheral device or socket?OptionsOutputStreamInputStreamInput/OutputStreamNone of these
In Java, which class is used for reading and writing binary data in a random-access file?Question 2Answera. RandomAccessFileb. FileReaderc. DataOutputStreamd. BufferedRandomAccessFile
What is the term for the process of converting data into a fixed-size string of characters, typically for hash code generation?Group of answer choicesSortingHashingEncryptionCompression
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.