Add SHA256 computation and verification functionality

- 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.
This commit is contained in:
2025-12-27 15:32:42 -06:00
parent fe67dc4d3e
commit c58384285a
3 changed files with 88 additions and 0 deletions

36
software-verifier/main.go Normal file
View File

@@ -0,0 +1,36 @@
//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
}

View File

@@ -0,0 +1,39 @@
package main
import (
"testing"
)
func TestComputeSHA256(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Empty string",
input: "",
expected: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
{
name: "Hello world",
input: "hello world",
expected: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
},
{
name: "Software Station",
input: "Software Station",
expected: "2f94338443740cd0ca2afbd1c177f8a624a521d1668d9eaf4e2439ae06e32b11",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := computeSHA256([]byte(tt.input))
if actual != tt.expected {
t.Errorf("computeSHA256() = %v, want %v", actual, tt.expected)
}
})
}
}

View File

@@ -0,0 +1,13 @@
package main
import (
"crypto/sha256"
"encoding/hex"
)
// computeSHA256 returns the hex-encoded SHA256 hash of the input data.
func computeSHA256(data []byte) string {
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}