C 语言实现简易区块链

Posted 广州吴彦祖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C 语言实现简易区块链相关的知识,希望对你有一定的参考价值。

C 语言实现简易区块链

总结:C 语言真不是我这种菜鸡所能驾驭的…

  • 无奈哈希函数太麻烦,就采用 base64 替代下哈希函数吧,其他符合区块链理论

    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <stdlib.h>
    
    // base64 编码
    static const char *ALPHA_BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    void Base64(const char *buf, const long size, char *base64Char) 
        int a = 0;
        int i = 0;
        while (i < size) 
            char b0 = buf[i++];
            char b1 = (i < size) ? buf[i++] : 0;
            char b2 = (i < size) ? buf[i++] : 0;
    
            int int63 = 0x3F;
            int int255 = 0xFF;
            base64Char[a++] = ALPHA_BASE[(b0 >> 2) & int63];
            base64Char[a++] = ALPHA_BASE[((b0 << 4) | ((b1 & int255) >> 4)) & int63];
            base64Char[a++] = ALPHA_BASE[((b1 << 2) | ((b2 & int255) >> 6)) & int63];
            base64Char[a++] = ALPHA_BASE[b2 & int63];
        
        switch (size % 3) 
            case 1:
                base64Char[--a] = '=';
            case 2:
                base64Char[--a] = '=';
        
    
    // 获取当前时间戳
    void timeStamp(char *timeStamp) 
        time_t t = time(NULL);
        int _timeStamp_ = time(&t);
        while (_timeStamp_ != 0) 
            timeStamp[strlen(timeStamp)] = (_timeStamp_ % 10) + '0';
            _timeStamp_ /= 10;
        
    
    
    typedef struct block 
        char prevHash[256];
        char hash[256];
        char timeStamp[20];
        char data[1024];
     Block;
    
    void setBlockHash(Block *block) 
        char str[2048];
        strcat(str, block->prevHash);
        strcat(str, block->hash);
        timeStamp(block->timeStamp);
        strcat(str, block->data);
        Base64(str, strlen(str), block->hash);
    
    
    char* getBlockHash(Block block) 
        char *res = block.hash;
        return res;
    
    
    // 创建区块
    Block createBlock(char prevHash[1024], char data[1024]) 
        Block block;
        strcpy(block.prevHash, prevHash);
        strcpy(block.data, data);
        timeStamp((&block)->timeStamp);
    
        char _hash_[2048];
        strcpy(_hash_, prevHash);
        strcat(_hash_, block.timeStamp);
        strcat(_hash_, data);
        Base64(_hash_, strlen(_hash_), block.hash);
        return block;
    
    
    typedef struct blockChain 
        Block block;
     BlockChain;
    BlockChain blockChain[1024];
    int blockChainPosition = 0;
    // 创世区块
    void Init_BlockChain() 
        char headHash[16];
        char headMsg[] = "Genesis Block";
        Base64(headMsg, strlen(headMsg), headHash);
        blockChain[blockChainPosition++].block = createBlock(headHash, "Genesis Block");
    
    
    void addBlock(char *data) 
        blockChain[blockChainPosition].block = createBlock(blockChain[blockChainPosition - 1].block.prevHash, data);
        char _block_[2048];
        strcat(_block_, blockChain[blockChainPosition].block.prevHash);
        strcat(_block_, blockChain[blockChainPosition].block.hash);
        strcat(_block_, blockChain[blockChainPosition].block.timeStamp);
        Base64(_block_, strlen(_block_), blockChain[blockChainPosition].block.prevHash);
        blockChainPosition++;
    
    
    int main() 
        Init_BlockChain();
    
        addBlock("this is block A");
        addBlock("this is block B");
    
        for (int i = 1; i < blockChainPosition; i++) 
            printf("PrevHash: %s\\n", blockChain[i].block.prevHash);
            printf("PrevHash: %s\\n", blockChain[i].block.hash);
            printf("PrevHash: %s\\n", blockChain[i].block.data);
            printf("\\n");
        
        return 0;
    
    
  • 运行结果

    PrevHash: UjJWdVpYTnBjeUJDYkc5amF3PT1VakpXZFZwWVRuQmplVUpEWWtjNWFtRjNQVDAyTWpNNU9URTRNell4ZEdocGN5QnBjeUJpYkc5amF5QkI2MjM5OTE4MzYx
    PrevHash: UjJWdVpYTnBjeUJDYkc5amF3PT02MjM5OTE4MzYxdGhpcyBpcyBibG9jayBB
    PrevHash: this is block A
    
    PrevHash: UjJWdVpYTnBjeUJDYkc5amF3PT1VakpXZFZwWVRuQmplVUpEWWtjNWFtRjNQVDAyTWpNNU9URTRNell4ZEdocGN5QnBjeUJpYkc5amF5QkI2MjM5OTE4MzYxVWpKV2RWcFlUbkJqZVVKRFlrYzVhbUYzUFQxVmFrcFhaRlp3V1ZSdVFtcGxWVXBFV1d0ak5XRnRSak5RVkRBeVRXcE5OVTlVUlRSTmVsbDRaRWRvY0dONVFuQmplVUpwWWtjNWFtRjVRa0kyTWpNNU9URTRNell4VldwS1YyUldjRmxVYmtKcVpWVktSRmxyWXpWaGJVWXpVRlF4Vm1GcmNGaGFSbHAzVjFaU2RWRnRjR3hXVlhCRlYxZDBhazVYUm5SU2FrNVJWa1JCZVZSWGNFNU9WVGxWVWxSU1RtVnNiRFJhUldSdlkwZE9OVkZ1UW1wbFZVcHdXV3RqTldGdFJqVlJhMGt5VFdwTk5VOVVSVFJOZWxsNE5qSXpPVGt4T0RNMk1UWXlNems1TVRnek5qRjBhR2x6SUdseklHSnNiMk5ySUVKMGFHbHpJR2x6SUdKc2IyTnJJRUk9NjIzOTkxODM2MTYyMzk5MTgzNjF0aGlzIGlzIGJsb2NrIEI=pH|
    
    PrevHash: RjVRa0kyTWpNNU9URTRNell4VldwS1YyUldjRmxVYmtKcVpWVktSRmxyWXpWaGJVWXpVRlF4Vm1GcmNGaGFSbHAzVjFaU2RWRnRjR3hXVlhCRlYxZDBhazVYUm5SU2FrNVJWa1JCZVZSWGNFNU9WVGxWVWxSU1RtVnNiRFJhUldSdlkwZE9OVkZ1UW1wbFZVcHdXV3RqTldGdFJqVlJhMGt5VFdwTk5VOVVSVFJOZWxsNE5qSXpPVGt4T0RNMk1UWXlNems1TVRnek5qRjBhR2x6SUdseklHSnNiMk5ySUVKMGFHbHpJR2x6SUdKc2IyTnJJRUk9NjIzOTkxODM2MTYyMzk5MTgzNjF0aGlzIGlzIGJsb2NrIEI=pH|
    
    PrevHash: R2x6SUdseklHSnNiMk5ySUVKMGFHbHpJR2x6SUdKc2IyTnJJRUk9NjIzOTkxODM2MTYyMzk5MTgzNjF0aGlzIGlzIGJsb2NrIEI=pH|
    

以上是关于C 语言实现简易区块链的主要内容,如果未能解决你的问题,请参考以下文章

用go语言撸一个简易版的区块链

用go语言撸一个简易版的区块链

从Go语言编码角度解释实现简易区块链

逻辑烧脑:python 区块链的简易实现

从0到1简易区块链开发手册V0.6-实现打印区块

从0到1简易区块链开发手册V0.5-实现余额查询