SHA-1 hashing by openSSL in android studio - android

I am creating an encryption library in android studio by android NDK. I am using CMakeLists.txt script.
In the C++ file, I want to include #include <openssl/sha.h>.
How can I include OpenSSL in my c++ file?

What you need is to have in CMakeLists.txt a line like
INCLUDE_DIRECTORIES(SYSTEM "/path/to/openssl")
then in the C++ source file you simply
#include <sha.h> // or #include <openssl/sha.h>

Related

Compiling 1 library with 2 sub-libraries with Android Studio's CMake and NDK

Working on an Android NDK project. The project is using CMake to build the C++ libraries.
The library has the following structue -
Main Library
main.cpp
CMakeLists.txt
hello (sub library)/
CMakeLists.txt
include/
hello.h
src/
hello.c
world (sub library)/
CMakeLists.txt
include/
world.h
src/
world.c
The contents are the following:
main.cpp
#include <jni.h>
#include <string>
#include <hello.h>
#include <world.h>
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_test_myapplication_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string str = hello() + world();
return env->NewStringUTF(str.c_str());
}
CMakeLists.txt for main.cpp
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
add_subdirectory(src/main/cpp/hello)
include_directories(src/main/cpp/hello)
add_subdirectory(src/main/cpp/world)
include_directories(src/main/cpp/world)
target_link_libraries( # Specifies the target library.
native-lib
hello
world)
world.h
#include <string>
std::string world();
world.c
#include "world.h"
std::string world() {
return std::string("world");
}
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (world)
add_definitions(/DVERSION="0.4.0")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os")
include_directories(include/)
set(SOURCES
src/world.c
include/world.h
)
add_library(world STATIC IMPORTED
${SOURCES}
)
target_include_directories(world PUBLIC ./)
The content is the same for the "hello" library as the only difference is the output returned from hello's function.
Now, my experience with CMake is close to non-existent and I can't get this to work. I'm constantly getting errors upon building it. Latest error is the following -
CMake Error at src/main/cpp/hello/CMakeLists.txt:19
(target_include_directories): Cannot specify include directories for
imported target "hello". CMake Error at
src/main/cpp/world/CMakeLists.txt:19 (target_include_directories):
Cannot specify include directories for imported target "world".
I just can't find a way to compile all of it into a simple library that will be used in the app. Sometimes I get " not found", sometimes it can't find the header files and etc.
I'd appericiate if someone will be able to post a simple structure and CMake of the correct way of doing so as I'm lost.
In the actual project I try to use libmicrohttpd, but again, can't make it work. I've tried using CentOS's libraries but Android Studio seems to use the system's libraries which causes things to get messed up. This is the reason for which I download the library and try to compile it via Android Studio.
So, again, if anyone will be able to find a way to compile all of the 3 libraries together into a single library (main) which connected the other 2 (hello, world), I'd greatly appericiate it.
Much appericiated.

Compile OpenSSL for Android with Bazel

I'm building a native library for my Android application with bazel.
I'd like to use some OpenSSL functions on it like this:
#include <jni.h>
#include <openssl/aes.h>
...
AES_encrypt(in, out, key);
How to add the openssl library to bazel build ?
Subsidiary question: which archive I should use ?
openssl-1.1.0c.tar.gz
openssl-1.0.2j.tar.gz
openssl-1.0.1u.tar.gz
openssl-fips-2.0.13.tar.gz
openssl-fips-ecp-2.0.13.tar.gz
What I've tried
I've downloaded the openssl-1.0.2j archive. and added a cc_library entry to my BUILD file.
cc_library(
name = "openssl",
srcs = glob([
"openssl-1.0.2j/crypto/**/*.h",
"openssl-1.0.2j/crypto/**/*.c"
]),
includes = [
"openssl-1.0.2j",
"openssl-1.0.2j/crypto/",
],
visibility = ["//visibility:public"],
)
I've this error:
openssl-1.0.2j/crypto/dh/p512.c:60:24: fatal error: openssl/bn.h: No such file or directory
#include <openssl/bn.h>
But I don't understand why this code is trying to include a file from openssl while it's in openssl-1.0.2j/crypto/
With openssl-1.1.0c
openssl-1.1.0c/include/openssl/e_os2.h:13:34: fatal error: openssl/opensslconf.h: No such file or directory
# include <openssl/opensslconf.h>
Even if I run the Configure command, no opensslconf.h file is generated.
Based on #Ulf Adams' answer, I gave up trying to compile OpenSSL with bazel. I used BoringSSL instead.
BoringSSLis a fork of OpenSSL, and can easily be incorporated to a bazel project by doing:
git_repository(
name = "boringssl",
commit = "_some commit_",
remote = "https://boringssl.googlesource.com/boringssl",
)

"openssl/aes.h: No such file or directory" under Android

I'm trying to compile to android environment. And because of that I get the following error:
error: openssl/aes.h: No such file or directory
I find some solution in stack but, I don't get how to end the process to be able to compile.
I already compiled one version of openssl were should I add the libs? or how can I generate the *.a?
Do you know how can I add this library to the arm-linux-androideabi-g++ that I need to run to be able to pass this problem?
[1] Get openssl library which has aes.h file in its include folder.
[2] If you have compiled openssl library in your lib folder then add to -lssl or -lopenssl to your command line.
Here you can find openssl includes: openssl
Download this includes and put them in some folder in your project, i.e. project_dir/module_dir/jni/openssl-includes.
Then you need set a LOCAL_C_INCLUDES variable in your Android.mk (which also is in jni folder:
LOCAL_C_INCLUDES += ./openssl-includes
After that, you can include files in openssl-includes folder directly by name, i.e.:
#include <aes.h>
If you need an *.a file as output, you should include BUILD_STATIC_LIBRARY in Android.mk, if you need a *.so lib, include BUILD_SHARED_LIBRARY.

Using opencv in native code for Android app development

I have namespace error building with ndk-build for native code in my Android app. The error sample is
C:/adt-bundle-windows-x86/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/bits
/allocator.h:54:1: error: unknown type name 'namespace'
C:/adt-bundle-windows-x86/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/bits
/allocator.h:55:1: error: expected ',' or ';' before '{' token
For OpenCV settings, my Application.mk file is
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi
APP_PLATFORM := android-10
That means I am using gnu-libstdc++ in compiling the native code.
My jni.c has c extension. That is I receive from my third party and they prefer in .c extension as if they have to change to .cpp extension, they have to change a lot in their other libraries.
So far is OK, I did all project settings for OpenCV for native development and if I do ndk-build, I can make .so libraries.
The problem of those namespace error happened when I include OpenCV's header file #include <opencv2/contrib/detection_based_tracker.hpp> into jni.c and I got a lot of name space error. If I include that #include <opencv2/contrib/detection_based_tracker.hpp> into cpp file, no error.
My questions are
(1)Is the error is because of using .hpp file in .c file?
(2)If I have no choice and have to use that .c file, is there way around to remove that error?
Thanks
My assumption would be that the file is compiled as a "C" file instead of a "C++" file because of the extension ".c". That means you cannot use any "C++" Code in your jni.c, wike classes or namespaces. These are obviously used however in your file "detection_based_tracker.hpp" that you are using.
So the problem is not that you include a file named ".hpp", but that this file contains "C++" code wich the "C" compiler cannot handle.
One solution to this problem is to only use the "C" functions in opencv (for example "opencv2/imgproc/imgproc_c.h" instead of "opencv2/imgproc/imgproc.hpp"). However, your function "detection_based_tracker.hpp" might not have a "C" version, as far as I can see.
The other option is to add a second file "function.cpp" with the "C++" functions that use opencv. The functions from "function.cpp" can be declared in a file "functions.h" and included in your "jni.c", so you can still use opencv c++ functions. Be careful to only use C style functions then, though (no classes, namespaces, ...) in your "function.h" file, otherwise you will have the same problems as before.

Unable to build external OpenSSL library for Android NDK on Windows/Cygwin

I need to build the latest OpenSSL (1.0.0g) for an Android application. I am trying to follow the example given by https://github.com/fries/android-external-openssl, but I just can't get it built.
I am running Windows 7 Professional (64-bit) with a complete and recent Cygwin. I have installed the Android SDK and NDK, and I can successfully build and run the NDK's hello-jni sample application.
I created a new sample NDK app called hello-openssl. In its jni directory, I created an openssl directory. There, I unzipped https://github.com/fries/android-external-openssl/zipball/master, which gave me this tree structure under c:\android\android-ndk\samples\hello-openssl:
jni
+- openssl
+- apps
+- crypto
+- include
+- openssl
+- ssl
I then modified the Android.mk file in the jni directory in an attempt to include the OpenSSL files:
subdirs := $(addprefix $(LOCAL_PATH)/,$(addsuffix /Android.mk, \
openssl \
))
include $(subdirs)
Now when I execute ndk-build, it compiles several .c files, but then quickly fails:
Compile thumb : crypto <= cryptlib.c
In file included from jni/openssl/crypto/cryptlib.c:117:
jni/openssl/crypto/cryptlib.h:65:18: error: e_os.h: No such file or directory
jni/openssl/crypto/cryptlib.h:72:28: error: openssl/crypto.h: No such file or directory
I found http://osdir.com/ml/android-ndk/2010-07/msg00424.html, which tells me to "add
jni and jni/include to the above LOCAL_C_INCLUDES" in crypto/Android.mk, but I can't figure out the syntax I should use to achieve this.
I also can't figure out of I have the correct directory structure.
I sincerely appreciate any help that can be offered.
Thanks!
I solved this problem by abandoning https://github.com/fries/android-external-openssl and instead using https://github.com/guardianproject/openssl-android.
It is based on a more recent OpenSSL (1.0.0a), and it builds in the NDK without any modifications.
Note that in order to use these libraries in an Android app, you must rename them. If you simply include the resulting libssl.so and libcrypto.so in your app, then call System.LoadLibrary ("crypto") and System.LoadLibrary ("ssl"), you will get the OpenSSL libraries included in the Android system, not your custom libraries.
To do this, just do a full-word search and replace ("libssl" -> "libsslx", and "libcrypto" -> "libcryptox") in each Android.mk (i.e, in /crypto, /ssl, and /apps).
Then in your Android app, call SystemLoadLibrary ("cryptox") and System.LoadLibrary ("sslx")

Categories

Resources