在Windows和Linux上略有不同的SHA256

时间:2015-02-10 15:28:44

标签: c++ linux windows base64 sha256

下面的代码在Windows上生成SHA256哈希。正如你所看到的那样,它会从文本产生哈希值" doublecheck" (5 / NK + 1ZAwTjzTY1PjZm0xcPRDf6KMQhmE4SVQnPOQ3M =) 我已经在linux中创建了应该生成相同哈希的代码,但它是不同的。 (5 / NK + 1ZAwTjzTY1PjZm0xcPRDf6KMQhmE4SVQnPOQ3O / enx3tzCun78sgwQIOK6fv1T6eLc =) 任何人都可以帮我解决任何这些代码以获得相同的哈希值吗? Windows代码:

    #include "stdafx.h"
    #include "Hash2.h"
    #include <Wincrypt.h>
    #pragma comment(lib, "Crypt32.lib")

    DWORD BufSize;
    #define BUF_SIZE 256
    TCHAR Buf[BUF_SIZE];

    CStringA BinaryToBase64(__in const byte * pbBinary, __in DWORD cbBinary)
    {
        ATLASSERT(pbBinary != NULL);
        if (pbBinary == NULL)
            AtlThrow(E_POINTER);
        ATLASSERT(cbBinary != 0);
        if (cbBinary == 0)
            AtlThrow(E_INVALIDARG);
        DWORD cchBase64;
        if (!CryptBinaryToStringA(pbBinary, cbBinary, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &cchBase64))
        {
            AtlThrowLastWin32();
        }
        CStringA strBase64;
        LPSTR pszBase64 = strBase64.GetBuffer(cchBase64);
        ATLASSERT(pszBase64 != NULL);
        if (!CryptBinaryToStringA(pbBinary, cbBinary, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, pszBase64, &cchBase64))
            AtlThrowLastWin32();
        strBase64.ReleaseBuffer();
        return strBase64;
    }

    // creates sha256 hash from string
    LPCSTR CreateHash(LPCSTR tohash)
    {
        HCRYPTPROV hProv;
        HCRYPTHASH hash;
        if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) 
        {
            if (CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hash))
            {
                int sz = strlen(tohash);
                if (CryptHashData(hash, (BYTE *)tohash, sz, 0))
                {
                    ZeroMemory(&Buf, sizeof(Buf));
                    BufSize = sizeof(Buf);
                    if (!CryptGetHashParam(hash, HP_HASHVAL, (BYTE *)&Buf, &BufSize, 0))
                        AtlThrowLastWin32();
                }
                else
                    AtlThrowLastWin32();
                if (!CryptDestroyHash(hash))
                    AtlThrowLastWin32();
            }
            else
                AtlThrowLastWin32();
            if (!CryptReleaseContext(hProv, 0))
                AtlThrowLastWin32();
        }
        else
            AtlThrowLastWin32();
        CStringA stemp = BinaryToBase64(reinterpret_cast<BYTE *>(Buf), BufSize).Trim();
        int sizeOfString = (stemp.GetLength() + 1);
        LPSTR retVal = new char[sizeOfString];
        strcpy_s(retVal, sizeOfString, stemp);
        return retVal;
    }

    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        LPCSTR text = "doublecheck";
        LPCSTR hash = CreateHash(text);
        printf("\"%s\" hashed = %s", text, hash);
//output: "doublecheck" hashed = 5/NK+1ZAwTjzTY1PjZm0xcPRDf6KMQhmE4SVQnPOQ3M=
        getchar();
    }

Linux代码:

#include <openssl/bio.h>
#include <openssl/evp.h>
#include <math.h> 
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/crypto.h> 

int Base64Encode(const char* message, char** buffer) { //Encodes a string to base64
BIO *bio, *b64;
FILE* stream;
int encodedSize = 4*ceil((double)strlen(message)/3);
*buffer = (char *)malloc(encodedSize+1);

stream = fmemopen(*buffer, encodedSize+1, "w");
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_fp(stream, BIO_NOCLOSE);
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line
// edit: bad code BIO_write(bio, message, strlen(message));
BIO_write(bio, message, SHA256_DIGEST_LENGTH); // edit: correction
BIO_flush(bio);
BIO_free_all(bio);
fclose(stream);
return (0); //success
} 

int main() {
    unsigned char digest[SHA256_DIGEST_LENGTH];
    const char* string = "doublecheck";

    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, string, strlen(string));
    SHA256_Final(digest, &ctx);
    printf("SHA256 digest: %s\n", digest);

    char* base64EncodeOutput;
    Base64Encode((char*)digest, &base64EncodeOutput);
    printf("Output (base64): %s\n", base64EncodeOutput); 
// now the output here is: 5/NK+1ZAwTjzTY1PjZm0xcPRDf6KMQhmE4SVQnPOQ3M=  
    return 0;
}

0 个答案:

没有答案