C++调用openssl使用sha256,并取结果前64位作为uint64

参考:unix - Generate sha256 with OpenSSL and C++ - Stack Overflow

代码如下:

#include "openssl/sha.h"
#include <string>
#include <iostream>
using namespace std;
inline uint64_t 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);

    uint64_t result = 0;
    for (int i = 0; i != 8; ++i) {
        result |= hash[i] << (56 - i * 8);    // += could have also been used
    }
    return result;
}
int main(void) {
    cout << "start hash test" << endl;
    string test = "test";
    uint64_t result = sha256(test);
    cout << "hash result is:" << result << endl;
    return 0;
}

编译运行:

-> % g++ -o test test.cpp -I/opt/ssl/include/ -L/opt/ssl/lib/ -lcrypto 
-> % ./test
start hash test
hash result is:18446744072095727077
发布了543 篇原创文章 · 获赞 223 · 访问量 71万+
展开阅读全文

vc6 mfc工程添加#include <openssl/ssl.h> 就报语法错误,意外符号错误

01-20

win7-en-32 vc6 创建的mfc工程,openssl路径已经设置好,加入 ``` #include <openssl/ssl.h> #include <openssl/err.h> #include<ws2tcpip.h> #pragma comment(lib, "libeay32.lib") #pragma comment(lib, "ssleay32.lib") #pragma comment(lib, "Ws2_32.lib") ``` 报错如下 Compiling... Dialog.cpp c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ')' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ';' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : fatal error C1004: unexpected end of file found Generating Code... Compiling... IHope_Client.cpp c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ')' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ';' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : fatal error C1004: unexpected end of file found Generating Code... Compiling... IHope_ClientDlg.cpp c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ')' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ';' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : fatal error C1004: unexpected end of file found Generating Code... Compiling... RemoteDestTop.cpp c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(327) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\bn.h(341) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2059: syntax error : 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(859) : error C2238: unexpected token(s) preceding ';' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ')' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : error C2143: syntax error : missing ';' before 'constant' c:\openssl-1.0.2j\include\openssl\ssl.h(2318) : fatal error C1004: unexpected end of file found Generating Code... Error executing cl.exe. IHope_Client.exe - 36 error(s), 0 warning(s) ``` ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览