You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
if you need to return value from c function, you must export runtime function : UTF8ToString
you will get three files :
call it from javascript
<html><head><scriptsrc="sm.js"></script><script>
function stringToHex(str) {lethex='';for(leti=0;i<str.length;i++){hex+=str.charCodeAt(i).toString(16).padStart(2,'0');}
return hex;
}functionhexToString(hex){// Ensure the hex string length is even if(hex.length%2!==0){thrownewError("Invalid hex string");}
let str = '';
for (let i = 0; i <hex.length;i+=2){// Parse each pair of hex digits as an integer, then convert to a character constbyte=parseInt(hex.substr(i,2),16);str+=String.fromCharCode(byte);}returnstr;}setTimeout(()=>{construn_test_sm3=Module.cwrap('run_test_sm3','string',['string']);// Call the wrapped function // const hexString = '74657374'; // 'test' in hex constresult=run_test_sm3(stringToHex('test'));// Output the result console.log("SM3 Hash:",result);construn_generate_sm2_keys=Module.cwrap('run_test_sm2_keys','number',[]);constkeysPtr=run_generate_sm2_keys();constprivateKeyPtr=Module.getValue(keysPtr,'i32');constpublicKeyPtr=Module.getValue(keysPtr+4,'i32');constprivateKey=Module.UTF8ToString(privateKeyPtr);constpublicKey=Module.UTF8ToString(publicKeyPtr);console.log("Private Key:",privateKey);console.log("Public Key:",publicKey);construn_sm2_encrypt=Module.cwrap('run_test_sm2_encrypt','string',['string','string']);construn_sm2_decrypt=Module.cwrap('run_test_sm2_decrypt','string',['string','string']);construn_sm2_sign=Module.cwrap('run_test_sm2_sign','string',['string','string']);construn_sm2_verify=Module.cwrap('run_test_sm2_verify','int',['string','string','string']);varplaintext="This is funny";try{var ciphertextHex =run_sm2_encrypt(publicKey,plaintext);console.log("Ciphertext (hex):",ciphertextHex);vardec_cipher=run_sm2_decrypt(privateKey,ciphertextHex);console.log("Plaintext:",dec_cipher);// pass param to function to c with stringToHexvarsignature=run_sm2_sign(privateKey,stringToHex(plaintext));console.log("Plaintext signature:",signature);varverify_result=run_sm2_verify(publicKey,stringToHex(plaintext),signature);if(verify_result==1){console.log("Signature Verify: passed",);}else{console.log("Signature Verify: failed",);}}catch(e){console.error("An error occurred:",e);}},1000);
</script></head></html>
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', []); and
Troubleshooting
RuntimeError: Aborted(Assertion failed: native function run_test_sm3 called before runtime initialization)
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)change your function in the below function list
-s EXPORTED_FUNCTIONS='["_run_test_sm3", "_hex_to_bytes", "_sm3_init", "_sm3_update", "_sm3_finish"]'
just export runtime function:
UTF8ToString
EXPORTED_RUNTIME_METHODS='["ccall", "cwrap","UTF8ToString", "stringToUTF8", "getValue"]'
if memroy error with
Module._xxx
Just replace it with
Module.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
The text was updated successfully, but these errors were encountered: