Documentation ¶
Overview ¶
Package hashcash contains the hashcash v1 implementation.
Hashcash is a proof-of-work system used to limit email spam and denial-of-service attacks. Read more: - https://en.wikipedia.org/wiki/Hashcash - https://www.hashcash.org/
Index ¶
Constants ¶
const ( // DateFormatYY is the date format used in hashcash, // indicating a stamp validity period of 2 years or more. DateFormatYY DateFormat = "06" // 2006 // DateFormatYYMM is the date format used in hashcash, // indicating a stamp validity period between 2 months and 2 years. DateFormatYYMM DateFormat = "0601" // DateFormatYYMMDD is the date format used in hashcash, // indicating a stamp validity period between 2 days and 2 months. DateFormatYYMMDD DateFormat = "060102" // DateFormatYYMMDDhhmm is the date format used in hashcash, // indicating a stamp validity period between 2 minutes and 2 days. DateFormatYYMMDDhhmm DateFormat = "0601021504" // DateFormatYYMMDDhhmmss is the date format used in hashcash, // indicating stamp validity period less than 2 minutes. DateFormatYYMMDDhhmmss DateFormat = "060102150455" // MaxDurationYYMM is the maximum duration for which the date format is YYMM. MaxDurationYYMM = 2 * 365 * 24 * time.Hour // 2 years // MaxDurationYYMMDD is the maximum duration for which the date format is YYMMDD. MaxDurationYYMMDD = 2 * 30 * 24 * time.Hour // 2 months // MaxDurationYYMMDDhhmm is the maximum duration for which the date format is YYMMDDhhmm. MaxDurationYYMMDDhhmm = 2 * 24 * time.Hour // 2 days // MaxDurationYYMMDDhhmmss is the maximum duration for which the date format is YYMMDDhhmmss. MaxDurationYYMMDDhhmmss = 2 * time.Minute // 2 minutes )
const ( // Version is the hashcash version. // It is always 1, as of this writing. Version = 1 // ValidPartsNumber is the number of parts in a valid hashcash string. ValidPartsNumber = 7 )
Variables ¶
var ( // ErrExpiredHashcash is returned when hashcash is expired. ErrExpiredHashcash = errors.New("hashcash is expired") // ErrNilHashcash is returned when hashcash is nil. ErrNilHashcash = errors.New("hashcash is nil") // ErrEmptyHashcash is returned when hashcash is empty. ErrEmptyHashcash = errors.New("hashcash is empty") // ErrIncorrectNumberOfParts is returned when the number of parts contained in the hashcash string is incorrect. ErrIncorrectNumberOfParts = errors.New("incorrect number of parts") // ErrInvalidVersion is returned when the hashcash version is invalid. ErrInvalidVersion = errors.New("hashcash version is invalid") // ErrInvalidSaltLength is returned when the salt length is invalid. ErrInvalidSaltLength = errors.New("invalid salt length") // ErrInvalidDifficulty is returned when the difficulty is invalid. ErrInvalidDifficulty = errors.New("invalid difficulty") // ErrInvalidDateFormat is returned when the date format is invalid. ErrInvalidDateFormat = errors.New("invalid date format") // ErrIncorrectSolution is returned when the hashcash solution is incorrect. ErrIncorrectSolution = errors.New("incorrect solution") // ErrAttemptToUseFutureHashcash is returned when the hashcash date is in the future. ErrAttemptToUseFutureHashcash = errors.New("attempt to use future hashcash") )
Functions ¶
func CheckSolution ¶
CheckSolution parses solution string and checks if the hashcash is solved and not expired.
Types ¶
type DateFormat ¶
type DateFormat string
DateFormat is the date format used in hashcash.
func ParseDateFormat ¶
func ParseDateFormat(date string) (DateFormat, error)
ParseDateFormat parses the date format from the given string. It is a rough implementation of the date format parser, but it is enough for the purpose of it's use.
func (DateFormat) IsValid ¶
func (df DateFormat) IsValid() bool
IsValid checks if the DateFormat is valid.
func (DateFormat) String ¶
func (df DateFormat) String() string
String returns the string representation of the DateFormat.
type Hashcash ¶
type Hashcash struct {
// contains filtered or unexported fields
}
Hashcash is a representation of a hashcash version 1.
func New ¶
func New(difficulty, saltLen int, dateFormat DateFormat, resource string) (*Hashcash, error)
New creates a new hashcash. It accepts the number of leading zeros and the resource. It returns a pointer to the hashcash and an error, if any.
func ParseStr ¶
ParseStr parses the hashcash string to the hashcash struct, validating it in the process. It accepts the hashcash string, expecting it to be in the following format: <version>:<difficulty>:<date>:<resource>:<extension>:<salt>:<counter hash> It returns a pointer to the hashcash and an error, if any.
func (*Hashcash) HasExpired ¶
HasExpired checks if the hashcash has expired.