//go:build js && wasm // +build js,wasm package main import ( "fmt" "syscall/js" ) // verifySHA256 verifies that the SHA256 hash of the input data matches the provided expected hash. func verifySHA256(this js.Value, args []js.Value) any { if len(args) < 2 { return js.ValueOf(map[string]any{ "valid": false, "error": "invalid arguments: expected (data []uint8, expectedHash string)", }) } dataLen := args[0].Get("length").Int() data := make([]byte, dataLen) steps := []any{} js.CopyBytesToGo(data, args[0]) steps = append(steps, map[string]any{ "name": "Data Loading", "status": "success", "details": fmt.Sprintf("Successfully read %d bytes into WASM memory", dataLen), }) expectedHash := args[1].String() actualHash := computeSHA256(data) steps = append(steps, map[string]any{ "name": "Checksum Calculation", "status": "success", "details": fmt.Sprintf("Computed SHA256: %s", actualHash), }) if actualHash == expectedHash { steps = append(steps, map[string]any{ "name": "Identity Verification", "status": "success", "details": "Computed hash matches the signed manifest", }) return js.ValueOf(map[string]any{ "valid": true, "steps": steps, }) } steps = append(steps, map[string]any{ "name": "Identity Verification", "status": "error", "details": fmt.Sprintf("Hash mismatch! Got %s, expected %s", actualHash, expectedHash), }) return js.ValueOf(map[string]any{ "valid": false, "steps": steps, "error": fmt.Sprintf("hash mismatch: got %s, expected %s", actualHash, expectedHash), }) } // main initializes the WebAssembly module and exposes verifySHA256 to the JavaScript global scope. func main() { c := make(chan struct{}) fmt.Println("Software Verifier WASM Initialized") js.Global().Set("verifySHA256", js.FuncOf(verifySHA256)) <-c }