tinygo wasm use c library (AES1 CBC2 mode)
This project demonstrates the integration of a C library with Go-based WebAssembly and a Go HTTP server. The C library is used for AES1 encryption, which is compiled to WebAssembly using Emscripten. The Go WebAssembly module interacts with this AES1 library, and both are served over an HTTP server written in Go.
The project is divided into three main directories:
myaeslib
: A C language-based AES1 library. It is compiled using the Emscripten compiler and includes Makefile for building and installing the library.mywasm
: Contains Go code for the WebAssembly module that utilizes the myaeslib for encryption. It is compiled using the TinyGo compiler and includes Makefile for building and installing.goserver
: A simple HTTP server written in Go that serves the WebAssembly module and provides an interface to interact with the AES1 encryption functionality.
- Install Emscripten for compiling C code to WebAssembly.
- Install TinyGo for compiling Go code to WebAssembly.
- Install Go for running the HTTP server.
Navigate to the myaeslib
directory:
cd myaeslib
Run Makefile commands to build and install the AES1 library:
make
make install
install: copy
libmyaes.a
andmyaes.h
from output directory to mywasm/mycrypto
Navigate to the mywasm
directory:
cd mywasm
Run Makefile commands to build and install the WebAssembly module:
make
make install
install: copy
mywasm.wasm
andwasm_exec.js
from output directory to server/static
Navigate to the goserver
directory:
cd goserver
Run the HTTP server:
go run main.go
The server will start, and you can access the WebAssembly module through the web browser at http://localhost:8080.
Starting the Server
Once the server is running, navigate to http://localhost:8080
in your web browser. You should see an interface that allows you to interact with the AES1 encryption functionality provided by the myaeslib
library through WebAssembly.
Linking the Static Library The C library functions are defined in myaes.h as follows:
int Encrypt(const uint8_t* sdkSecrty, const char* plaintext, char* out);
: Encryption functionint Decrypt(const uint8_t* sdkSecrty, const char* pOutB64, char* out);
: Decryption function
To use these functions in Go code, prepare a Go package myaes.go to link the static library libmyaes.a
.
To call libmyaes.a
using cgo, here's how you can link the static library and call the encryption and decryption functions in your Go code:
/*
#cgo LDFLAGS: -L. -lmyaes
#include "myaes.h"
*/
import "C"
import (
"unsafe"
)
// Assume secrtyKey is a []byte slice containing the security key,
// plaintext is the string to encrypt, and ciphertext is the encrypted string.
// out is used to store the result of encryption or decryption.
// Encryption example
func Encrypt(secrtyKey []byte, plaintext string) string {
var out [128]byte // Assume the output will not exceed 128 bytes
C.Encrypt((*C.uint8_t)(&secrtyKey[0]), C.CString(plaintext), (*C.char)(unsafe.Pointer(&out[0])))
return string(out[:])
}
// Decryption example
func Decrypt(secrtyKey []byte, ciphertext string) string {
var out [128]byte // Assume the output will not exceed 128 bytes
C.Decrypt((*C.uint8_t)(&secrtyKey[0]), C.CString(ciphertext), (*C.char)(unsafe.Pointer(&out[0])))
return string(out[:])
}
Note: These examples assume the output will not exceed 128 bytes. In practice, depending on the size of your encrypted text, you might need to adjust the size of the out
array.
Furthermore, the examples do not handle memory allocation and release for the strings returned from C.CString
and C.Encrypt
/C.Decrypt
calls. In actual applications, ensure proper memory management to avoid memory leaks.
Load WebAssembly in index.html:
// Prepare a 32-byte secret key
let secret = new Uint8Array([31, 63, 210, 217, 114, 192, 171, 225, 29, 225, 12, 140, 3, 19, 110, 132, 188, 228, 132, 35, 144, 38, 168, 153, 210, 203, 150, 127, 141, 239, 8, 22]);
// Initialize AES
AesInit(secret);
// Encrypt
AesEncrypt("Advanced Encryption Standard $$$$$$$$$$$$$$$$$$$")
// Decrypt
AesDecrypt("Tkm0vfvenJfWwNEn/XwogrjOwN3p12U7HLcvtWgE/F4IxlbGGDIpx7KzgLES2fVB")
Contributions are welcome! Please feel free to submit pull requests or open issues to improve the project.
This project is licensed under the MIT License - see the LICENSE file for details.