GMSSL is a popular library for china national shangmi(SM) algorithms, include sm2, sm3, sm4 and related. But do not have a good implements of Javascript language. also there was another robust library for Shangmi, but it was not used in finance yet. after few days working, today i gonna show you how to use EMScripten to compile it and use it in javascript within browser.
sudo apt install emscripten llvm clang
git clone https://github.com/guanzhi/GmSSL
cd GmSSL && mkdir build
cd build
emcmake cmake ..
emmake make -j8
when you finished the compile process. you will get a lot of wasm file and js file. but it can not work correctly yet.
and you have a static library was named libgmssl.a now.
write c code and compile to WASM with gmssl lib(libgmssl.a)
you should write your own code and compile it with libgmssl.a
those code was demonstrated how to keep your c code can be calling in the Javascript. Once you compiled those c code, you will get a wasm file and js file.
GMSSL is a popular library for china national
shangmi(SM)
algorithms, includesm2
,sm3
,sm4
and related. But do not have a good implements of Javascript language. also there was another robust library for Shangmi, but it was not used in finance yet. after few days working, today i gonna show you how to useEMScripten
to compile it and use it in javascript within browser.requirements
Install EMScripten and Compile GMSSL
when you finished the compile process. you will get a lot of wasm file and js file. but it can not work correctly yet. and you have a static library was named
libgmssl.a
now.write c code and compile to WASM with gmssl lib(libgmssl.a)
you should write your own code and compile it with
libgmssl.a
those code was demonstrated how to keep your c code can be calling in the Javascript. Once you compiled those c code, you will get a
wasm
file andjs
file.use below shell script to compile it
if you need to return value from c function, you must export runtime function : UTF8ToString
you will get three files :
call it from javascript
and run
python -m http.server
to host the html, then you will see:all exported function can be called by wasm with
Module.cwrap('run_test_sm2_keys', 'number', []);
andTroubleshooting
run_test_sm3
called before runtime initialization)xxx function was not exported change your function in the below function list
if you need return a value in c but can not accept it in js
just export runtime function:
UTF8ToString
if memroy error with
Module._xxx
Just replace it withModule.cwrap('run_test_sm2_keys', 'number', []);
compare two hash was equal in sign function and verify function gmssl was accept hex value by default.
Resources