Deribit FIX API Logon

Following code doesn’t seem to work to Logon using FIX API. Getting "invalid credentials" from the exchange though same username and access key seem to work with REST API over websockets.
Seems like issue with the definition of nonce. Here I am trying a basic example to try to Logon.

        string user = settings->get().getString("Username");
        message.setField(Username(user));
        string pass = settings->get().getString("Password");
        milliseconds ms = duration_cast< milliseconds >(
                system_clock::now().time_since_epoch()
        );
        long long millis = ms.count();
        string nonce = "abcdefghijkabcdefghijkabcdefghijkabcdefghijk";
        nonce = base64_encode(nonce);
        string raw = to_string(millis) + "." + nonce;

        message.setField(RawData(raw));
        string password = base64_encode(sha256(raw+pass));

        message.setField(Password(password));

the functions used for base64 and sha256 encoding are:

string base64_encode(const std::string &in) {

    std::string out;

    int val = 0, valb = -6;
    for (unsigned char c : in) {
        val = (val << 8) + c;
        valb += 8;
        while (valb >= 0) {
            out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(val>>valb)&0x3F]);
            valb -= 6;
        }
    }
    if (valb>-6) out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((val<<8)>>(valb+8))&0x3F]);
    while (out.size()%4) out.push_back('=');
    return out;
}

string sha256(const string str)
{
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, str.c_str(), str.size());
    SHA256_Final(hash, &sha256);
    stringstream ss;
    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        ss << hex << setw(2) << setfill('0') << (int)hash[i];
    }
    return ss.str();
}

I am following this documentation from Deribit https://docs.deribit.com/?shell#logon-a and quickfix as the fix engine.

enter image description here

Code follows from this description mentioned in the docs.

Source: Windows Questions C++

LEAVE A COMMENT