Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use OpenSSL's SHA256 functions
    text
    copied!<p>I'm writing a program to get myself acquainted with OpenSSL, libncurses, and UDP networking. I decided to work with OpenSSL's SHA256 to become familiar with industry encryption standards, but I'm having issues with getting it working. I've isolated the error to the linking of OpenSSL with the compiled program. I'm working on Ubuntu 12.10, 64 bit. I have the package libssl-dev installed.</p> <p>Take, for instance, the C++ main.cpp:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;iomanip&gt; using namespace std; #include &lt;openssl/sha.h&gt; string sha256(const string str) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&amp;sha256); SHA256_Update(&amp;sha256, str.c_str(), str.size()); SHA256_Final(hash, &amp;sha256); stringstream ss; for(int i = 0; i &lt; SHA256_DIGEST_LENGTH; i++) { ss &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; (int)hash[i]; } return ss.str(); } int main() { cout &lt;&lt; sha256("test") &lt;&lt; endl; cout &lt;&lt; sha256("test2") &lt;&lt; endl; return 0; } </code></pre> <p>I'm using the SHA256() function found <a href="https://stackoverflow.com/questions/2262386/generate-sha256-with-openssl-and-c">here</a> as a wrapper for OpenSSL's SHA256 functionality.</p> <p>When I attempt to compile with the following g++ arguments, I receive the following error:</p> <pre><code>millinon@myhost:~/Programming/sha256$ g++ -lssl -lcrypto -o main main.cpp /tmp/ccYqwPUC.o: In function `sha256(std::string)': main.cpp:(.text+0x38): undefined reference to `SHA256_Init' main.cpp:(.text+0x71): undefined reference to `SHA256_Update' main.cpp:(.text+0x87): undefined reference to `SHA256_Final' collect2: error: ld returned 1 exit status </code></pre> <p>So, GCC clearly recognizes OpenSSL's defined functions and types, but ld is failing to find the function symbols referred to in sha.h.</p> <p>Do I need to manually point to a specific shared object or directory?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload