diff --git a/Makefile b/Makefile index 190a55e..861b791 100644 --- a/Makefile +++ b/Makefile @@ -7,3 +7,9 @@ build-rust: test: cd ./mcaptcha-pow-ffi/ && cargo test + +example: + go run examples/main.go ^C + +test: + go test diff --git a/examples/main.go b/examples/main.go new file mode 100644 index 0000000..59ce1a7 --- /dev/null +++ b/examples/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "C" + "fmt" + + "git.batsense.net/mCaptcha/gomcaptcha" +) + +func main() { + + salt := "56126a526f6f1e6728e904d8b574ebb8" + phrase := "ef055efeac72b99ed24af687e49d6ec7" + difficulty := uint(50000) + + res := gomcaptcha.ProveWork(salt, phrase, difficulty) + fmt.Println(res.Nonce) + fmt.Println(res.Result) + fmt.Println(res.IsSufficientDifficulty(salt, difficulty)) + fmt.Println(res.IsValidProof(phrase, salt)) + +} diff --git a/go.mod b/go.mod index d7d96d4..d69b242 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ -module git.batsense.net/mCaptcha/mcaptcha-pow-go +module git.batsense.net/mCaptcha/gomcaptcha go 1.21.5 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8cf6655 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mcaptcha_pow_go.go b/gomcaptcha.go similarity index 75% rename from mcaptcha_pow_go.go rename to gomcaptcha.go index 317446a..ba75f03 100644 --- a/mcaptcha_pow_go.go +++ b/gomcaptcha.go @@ -1,4 +1,4 @@ -package "mcaptcha_pow_go" +package gomcaptcha /* #cgo LDFLAGS: ./lib/libmcaptcha_pow_ffi.a -ldl @@ -6,11 +6,10 @@ package "mcaptcha_pow_go" #include */ import "C" -import "fmt" type ProofOfWork struct { - nonce uint64 - result string + Nonce uint64 + Result string } func ProveWork(salt string, phrase string, difficulty uint) ProofOfWork { @@ -18,16 +17,16 @@ func ProveWork(salt string, phrase string, difficulty uint) ProofOfWork { C.CString(phrase), C.uint(difficulty)) return ProofOfWork{ - nonce: uint64(res.nonce), - result: C.GoString(res.result), + Nonce: uint64(res.nonce), + Result: C.GoString(res.result), } } func (v ProofOfWork) IsSufficientDifficulty(salt string, difficulty uint) bool { res := C.is_sufficient_difficulty( - C.ulong(v.nonce), - C.CString(v.result), + C.ulong(v.Nonce), + C.CString(v.Result), C.CString(salt), C.uint(difficulty)) return bool(res) @@ -36,8 +35,8 @@ func (v ProofOfWork) IsSufficientDifficulty(salt string, difficulty uint) bool { func (v ProofOfWork) IsValidProof(phrase string, salt string) bool { res := C.is_valid_proof( - C.ulong(v.nonce), - C.CString(v.result), + C.ulong(v.Nonce), + C.CString(v.Result), C.CString(phrase), C.CString(salt)) return bool(res) diff --git a/gomcaptcha_test.go b/gomcaptcha_test.go new file mode 100644 index 0000000..35845ae --- /dev/null +++ b/gomcaptcha_test.go @@ -0,0 +1,18 @@ +package gomcaptcha + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGoMcaptcha(t *testing.T) { + + salt := "56126a526f6f1e6728e904d8b574ebb8" + phrase := "ef055efeac72b99ed24af687e49d6ec7" + difficulty := uint(50000) + + res := ProveWork(salt, phrase, difficulty) + assert.True(t, res.IsSufficientDifficulty(salt, difficulty)) + assert.True(t, res.IsValidProof(phrase, salt)) +}