'SHA256'에 해당되는 글 3건

  1. 2017.08.25 SHA256

SHA256

programming/C_C++ 2017. 8. 25. 17:00
반응형

 

 

http://www.askyb.com/cpp/openssl-sha256-hashing-example-in-cpp/

 

Example #1: sha256_sample1.cpp

 

#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
 
int main()
{
    unsigned char digest[SHA256_DIGEST_LENGTH];
    char string[] = "hello world";
    
    SHA256((unsigned char*)&string, strlen(string), (unsigned char*)&digest);    
 
    char mdString[SHA256_DIGEST_LENGTH*2+1];
 
    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
 
    printf("SHA256 digest: %s\n", mdString);
 
    return 0;
}

 

Example #2: sha256_sample2.cpp

 

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

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

    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, string, strlen(string));
    SHA256_Final(digest, &ctx);

    char mdString[SHA256_DIGEST_LENGTH*2+1];
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
        sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("SHA256 digest: %s\n", mdString);


    return 0;
}

 

 

~$ gcc sha256_sample1.cpp -o sample1 -lcrypto
~$ ./sample1
SHA256 digest: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

 


~$ gcc sha256_sample2.cpp -o sample2 -lcrypto
~$ ./sample2
SHA256 digest: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

 

반응형
Posted by 공간사랑
,