Libraries: SHA256, SHA384 and SHA512 + HMAC

 

SHA256, SHA384 and SHA512 + HMAC:

Many developers need these functions, while working with different kinds of external APIs, like Bitcoin and altcoin exchanges where it is often necessary to send data with the confirmation of parameters validity through HMAC-SHA512, HMAC-SHA384 and HMAC-SHA256.

The MQL5 version of SHA512 library can be found here: https://www.mql5.com/en/code/18158. Besides SHA512, added SHA256 and SHA384 support in form of separate libraries.

HMAC functions were ported from https://en.wikipedia.org/wiki/Hash-based_message_authentication_code

Use example (tested on http://www.freeformatter.com/hmac-generator.html)

#include <SHA256.mqh>
#include <SHA384.mqh>
#include <SHA512.mqh>

input string phrase = "The quick brown fox jumps over the lazy dog";
input string phrase_key = "ABCDEFG";

void Start()
  {
   SHA256 hash256;
   Print("SHA256:",hash256.hmac(phrase,phrase_key));

   SHA384 hash384;
   Print("SHA384:",hash384.hmac(phrase,phrase_key));

   SHA512 hash512;
   Print("SHA512:",hash512.hmac(phrase,phrase_key));
  }

Author: Grzegorz Korycki

 
Amazing job! Thanks!
 

Thanks i hope there's RIPEMD160 also.

For crypto there is need to use immutable bytestring.

 
Your the library really helped! - Thank!
 

SHA256, SHA384 work in MQL5. great.

at first, I thought they only work in MQL4.
 
HMAC-SHA256 does not work if the key length is greater than 64.
 

Keccak256 is up next. First off, has anyone successfully made a mq5 version? There's no mention of keccak within search results as of today. 


If not, freelance is next. 


/* 
 * Bitcoin cryptography library
 * Copyright (c) Project Nayuki
 * 
 * https://www.nayuki.io/page/bitcoin-cryptography-library
 * https://github.com/nayuki/Bitcoin-Cryptography-Library
 */

#include <cassert>
#include "Keccak256.hpp"

using std::uint8_t;
using std::uint64_t;
using std::size_t;


void Keccak256::getHash(const uint8_t msg[], size_t len, uint8_t hashResult[HASH_LEN]) {
        assert((msg != nullptr || len == 0) && hashResult != nullptr);
        uint64_t state[5][5] = {};
        
        // XOR each message byte into the state, and absorb full blocks
        int blockOff = 0;
        for (size_t i = 0; i < len; i++) {
                int j = blockOff >> 3;
                state[j % 5][j / 5] ^= static_cast<uint64_t>(msg[i]) << ((blockOff & 7) << 3);
                blockOff++;
                if (blockOff == BLOCK_SIZE) {
                        absorb(state);
                        blockOff = 0;
                }
        }
        
        // Final block and padding
        {
                int i = blockOff >> 3;
                state[i % 5][i / 5] ^= UINT64_C(0x01) << ((blockOff & 7) << 3);
                blockOff = BLOCK_SIZE - 1;
                int j = blockOff >> 3;
                state[j % 5][j / 5] ^= UINT64_C(0x80) << ((blockOff & 7) << 3);
                absorb(state);
        }
        
        // Uint64 array to bytes in little endian
        for (int i = 0; i < HASH_LEN; i++) {
                int j = i >> 3;
                hashResult[i] = static_cast<uint8_t>(state[j % 5][j / 5] >> ((i & 7) << 3));
        }
}


void Keccak256::absorb(uint64_t state[5][5]) {
        uint64_t (*a)[5] = state;
        uint8_t r = 1;  // LFSR
        for (int i = 0; i < NUM_ROUNDS; i++) {
                // Theta step
                uint64_t c[5] = {};
                for (int x = 0; x < 5; x++) {
                        for (int y = 0; y < 5; y++)
                                c[x] ^= a[x][y];
                }
                for (int x = 0; x < 5; x++) {
                        uint64_t d = c[(x + 4) % 5] ^ rotl64(c[(x + 1) % 5], 1);
                        for (int y = 0; y < 5; y++)
                                a[x][y] ^= d;
                }
                
                // Rho and pi steps
                uint64_t b[5][5];
                for (int x = 0; x < 5; x++) {
                        for (int y = 0; y < 5; y++)
                                b[y][(x * 2 + y * 3) % 5] = rotl64(a[x][y], ROTATION[x][y]);
                }
                
                // Chi step
                for (int x = 0; x < 5; x++) {
                        for (int y = 0; y < 5; y++)
                                a[x][y] = b[x][y] ^ (~b[(x + 1) % 5][y] & b[(x + 2) % 5][y]);
                }
                
                // Iota step
                for (int j = 0; j < 7; j++) {
                        a[0][0] ^= static_cast<uint64_t>(r & 1) << ((1 << j) - 1);
                        r = static_cast<uint8_t>((r << 1) ^ ((r >> 7) * 0x171));
                }
        }
}


uint64_t Keccak256::rotl64(uint64_t x, int i) {
        return ((0U + x) << i) | (x >> ((64 - i) & 63));
}


// Static initializers
const unsigned char Keccak256::ROTATION[5][5] = {
        { 0, 36,  3, 41, 18},
        { 1, 44, 10, 45,  2},
        {62,  6, 43, 15, 61},
        {28, 55, 25, 21, 56},
        {27, 20, 39,  8, 14},
};