1+ // Package main provides a shared library for signing and verification.
12package main
23
34// #include <stdlib.h>
45import "C"
6+
57import (
68 "crypto/sha256"
79 "encoding/hex"
@@ -10,52 +12,62 @@ import (
1012 "unsafe"
1113)
1214
15+ // GetVersion returns the version string.
16+ //
1317//export GetVersion
1418func GetVersion () * C.char {
1519 return C .CString ("1.0.0" )
1620}
1721
22+ // GetPlatform returns the platform string.
23+ //
1824//export GetPlatform
1925func GetPlatform () * C.char {
2026 return C .CString (fmt .Sprintf ("%s/%s" , runtime .GOOS , runtime .GOARCH ))
2127}
2228
29+ // Sign creates a signature for the given data.
30+ //
2331//export Sign
2432func Sign (data * C.char ) * C.char {
2533 if data == nil {
2634 return C .CString ("" )
2735 }
28-
36+
2937 goData := C .GoString (data )
3038 hash := sha256 .Sum256 ([]byte (goData ))
3139 signature := hex .EncodeToString (hash [:])
32-
40+
3341 return C .CString (signature )
3442}
3543
44+ // Verify checks if the signature matches the data.
45+ //
3646//export Verify
37- func Verify (data * C. char , signature * C.char ) C.int {
47+ func Verify (data , signature * C.char ) C.int {
3848 if data == nil || signature == nil {
3949 return 0
4050 }
41-
51+
4252 goData := C .GoString (data )
4353 goSignature := C .GoString (signature )
44-
54+
4555 hash := sha256 .Sum256 ([]byte (goData ))
4656 expectedSignature := hex .EncodeToString (hash [:])
47-
57+
4858 if expectedSignature == goSignature {
4959 return 1
5060 }
5161 return 0
5262}
5363
64+ // FreeString frees a C string allocated by the library.
65+ //
5466//export FreeString
5567func FreeString (str * C.char ) {
5668 C .free (unsafe .Pointer (str ))
5769}
5870
5971func main () {
6072 // Required for building shared library
61- }
73+ }
0 commit comments