Go WebAssembly — AES Encryption in the Browser
Created: March 18, 2023 · Last Updated: December 6, 2023
An experimental project exploring the boundary between Go and the web platform, using WebAssembly (WASM) to run Go code directly inside the browser — no server required for the encryption logic.
What is This?
This project demonstrates how to compile Go into a WebAssembly binary and call it from a standard HTML/JavaScript frontend. The core functionality is AES encryption and decryption, running entirely client-side through the compiled WASM module.
Live demo: go-web-assembly.web.app
Why Go + WebAssembly?
Go's standard library includes a solid crypto/aes implementation. By compiling it to WASM, we get:
- Zero-dependency encryption in the browser — no npm crypto packages
- Consistent behavior between server-side Go code and the browser
- An exploration of the Go WASM toolchain and how it interacts with JavaScript
Stack
| Layer | Technology |
|---|---|
| Core Logic | Go (crypto/aes) |
| Platform | WebAssembly (GOARCH=wasm GOOS=js) |
| Frontend | HTML, JavaScript, CSS |
| Hosting | Firebase Hosting |
| CI/CD | GitHub Actions |
How It Works
Go's WASM build target compiles to a .wasm binary. The browser loads it using JavaScript's WebAssembly APIs with the Go-provided wasm_exec.js runtime bridge. Go functions registered via js.Global().Set(...) become callable directly from JavaScript.
// Go function exposed to JavaScript js.Global().Set("encryptAES", js.FuncOf(func(this js.Value, args []js.Value) interface{} { plaintext := args[0].String() key := args[1].String() // AES encryption logic here return encrypted }))
// Called from the browser const encrypted = encryptAES(plaintext, secretKey);
Build
GOARCH=wasm GOOS=js go build -o public/main.wasm ./src
Links
- GitHub: luthfiyufajjiru/go-webassembly
- Live Demo: go-web-assembly.web.app