Загрузка данных


#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <openssl/sha.h>

typedef struct {
    uint32_t index;
    char previousHash[256];
    uint32_t timestamp;
    char data[256];
    uint32_t difficulty;
    uint32_t nonce;
    char hash[256];
} TBlock;

void calculate_hash(TBlock *block, char *output) {
    char input[1024];
    sprintf(input, "%u%s%u%s%u%u", 
            block->index, 
            block->previousHash,
            block->timestamp,
            block->data,
            block->difficulty,
            block->nonce);
    
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256((unsigned char*)input, strlen(input), hash);
    
    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        sprintf(output + (i*2), "%02x", hash[i]);
    }
    output[64] = '\0';
}

int main() {
    TBlock block;
    block.index = 0;
    strcpy(block.previousHash, "00");
    block.timestamp = 1639469010;
    strcpy(block.data, "Hello, world!");
    block.difficulty = 20;
    block.nonce = 1225505;
    
    char hash[65];
    calculate_hash(&block, hash);
    
    printf("Хеш блока: %s\n", hash);
    
    // Проверка сложности (первые 20 бит = 5 hex символов = 20 бит)
    printf("Первые 20 бит (5 hex символов): %.5s\n", hash);
    
    return 0;
}