mint static method

String mint(
  1. String resource, {
  2. int bits = 20,
  3. DateTime? now,
  4. String extension = '',
  5. int saltChars = 8,
  6. bool stampSeconds = false,
})

Mint a new hashcash stamp for 'resource' with 'bits' of collision

20 bits of collision is the default.

'ext' lets you add your own extensions to a minted stamp. Specify an extension as a string of form 'name1=2,3;name2;name3=var1=2,2,val' FWIW, urllib.urlencode(dct).replace('&',';') comes close to the hashcash extension format.

'saltChars' specifies the length of the salt used; this version defaults 8 chars, rather than the C version's 16 chars. This still provides about 17 million salts per resource, per timestamp, before birthday paradox collisions occur. Really paranoid users can use a larger salt though.

'stamp_seconds' lets you add the option time elements to the datestamp. If you want more than just day, you get all the way down to seconds, even though the spec also allows hours/minutes without seconds.

Implementation

static String mint(String resource,
    {int bits = 20,
    DateTime? now,
    String extension = '',
    int saltChars = 8,
    bool stampSeconds = false}) {
  var isoNow = now?.toIso8601String() ?? DateTime.now().toIso8601String();
  isoNow = isoNow.replaceAll('-', '').replaceAll(':', '');
  final dateTime = isoNow.split('T');
  var ts = dateTime[0].substring(2, dateTime[0].length);

  if (stampSeconds) {
    ts = '$ts${dateTime[1].substring(0, 6)}';
  }

  final challenge = <String>[
    version.toString(),
    bits.toString(),
    ts,
    resource,
    extension,
    _salt(saltChars)
  ];

  final mint = _mint(challenge.join(':'), bits);
  challenge.add(mint);
  return challenge.join(':');
}