Hashcash is a Go library which implements the hashcash proof-of-work algorithm. Hashcash has been used as a denial-of-service counter measure technique in a number of systems. To learn more about hashcash visit official page.
Computing a valid hashcash:
package main
import (
"context"
"fmt"
"github.com/PoW-HC/hashcash/pkg/hash"
"github.com/PoW-HC/hashcash/pkg/pow"
)
const maxIterations = 1 << 30
func main() {
hasher, err := hash.NewHasher("sha256")
if err != nil {
// handle error
}
p := pow.New(hasher)
hashcash, err := pow.InitHashcash(5, "127.0.0.1", pow.SignExt("secret", hasher))
if err != nil {
// handle error
}
solution, err := p.Compute(context.Background(), hashcash, maxIterations)
if err != nil {
// handle error
}
fmt.Println(solution)
}
Outputs:
1:5:1649257375:127.0.0.1:41965c8500f67f79b35672d7fb7e19fe5af0d51da582cbfcdbedb0f5944198bb:MgjmhCBiRV4=:MTk3YzA0
echo -n "1:5:1649257375:127.0.0.1:41965c8500f67f79b35672d7fb7e19fe5af0d51da582cbfcdbedb0f5944198bb:MgjmhCBiRV4=:MTk3YzA0" | shasum -a 256
0000067c716fa612cee5eb31eab6163e804c002841cfe96cc81f4f8034fd6006 -
Verification token:
err := p.Verify(solution, "127.0.0.1")
if err != nil {
// hashcash token failed verification.
}
https://pkg.go.dev/github.com/PoW-HC/hashcash
See the LICENSE file for license rights and limitations (MIT).