You are developing a method named GetHash that will return a hash value for a file. The
method includes the following code. (Line numbers are included for reference only.)
You need to return the cryptographic hash of the bytes contained in the fileBytes variable.
Which code segment should you insert at line 05?
A.
Option A
B.
Option B
C.
Option C
D.
Option D
Correct answer is D.
http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.hash(v=vs.110).aspx
D
D
Comparing with 110 answer should be D.
D
I feel this question is wrong.
A:This is wrong as fro transformblock the input and output buffer should be same. The syntax does not make sense as we do not use the outputbuffer in the transformfinalblock.
ReferenceLink is:https://blogs.msdn.microsoft.com/shawnfa/2004/02/20/using-the-hashing-transforms-or-how-do-i-compute-a-hash-block-by-block/
B: Gets the code not the hash. Return type is int.
C: It is incomplete without transformblockfinal
D:Gets the value of the computed hash code and not the hash.
B: actually returns the hashcode of the object, so in that case it would return “hasher” hashcode, and not the hashcode computed by “hash”.
However the closest correct answer is A
Azmat Parveen, I disagree, answer should be D.
Try it in your Main:
byte[] GetHashAnswerD(string fileName, string algorithmType)
{
var hasher = HashAlgorithm.Create(algorithmType);
var fileBytes = File.ReadAllBytes(fileName);
// Compute the hash value for the specified byte array
hasher.ComputeHash(fileBytes);
// Gets the value of the computed hash code
return hasher.Hash;
}
var hashedFileAnswerD = GetHashAnswerD(@”D:\temp\test.test”, “SHA256”);
var AnswerDHashAsString = BitConverter.ToString(hashedFileAnswerD);
-> The hash is correct (you can check the hash of a file there: http://onlinemd5.com/).
I’m not pretty sure how to use the TransformFinalBlock, but I suppose it must be used when you iterate over all bytes on the file and after the last iteration only, which is not the case in answer A.
See https://stackoverflow.com/a/20048684
The link you provided is also useful, and what I understand is the same as what I stated before. I quote: “For all but the last block of your data, you call TransformBlock(). The last block gets passed to TransformFinalBlock()”