File Exchange

image thumbnail

SHA-3 HASH

version 1.0.8 (4.38 KB) by David Hill
SHA-3 Hash implemented from standard

5 Downloads

Updated 18 Jun 2019

View License

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.

Cite As

David Hill (2019). SHA-3 HASH (https://www.mathworks.com/matlabcentral/fileexchange/71760-sha-3-hash), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

David Hill

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.

David Hill

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.

Jan

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.
for z=1:w
Ap(1,1,z)=A(1,1,z);
end
is less efficient than:
Ap(1, 1, :) = A(1, 1, :);
The loop:
RC=zeros(1,w);
for j=0:l
RC(2^j)=rc(j+7*ir);
end
case be replaced by this:
RC(1) = rc(7*ir)
RC(2) = rc(7*ir + 1);
The code
B=A(:)';
Sp=[B(1:25:end),B(2:25:end),B(3:25:end),B(4:25:end),B(5:25:end),B(6:25:end),B(7:25:end)...
,B(8:25:end),B(9:25:end),B(10:25:end),B(11:25:end),B(12:25:end),B(13:25:end),B(14:25:end)...
,B(15:25:end),B(16:25:end),B(17:25:end),B(18:25:end),B(19:25:end),B(20:25:end),...
B(21:25:end),B(22:25:end),B(23:25:end),B(24:25:end),B(25:25:end)];
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.

Updates

1.0.8

Changed to convert using UTF-8 instead of Unicode. Simplified code in several areas.

1.0.7

Simplified the code and added many additional comments.

1.0.6

Changed image

1.0.5

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.

1.0.4

All functions should be included now.

1.0.3

Included all functions

1.0.2

Did not include all functions previously

1.0.1

Updated some functions based on comments. Included two functions that I forgot to include previously that are essential for the execution.

MATLAB Release Compatibility
Created with R2019a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor