1

I'm trying to use the Sha512 function in openSSL but can't seem to get it to work as I get compiler errors just starting into the code. I include #include <openssl/sha.h> at the top of the .cpp file, then in the action of a button event I put just the following code below.

SHA512_CTX ctx;
SHA512_Init(&ctx);

//Will uncomment below in later if I get SHA512_Init to work

//SHA512_Update(&ctx, string, strlen(string));
//SHA512_Final(digest, &ctx);

I get a linker error telling my undefined symbols for architecture x86_64, implying the function does not exist?

I'm aware QT 5 has a hash function, but I'm limited to QT 4.8 so I can not use the cryptographic sha512 hash function available in the QT 5+ framework.

Any help is appreciated!

Used macports to install openssl

I'm using Mac OS 10.9.2

MAKE FILE

#-------------------------------------------------
#
# Project created by QtCreator 2014-06-11T20:27:49
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ExchangeTab
TEMPLATE = app

LIBS += -L/usr/include/openssl -openssl
INCLUDEPATH += /usr/include/openssl

SOURCES += main.cpp\
        mainwindow.cpp \
    httpsocket.cpp \
    cloaksend.cpp \
    exchange.cpp

HEADERS  += mainwindow.h \
    httpsocket.h \
    cloaksend.h \
    exchange.h

FORMS    += mainwindow.ui

RESOURCES += \
    AppResources.qrc

Looking for cross platform solution please.

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154

3 Answers3

2

You need to link to the right library (openSSL)

Have a look here: How to Include OpenSSL in a Qt project

Specifically, add this to your .pro file.:

LIBS += -L/opt/local/lib/ -lcrypto

For including .h files add this line to your .pro file :

INCLUDEPATH += /opt/local/include

[1] says that the default include path will be /opt/local/include/.

Community
  • 1
  • 1
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
  • I actually was just looking there, but I'm not sure what the include path is for mac, any ideas? I edited my makefile to show what I think the path is, but still doesn't work, same linker error. You are definitely on the right track with this. – Joseph Astrahan Jun 17 '14 at 12:40
  • It should be near `/opt/local/include`. And the library path will be `/usr/local/lib` It should say that when installing using macports. – Unapiedra Jun 17 '14 at 12:47
  • still not working, here is what I tried LIBS += -L/opt/local/include/ -openssl INCLUDEPATH += /opt/local/include/ I was reading somewhere else that to make this cross platform friendly I might have to use something called OPENSSL_LIB_PATH, know what that is? I also tried on the libs part having it go to the /usr/local/lib, even navigated in terminal to see if it the files actually existed, openssl does indeed exist there oddly without a .h (well just realized it was a directory of further .h files). – Joseph Astrahan Jun 17 '14 at 12:57
1
LIBS += -L/usr/include/openssl -openssl
INCLUDEPATH += /usr/include/openssl

This looks incorrect. The OpenSSL libraries are libcrypto (-lcrypto) and libssl (-lssl). There is nolibopenssl(-lopenssl). Try:

LIBS += -L/usr/lib -lcrypto
INCLUDEPATH += /usr/include/openssl

But the libraries are version 0.9.8. You might consider upgrading to 1.0.1h.

$ ls /usr/lib | grep crypto
libcrypto.0.9.7.dylib
libcrypto.0.9.8.dylib
libcrypto.dylib
libk5crypto.dylib

And

$ /usr/bin/openssl version
OpenSSL 0.9.8y 5 Feb 2013

If you choose to upgrade, OpenSSL will install into /usr/local/ssl. Avoid mixing/matching version of OpenSSL with the following.

INCLUDEPATH += /usr/local/ssl/include/openssl
LIBS += /usr/local/ssl/lib/libcrypto.a

Its OK to specify objects and archives in LIBS. See How to add object files to a project in Qt.

jww
  • 97,681
  • 90
  • 411
  • 885
  • Thanks. It looks like the -lcrypto in your post seemed to be key to the solution I found. I'm curious if you could explain why my solution worked? – Joseph Astrahan Jun 17 '14 at 13:13
0

Okay so I may have answered my own question but I will need some help understanding why it worked.

My make file was indeed the problem. I added the following 3 lines.

INCLUDEPATH += $$OPENSSL_INCLUDE_PATH
LIBS += $$join(OPENSSL_LIB_PATH,,-L,)
LIBS += -lcrypto

Then it magically compiled just fine. I found these in another project made with QT that compiled for OpenSSL.

Interestingly enough. I removed the top two lines so only the following remained. Then ran the clean on my project to be sure the code was being recompiled.

LIBS += -lcrypto

This also just 'worked' without linker errors. It looks like that is the only command I need. Question is... will that be cross platform friendly if I take this code and compile on linux or windows? That I am not sure, but this worked.

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
  • 1
    Yes, it should be cross platform friendly. However, you need a way to find the lib-path automatically. – Unapiedra Jun 17 '14 at 14:34
  • I'm not sure what to select as the correct answer in this case, but I did have another question you might be able to help with as well. It's located at(http://stackoverflow.com/questions/24266505/hmac-sha512-in-qt-c-using-openssl-library-and-qt-4-8-framework). I got the SHA working, just trying to get it working with HMAC now. I think I almost got it figured out, maybe you can help me with what I did wrong? Much appreciated for all the advice so far! It has really helped! – Joseph Astrahan Jun 18 '14 at 03:32