check static method

bool check(
  1. String stamp,
  2. {String? resource,
  3. int bits = 20,
  4. Duration? checkExpiration}
)

Implementation

static bool check(String stamp,
    {String? resource, int bits = 20, Duration? checkExpiration}) {
  if (stamp.isEmpty) return false;

  if (stamp.startsWith('0:')) {
    final stampParts = stamp.substring(2).split(':');

    if (stampParts.length != 3) return false;

    final date = stampParts[0];
    final res = stampParts[1];

    final dt = _currentDate(date);
    if (dt == null) return false;

    if (resource != null && resource != res) return false;

    if (checkExpiration != null) {
      final goodUntil = dt.add(checkExpiration);
      final now = DateTime.now();

      if (now.isAfter(goodUntil)) return false;
    }

    final hexDigits = (bits / 4).floor();
    return crypto.sha1
        .convert(stamp.codeUnits)
        .toString()
        .startsWith('0' * hexDigits);
  } else if (stamp.startsWith('1:')) {
    final stampParts = stamp.substring(2).split(':');
    if (stampParts.length != 6) return false;

    final claim = int.parse(stampParts[0]);
    final date = stampParts[1];
    final res = stampParts[2];
    final dt = _currentDate(date);

    if (dt == null) return false;
    if (resource != null && resource != res) return false;
    if (bits != claim) return false;

    if (checkExpiration != null) {
      final goodUntil = dt.add(checkExpiration);
      final now = DateTime.now();

      if (now.isAfter(goodUntil)) return false;
    }

    final hexDigits = (claim / 4).floor();
    return crypto.sha1
        .convert(stamp.codeUnits)
        .toString()
        .startsWith('0' * hexDigits);
  } else {
    if (resource != null && !stamp.contains(resource)) return false;

    final hexDigits = (bits / 4).floor();
    return crypto.sha1
        .convert(stamp.codeUnits)
        .toString()
        .startsWith('0' * hexDigits);
  }
}