Download cryptography library and build for wasm

Download cryptography source code at GitHub, cryptopp.
Go to cryptopp folder which contains GNUmakefile.
Build it to generate C++ library and execute file.

make static dynamic cryptest.exe

Create a new folder build and move all generated files into it.

mv *.o *.so *.a *.exe libcryptopp.so.8* build

Then we will build wasm for web page. The process is similar to the above CPlusPlus’s one.

emmake make static dynamic cryptest.exe
mkdir build_wasm
mv *.o *.so *.wasm *.js *.a build_wasm

Build project based on cryptography for CPP program and wasm file.

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(EncrypteFile LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(Cryptopp_DIR "/home/stephen/Downloads/cryptopp")

include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${Cryptopp_DIR}
)

if(EMSCRIPTEN)
  set(emscripten_options)
  list(APPEND emscripten_options
    "--bind"
    "-O3"
    "-g"
    "-DLOG_ON"
    "SHELL:-s EXPORTED_RUNTIME_METHODS=['allocate,stringToUTF8,UTF8ToString,FS,intArrayFromString']"
    "SHELL:-s EXTRA_EXPORTED_RUNTIME_METHODS=['ALLOC_NORMAL']"
    "SHELL:-s EXPORT_NAME=encrypteFile"
    "SHELL:-s ALLOW_MEMORY_GROWTH=1"
    "SHELL:-s DEMANGLE_SUPPORT=1"
    "SHELL:-s EMULATE_FUNCTION_POINTER_CASTS=0"
    "SHELL:-s ERROR_ON_UNDEFINED_SYMBOLS=0"
    "SHELL:-s MODULARIZE=1"
    "SHELL:-s WASM=1"
    "SHELL:-s --no-heap-copy"
    "SHELL:-s INITIAL_MEMORY=200MB"
    "SHELL:-s MAXIMUM_MEMORY=512MB"
    "SHELL:-s ASSERTIONS=2"
    "SHELL:-s TOTAL_MEMORY=512MB"
    "SHELL:-s DISABLE_EXCEPTION_CATCHING=0"
  )
    link_directories( ${Cryptopp_DIR}/build_wasm )
else()
    link_directories( ${Cryptopp_DIR}/build )
endif()


add_executable(EncrypteFile main.cpp)

target_compile_options(${PROJECT_NAME}
  PUBLIC
    ${emscripten_options}
)

target_link_options(${PROJECT_NAME}
  PUBLIC
    ${emscripten_options}
)

target_link_libraries( ${PROJECT_NAME} cryptopp)

main.cpp

#include <iostream>
#include "integer.h"

using namespace std;
using CryptoPP::Integer;
int main()
{
    Integer i( 10 );
    cout << "Hello World! " << i << endl;
    return 0;
}

It’s easy to build c++ program. I will introduce how to build js and wasm file for web page.

mkdir build_wasm
emcmake cmake ../
emmake make

Then you can get these items.

➜  build_wasm ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  EncrypteFile.js  EncrypteFile.wasm  Makefile

Create index.html in build_wasm.

<html>
   <head>
      <!-- Load WebAssembly module -->
      <script type="text/javascript" src="EncrypteFile.js"></script>
   </head>
   <body>
      <div>
         Result 
         <span id="answer"/>
      </div>
      <script>
         // Wait for module to initialize,
         encrypteFile().then(({EncrypteFile}) => {
         });
      </script>
   </body>
</html>

Set up a simple http server by python in the folder build_wasm to see the web page.

python3 -m http.server 4000


0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Computer Number Terms
: You can use the tool to get different expressions of color, disk space and number.

X
A prohibited operation
0
Would love your thoughts, please comment.x
()
x