hashcash

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2020 License: MPL-2.0 Imports: 8 Imported by: 0

README

hashcash

HTTP Hashcash implemented in Go.

Explanation at https://therootcompany.com/blog/http-hashcash/

Go API docs at https://pkg.go.dev/git.rootprojects.org/root/hashcash?tab=doc

CLI Usage

Install:

go get git.rootprojects.org/root/hashcash/cmd/hashcash

Usage:

Usage:
	hashcash new [subject *] [expires in 5m] [difficulty 10]
	hashcash parse <hashcash>
	hashcash solve <hashcash>
	hashcash hash <hashcash>
	hashcash verify <hashcash> [subject *]

Example:

my_hc=$(hashcash new)
echo New: $my_hc
hashcash parse "$my_hc"
echo ""

my_hc=$(hashcash solve "$my_hc")
echo Solved: $my_hc
hashcash parse "$my_hc"
echo ""

hashcash verify "$my_hc"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExpired = errors.New("expired hashcash")

ErrExpired is returned when the current time is past that of ExpiresAt

View Source
var ErrInvalidDate = errors.New("invalid date")

ErrInvalidDate is returned when the date cannot be parsed as a positive int64

View Source
var ErrInvalidDifficulty = errors.New("the number of bits of difficulty is too low or too high")

ErrInvalidDifficulty is returned when the difficulty is outside of the acceptable range

View Source
var ErrInvalidSolution = errors.New("the given solution is not valid")

ErrInvalidSolution is returned when the given hashcash is not properly solved

View Source
var ErrInvalidSubject = errors.New("the subject is invalid or rejected")

ErrInvalidSubject is returned when the subject is invalid or does not match that passed to Verify()

View Source
var ErrInvalidTag = errors.New("expected tag to be 'H'")

ErrInvalidTag is returned when the Hashcash version is unsupported

View Source
var ErrParse = errors.New("could not split the hashcash parts")

ErrParse is returned when fewer than 6 or more than 7 segments are split

View Source
var ErrUnsupportedAlgorithm = errors.New("the given algorithm is invalid or not supported")

ErrUnsupportedAlgorithm is returned when the given algorithm is not supported

View Source
var MaxDifficulty = 26

MaxDifficulty is the upper bound for all Solve() operations

View Source
var Sep = ":"

Sep is the separator character to use

Functions

This section is empty.

Types

type Hashcash

type Hashcash struct {
	Tag        string    `json:"tag"`        // Always "H" for "HTTP"
	Difficulty int       `json:"difficulty"` // Number of "partial pre-image" (zero) bits in the hashed code
	ExpiresAt  time.Time `json:"exp"`        // The timestamp that the hashcash expires, as seconds since the Unix epoch
	Subject    string    `json:"sub"`        // Resource data string being transmitted, e.g., a domain or URL
	Nonce      string    `json:"nonce"`      // Unique string of random characters, encoded as url-safe base-64
	Alg        string    `json:"alg"`        // always SHA-256 for now
	Solution   string    `json:"solution"`   // Binary counter, encoded as url-safe base-64
}

Hashcash represents a parsed Hashcash string

func New

func New(h Hashcash) *Hashcash

New returns a Hashcash with reasonable defaults

func Parse

func Parse(hc string) (*Hashcash, error)

Parse will (obviously) parse the hashcash string, without verifying any of the parameters.

func (*Hashcash) Solve

func (h *Hashcash) Solve(maxDifficulty int) error

Solve will search for a solution, returning an error if the difficulty is above the local or global MaxDifficulty, the Algorithm is unsupported.

func (*Hashcash) String

func (h *Hashcash) String() string

String will return the formatted Hashcash, omitting the solution if it has not be solved.

func (*Hashcash) Verify

func (h *Hashcash) Verify(subject string) error

Verify the Hashcash based on Difficulty, Algorithm, ExpiresAt, Subject and, of course, the Solution and hash.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL