Hashcash (hashcash v1.2.0)
Elixir implementation of the hashcash algorigthm as described in http://hashcash.org and https://en.wikipedia.org/wiki/Hashcash
Generate a stamp for another party to generate:
Hashcash.resource("foobar") |> Hashcash.resource_format
%Hashcash{
bits: 20,
counter: 0,
date: [year: 2022, month: 9, day: 2],
ext: nil,
rand: "gAbLlrNJFwsKWincKbOvNP6kNkUHRt1",
resource: "foobar",
stamp_base: "1:20:220902:foobar::gAbLlrNJFwsKWincKbOvNP6kNkUHRt1",
stamp_string: "1:20:220902:foobar::gAbLlrNJFwsKWincKbOvNP6kNkUHRt1:0",
version: 1
}
The stamp_string
can be sent to the other party.
To verify:
iex(4)> Hashcash.stamp("1:20:220902:foobar::GszJUJJC+tcQSkvw+GPg7FBYYi289eL:294524") |> Hashcash.verify("foobar")
{:ok, :verified}
Link to this section Summary
Functions
Count leading zero bits in a bitstring
Generate date string section from date keywords list
Generate date keywords list of now
Generate a full stamp, doing the work
Generate the rand field using a crypto.strong_rand_bytes
Create a new %Hashcash with resource_string and today as the date.
Create a new %Hashcash with the resource_string and specified date Keyword list
Modify the bits required of a %Hashcash
Modify the date of a %Hashcash
Return %Hashcash with stamp_base and stamp_string set.
Generate or return hcash.base from properies This excludes the count field so that the generator can use this base with successive iterations of new counts by appending just the count.
Append the counter to the base to make a full stamp string
Set rand field of %Hashcash to newly generated string
Create a %Hashcash stamp from a stamp string. Use this to turn a string that may be passed between parties to a form that can be used with the rest of the functions here.
Validate the stamp string proof-of-work only. Use verify for full check
Verfiy all attributes and proof of work against a list of acceptable resources or a single resource string
Verify stamp resource is a valid resource
Verify date keyword list is within 2 days
Count leading zero bits in SHA1 hash of stamp.stamp_string
Link to this section Types
Link to this section Functions
count_lead_zeros_in_bitstring(bs, count \\ 0)
Count leading zero bits in a bitstring
date_format(date_keywords)
Generate date string section from date keywords list
date_now()
@spec date_now() :: Keyword.t()
Generate date keywords list of now
generate(hcash)
Generate a full stamp, doing the work
rand_generate()
@spec rand_generate() :: String.t()
Generate the rand field using a crypto.strong_rand_bytes
resource(resource_string)
Create a new %Hashcash with resource_string and today as the date.
resource(resource_string, date)
Create a new %Hashcash with the resource_string and specified date Keyword list
resource_bits(hcash, bits)
Modify the bits required of a %Hashcash
resource_date(hcash, y, m, d)
Modify the date of a %Hashcash
resource_format(hcash)
Return %Hashcash with stamp_base and stamp_string set.
resource_format_base(hcash)
Generate or return hcash.base from properies This excludes the count field so that the generator can use this base with successive iterations of new counts by appending just the count.
resource_format_string(base, counter)
Append the counter to the base to make a full stamp string
resource_rand(hcash)
Set rand field of %Hashcash to newly generated string
stamp(stamp_string)
Create a %Hashcash stamp from a stamp string. Use this to turn a string that may be passed between parties to a form that can be used with the rest of the functions here.
strip_trailing_char(string)
validate(hcash)
Validate the stamp string proof-of-work only. Use verify for full check
verify(hcash, valid_resources)
@spec verify(hcash :: t(), valid_resources :: list()) :: tuple()
@spec verify(hcash :: t(), single_resource :: String.t()) :: tuple()
Verfiy all attributes and proof of work against a list of acceptable resources or a single resource string
verify_resource(resource, valid_resources)
Verify stamp resource is a valid resource
verify_time(date)
Verify date keyword list is within 2 days
zero_bits_count(hcash)
Count leading zero bits in SHA1 hash of stamp.stamp_string