MATLAB: The encryption doesn’t work correctly? (SHA-256, HMAC-SHA-512)

apibase64decodebase64necodecrytographyhmac-sha-512MATLABsha-256

1) We have determined input:
challenge = 'c100b894-1729-464d-ace1-52dbce11db42';
apiSecret = '7zxMEF5p/Z8l2p2U7Ghv6x14Af+Fx+92tPgUdVQ748FOIrEoT9bgT+bTRfXc5pz8na+hL/QdrCVG7bh9KpT0eMTm';
2) We need to receive an expected output:
signed_output = '4JEpF3ix66GA2B+ooK128Ift4XQVtc137N9yeg4Kqsn9PI0Kpzbysl9M1IeCEdjg0zl00wkVqcsnG4bmnlMb3A==';
3) Here are steps of how we should encrypt it:
  1. Hash the challenge with the SHA-256 algorithm
  2. Base64-decode your api_secret
  3. Use the result of step 2 to hash the result of step 1 with the HMAC-SHA-512 algorithm
  4. Base64-encode the result of step 3
My code:
import matlab.net.*
% 1. Hash the challenge with the SHA-256 algorithm
hash_digest = DataHash(challenge, struct('Method','SHA-256'));
% 2. Base64-decode your api_secret
secret_decoded = base64decode(apiSecret);
% 3. Use the result of step 2 to hash the result of step 1 with the HMAC-SHA-512 algorithm
hmac_digest = HMAC(secret_decoded,hash_digest,'SHA-512');
% 4. Base64-encode the result of step 3
signed_output = base64encode(hmac_digest);
I am using these functions:
The result is wrong. Could you please help me to fix it?

Best Answer

Your call of DataHash creates the hash considering the class of the obejct. You want to get the hash for the values only.
challenge = 'c100b894-1729-464d-ace1-52dbce11db42';
Opt.Method = 'SHA-256';
Opt.Input = 'ascii'; % Use the contents only and only 8 bits
hash_digest = DataHash(challenge, Opt)
% Compare with:
DataHash(challenge, struct('Method','SHA-256'))
% Note that this considers the class and dimensions of the input also:
DataHash(challenge.', struct('Method','SHA-256'))
% ^^ other result for transposed input!!!
By the way, DataHash is an overkill here. Leaner:
Engine = java.security.MessageDigest.getInstance('SHA-256');
Engine.update(uint8(challenge(:)));
Hash = typecast(Engine.digest, 'uint8')
And for HMAC:
BlockSize = 128; % 64 for: MD5, SHA-1, SHA-256
KeySize = numel(Key);
Key = uint8(Key); % Hash it if it is longer than BlockSize
ipad(1:BlockSize) = uint8(54); % 0x36
ipad(1:KeySize) = bitxor(uint8(54), Key);
opad(1:BlockSize) = uint8(92); % 0x5c
opad(1:KeySize) = bitxor(uint8(92), Key);
Engine2 = java.security.MessageDigest.getInstance('SHA-512');
Engine2.update(ipad);
Engine2.update(uint8(Msg));
iHash = typecast(Engine2.digest, 'uint8');
Engine2.update(opad);
Engine2.update(iHash);
HMAC = typecast(Engine2.digest, 'uint8');