Documentation ¶
Overview ¶
Package hashcash a library which implements the hashcash proof-of-work algorithm.
Computing a hashcash:
package main import ( "fmt" "github.com/umahmood/hashcash" ) func validateResource(resource string) bool { // validate resource. resource could be an email, ip // address, etc... so for an email we could check it // exists in the database. return true } func main() { hc, err := hashcash.New( &hashcash.Resource{ Data: "someone@gmail.com", ValidatorFunc: validateResource, }, nil, // use default config. ) if err != nil { // handle error } solution, err := hc.Compute() if err != nil { if err != hashcash.ErrSolutionFail { // did not find a solution, can call compute again. } } fmt.Println(solution) }
Verifying a hashcash:
valid, err := hc.Verify(solution) if err != nil { // handle error } if !valid { // hashcash token failed verification. }
Index ¶
Constants ¶
View Source
const ( Major = 1 Minor = 0 Patch = 0 )
Semantic versioning - http://semver.org/
Variables ¶
View Source
var ( // ErrSolutionFail error cannot compute a solution ErrSolutionFail = errors.New("exceeded 2^20 iterations failed to find solution") // ErrResourceEmpty error empty hashcash resource ErrResourceEmpty = errors.New("empty hashcash resource") // ErrInvalidHeader error invalid hashcash header format ErrInvalidHeader = errors.New("invalid hashcash header format") // ErrNoCollision error n 5 most significant hex digits (n most significant // bits are not 0. ErrNoCollision = errors.New("no collision most significant bits are not zero") // ErrTimestamp error futuristic and expired time stamps are rejected ErrTimestamp = errors.New("time stamp is too far into the future or expired") // ErrResourceFail error hashcash resource data did not pass validation ErrResourceFail = errors.New("resource data did not pass validation") // ErrSpent error avoid accepting the same stamp twice ErrSpent = errors.New("hashcash has already been spent") )
View Source
var DefaultConfig = &Config{ Bits: 20, Future: time.Now().AddDate(0, 0, 2), Expired: time.Now().AddDate(0, 0, -30), }
DefaultConfig default hashcash configuration
Functions ¶
Types ¶
type Config ¶
type Config struct { // Bits recommended default collision sizes are 20-bits Bits int // Expiry time before hashcash tokens are considered expired. Recommended // expiry time is 28 days Expired time.Time // Future hashcash in the future that should be rejected. Recommended // tolerance for clock skew is 48 hours Future time.Time // Storage underlying storage where hashcash tokens are stored and retrieved. Storage Storage }
Config for a hashcash instance
type Hashcash ¶
type Hashcash struct {
// contains filtered or unexported fields
}
Hashcash instance
type Resource ¶
type Resource struct { // Data email, IP address, etc... Data string // ValidatorFunc user supplied function which validates Data ValidatorFunc func(string) bool }
Resource represents a hashcash resource
Source Files ¶
Click to show internal directories.
Click to hide internal directories.