You are developing a method to hash data for later verification by using the MD5 algorithm.
The data is passed to your method as a byte array named message.
You need to compute the hash of the incoming parameter by using MD5.
You also need to place the result into a byte array.
Which code segment should you use?
A.
HashAlgorithm algo = HashAlgorithm.Create(“MD5”);
byte[] hash = algo.ComputeHash(message);
B.
HashAlgorithm algo = HashAlgorithm.Create(“MD5”);
byte[] hash = BitConverter.GetBytes(algo.GetHashCode());
C.
HashAlgorithm algo;
algo = HashAlgorithm.Create(message.ToString());
byte[] hash = algo.Hash;
D.
HashAlgorithm algo = HashAlgorithm.Create(“MD5”);
byte[] hash = null;
algo.TransformBlock(message, 0, message.Lenth, hash, 0);
Explanation:
Create a HashAlgorithm object based on the MD5 algorithm and call the ComputerHash method that will return the hash as an array of bytes.
B GetHashCode() will call the method inherited from object, it will not hash the message.
C The parameter of the Create method should specify the type of hashing algorithm to use not the message to be hashed.
D TransferBlock is more appropriate for hashing part of a message. Also it should be called with TransferEndBlock.
I choose A