OpenSSL is a robust, commercial-grade, full-featured, and Open Source toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS) protocols as well as a full-strength general purpose cryptography library.
The versions of OpenSSL library at biicode are:
- OpenSSL 1.0.1 at lasote/openssl.
- OpenSSL 1.0.2 at lasote/openssl (v1.0.2).
Both versions are generated from this github repo.
This example shows how to use the cryptography feature of OpenSSL using a MD5 and SHA1 algorithm to encrypt a string.
Create a new simple layout project with the name of your block and copy the code below:
$ bii init mysslproject -L
$ cd mysslproject
$ # Copy both files inside
The MD5 example encrypts a string “happy”:
md5.cpp
#include "openssl/md5.h"
#include <stdio.h>
#include <string.h>
int main()
{
unsigned char digest[MD5_DIGEST_LENGTH];
char string[] = "happy";
MD5((unsigned char*)&string, strlen(string), (unsigned char*)&digest);
char mdString[33];
for(int i = 0; i < 16; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
printf("md5 digest: %s\n", mdString);
return 0;
}
Next, we’ll use a similar code to encrypt a “hello world!” string with SHA1:
sha1.cpp
#include "openssl/sha.h"
#include <string.h>
#include <stdio.h>
int main(){
// The data to be hashed
const unsigned char data[] = "Hello, world!";
size_t length = sizeof(data);
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(data, length, hash);
char mdString[SHA_DIGEST_LENGTH];
for(int i = 0; i < (SHA_DIGEST_LENGTH/2) - 1; i++)
sprintf(&mdString[i*2], "%02x", (unsigned int)hash[i]);
printf("sha1 digest: %s\n", mdString);
return 0;
}
Take a look at the #include
pointing to OpenSSL. Type bii deps to see unresolved dependencies:
$ bii deps
INFO: Processing changes...
your_user/mysslproject depends on:
system:
stdio.h
string.h
unresolved:
openssl/md5.h
openssl/sha.h
Let’s edit now the biicode.conf file generated in the project folder. Add your [requirements]
depending on the version you want and map your [includes]
:
[requirements]
lasote/openssl: 3
[includes]
openssl/*.h: lasote/openssl/include
Retrieve your unresolved dependencies:
$ bii find
INFO: Processing changes...
INFO: Downloading files from: lasote/openssl
INFO: Downloading files from: biicode/cmake
INFO: Saving files from: lasote/openssl
...
Now execute bii build.
$ bii build
Go to /bin directory and execute the binaries:
$ cd bin
$./examples_openssl_md5
md5 digest: 56ab24c15b72a457069c5ea42fcfc640
That output is the MD5 hash value of your encrypted string.
$./examples_openssl_sha1
sha1 digest: 2d5ec68b0d061c75db
And that is the SHA1 hash value for the “hello world!” string.
You can develop your own project with OpenSSL. Choose your version or switch between them using the block track feature.
Just modify the [requirements]
section in the biicode.conf file of your block’s folder:
To depend on OpenSSL 1.0.1:
[requirements] lasote/openssl: 2
To depend on OpenSSL 1.0.2:
[requirements] lasote/openssl(v1.0.2): 1
This example is already in biicode: examples/openssl.
Just open and build it to give it a quick try.
Create a new project and open the block:
$ bii init mysslproject
$ cd mysslproject
$ bii open examples/openssl
Now build and run the examples:
$ cd bin
$./examples_openssl_md5
md5 digest: 56ab24c15b72a457069c5ea42fcfc640
$./examples_openssl_sha1
sha1 digest: 2d5ec68b0d061c75db
Got any doubts? Do not hesitate to contact us, visit our forum and feel free to ask any questions.