代码之家  ›  专栏  ›  技术社区  ›  stan

用OpenSSL和C++生成Sa2525

  •  57
  • stan  · 技术社区  · 15 年前

    我想用OpenSSL和C++创建一个用Sh256创建的散列。我知道在网上也有类似的帖子 Generate SHA hash in C++ using OpenSSL library ,但我希望专门创建sha256。

    更新:

    #include "openssl/sha.h"
    

    我在构建中包含了路径

    -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto 
    
    5 回复  |  直到 7 年前
        1
  •  72
  •   user2245192 Adam Lamers    6 年前

    我是这样做的:

    void sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65])
    {
        int i = 0;
    
        for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
        {
            sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
        }
    
        outputBuffer[64] = 0;
    }
    
    
    void sha256_string(char *string, char outputBuffer[65])
    {
        unsigned char hash[SHA256_DIGEST_LENGTH];
        SHA256_CTX sha256;
        SHA256_Init(&sha256);
        SHA256_Update(&sha256, string, strlen(string));
        SHA256_Final(hash, &sha256);
        int i = 0;
        for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
        {
            sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
        }
        outputBuffer[64] = 0;
    }
    
    int sha256_file(char *path, char outputBuffer[65])
    {
        FILE *file = fopen(path, "rb");
        if(!file) return -534;
    
        unsigned char hash[SHA256_DIGEST_LENGTH];
        SHA256_CTX sha256;
        SHA256_Init(&sha256);
        const int bufSize = 32768;
        unsigned char *buffer = malloc(bufSize);
        int bytesRead = 0;
        if(!buffer) return ENOMEM;
        while((bytesRead = fread(buffer, 1, bufSize, file)))
        {
            SHA256_Update(&sha256, buffer, bytesRead);
        }
        SHA256_Final(hash, &sha256);
    
        sha256_hash_string(hash, outputBuffer);
        fclose(file);
        free(buffer);
        return 0;
    }
    

    它的名字是这样的:

    static unsigned char buffer[65];
    sha256("string", buffer);
    printf("%s\n", buffer);
    
        2
  •  58
  •   Jonathan Drapeau    10 年前

    基于std的

    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    #include <openssl/sha.h>
    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();
    }
    
    int main() {
        cout << sha256("1234567890_1") << endl;
        cout << sha256("1234567890_2") << endl;
        cout << sha256("1234567890_3") << endl;
        cout << sha256("1234567890_4") << endl;
        return 0;
    }
    
        3
  •  25
  •   villapx    7 年前

    EVP interface (以下内容适用于OpenSSL 1.1):

    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <openssl/evp.h>
    
    bool computeHash(const std::string& unhashed, std::string& hashed)
    {
        bool success = false;
    
        EVP_MD_CTX* context = EVP_MD_CTX_new();
    
        if(context != NULL)
        {
            if(EVP_DigestInit_ex(context, EVP_sha256(), NULL))
            {
                if(EVP_DigestUpdate(context, unhashed.c_str(), unhashed.length()))
                {
                    unsigned char hash[EVP_MAX_MD_SIZE];
                    unsigned int lengthOfHash = 0;
    
                    if(EVP_DigestFinal_ex(context, hash, &lengthOfHash))
                    {
                        std::stringstream ss;
                        for(unsigned int i = 0; i < lengthOfHash; ++i)
                        {
                            ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
                        }
    
                        hashed = ss.str();
                        success = true;
                    }
                }
            }
    
            EVP_MD_CTX_free(context);
        }
    
        return success;
    }
    
    int main(int, char**)
    {
        std::string pw1 = "password1", pw1hashed;
        std::string pw2 = "password2", pw2hashed;
        std::string pw3 = "password3", pw3hashed;
        std::string pw4 = "password4", pw4hashed;
    
        hashPassword(pw1, pw1hashed);
        hashPassword(pw2, pw2hashed);
        hashPassword(pw3, pw3hashed);
        hashPassword(pw4, pw4hashed);
    
        std::cout << pw1hashed << std::endl;
        std::cout << pw2hashed << std::endl;
        std::cout << pw3hashed << std::endl;
        std::cout << pw4hashed << std::endl;
    
        return 0;
    }
    

    这个高级接口的优点是,您只需交换 EVP_sha256() 使用另一个摘要的函数调用,例如。 EVP_sha512() ,以使用不同的摘要。因此,它增加了一些灵活性。

        4
  •  1
  •   Max    13 年前

    #include <iostream>
    #include <sstream>
    
    #include "openssl/sha.h"
    
    using namespace std;
    
    string to_hex(unsigned char s) {
        stringstream ss;
        ss << hex << (int) s;
        return ss.str();
    }   
    
    string sha256(string line) {    
        unsigned char hash[SHA256_DIGEST_LENGTH];
        SHA256_CTX sha256;
        SHA256_Init(&sha256);
        SHA256_Update(&sha256, line.c_str(), line.length());
        SHA256_Final(hash, &sha256);
    
        string output = "";    
        for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
            output += to_hex(hash[i]);
        }
        return output;
    }
    
    int main() {
        cout << sha256("hello, world") << endl;
    
        return 0;
    }
    
        5
  •  0
  •   finnmglas    4 年前

    这是我个人使用的函数-我只是从我用于 sha-1 散列:

    char *str2sha256( const char *str, int length ) {
      int n;
      SHA256_CTX c;
      unsigned char digest[ SHA256_DIGEST_LENGTH ];
      char *out = (char*) malloc( 33 );
    
      SHA256_Init( &c );
    
      while ( length > 0 ) {
        if ( length > 512 ) SHA256_Update( &c, str, 512 );
        else SHA256_Update( &c, str, length );
    
        length -= 512;
        str += 512;
      }
    
      SHA256_Final ( digest, &c );
    
      for ( n = 0; n < SHA256_DIGEST_LENGTH; ++n )
        snprintf( &( out[ n*2 ] ), 16*2, "%02x", (unsigned int) digest[ n ] );
    
      return out;
    }
    
        6
  •  -1
  •   Peter    7 年前

    我认为你只需要用你文章中链接的tatk代码替换SHA1函数和SH256函数