- Implemented computeSHA256 function to generate hex-encoded SHA256 hashes. - Added verifySHA256 function to validate input data against expected hash in a WebAssembly context. - Created main_test.go to include unit tests for computeSHA256 with various input cases.
37 lines
947 B
Go
37 lines
947 B
Go
//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("invalid arguments: expected (data []uint8, expectedHash string)")
|
|
}
|
|
|
|
dataLen := args[0].Get("length").Int()
|
|
data := make([]byte, dataLen)
|
|
js.CopyBytesToGo(data, args[0])
|
|
|
|
expectedHash := args[1].String()
|
|
actualHash := computeSHA256(data)
|
|
|
|
if actualHash == expectedHash {
|
|
return js.ValueOf(true)
|
|
}
|
|
return js.ValueOf(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
|
|
}
|