SHA-3 Hash implemented from standard (SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions, FIPS PUB 202, dated August 2015). Execute SHA3.m with inputs of message N (hex array), and d(224, 256, 384, or 512) which is the length of the binary output. Function provides hash output H (hex array). My function does not produce the expected hash outputs from known inputs. If anyone can point out my error in implementation that would be helpful.
David Hill (2019). SHA-3 HASH (https://www.mathworks.com/matlabcentral/fileexchange/71760-sha-3-hash), MATLAB Central File Exchange. Retrieved .
I still need some help figuring out where I am making a mistake in my code. For an empty input (''), I get: 'd672c0426ce6dbeddc76a8a2f2708dd52b9afe59d8e071fcda5ad6e3' which is not the correct answer. The correct answer is '6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7'. I am not sure how the message input should be converted to a binary vector. I am just doing a direct conversion to a uint8 Unicode number representation for each character and then converting to binary. That being said, the empty message input should be correct, so I must have another error in my coding.
Thanks for the comment. I am sorry I forgot to include the Rnd and SPONGE functions. I did not realize that the hex2poly function required the Communication Toolbox. I am reviewing the standard to try to understand the 'B34F9' but previously all I found was that [0 1] was the only thing added to the end of the binary string (once it is converted). Thanks for the comment on reshaping. I was trying to figure out how to do what I wanted but never could so I just pushed through it.
You need the Communication Toolbox to use hex2poly. I did not find out, what "Rnd" is inside KECCAK() and what "SPONGE" is inside SHA3(). The documentation says, that the first input of SHA3 is 'B34F9', but why? Does this input has a meaning also? Perhaps this is the message to be hashed? But why as hex string?
Some code should be vectorized, e.g.
is less efficient than:
Ap(1, 1, :) = A(1, 1, :);
case be replaced by this:
RC(1) = rc(7*ir)
RC(2) = rc(7*ir + 1);
can be simplified to:
Sp = reshape(permute(A, [3,1,2]), 1, );
Currently the code fails at least by the undefined functions. KECCAK is not called from anywhere. The inputs are not explained in the help section. So before the function is fixed to reply the expected hash values, it should be fixed until it runs and simplified.
Changed to convert using UTF-8 instead of Unicode. Simplified code in several areas.
Simplified the code and added many additional comments.
Changed input to SHA3 to be char array of any length. Output is still hex character array. This better matches the normal input stream for hashing. Eliminated use of hex2poly; therefore, no longer require Communications Toolbox.
All functions should be included now.
Included all functions
Did not include all functions previously
Updated some functions based on comments. Included two functions that I forgot to include previously that are essential for the execution.
Create scripts with code, output, and formatted text in a single executable document.