<commit_before>// Copyright (c) 2015, <NAME>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

part of hashcash;

class HashCash {
  static String _salt(int l) {
    String _ascii_letters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ+/=";
    Random rand = new Random();
    String sb = "";
    for (int i = 0; i < l; i++) {
      sb = "$sb${_ascii_letters[rand.nextInt(_ascii_letters.length)]}";
    }
    return sb;
  }

  static String _mint(String challenge, int bits) {
    int counter = 0;
    int hex_digits = (bits / 4.0).ceil();
    String zeros = "0" * hex_digits;
    SHA1 sha1 = new SHA1();
    while (true) {
      String hex_counter = counter.toRadixString(16);
      sha1.add("$challenge$hex_counter".codeUnits);
      String digest = CryptoUtils.bytesToHex(sha1.close());
      if (digest.startsWith(zeros)) {
        return hex_counter;
      }
      counter++;
      sha1 = sha1.newInstance();
    }
  }

  static String mint(String resource,
                     {int bits: 20, DateTime now: null, String ext: '',
                     int saltchars: 16, bool stamp_seconds: true}) {
    String ts = "";
    String challenge;
    String ver = "1";
    String iso_now = now == null ?
        new DateTime.now().toIso8601String() : now.toIso8601String();
    ts = "$ts${iso_now.substring(2, 4)}";
    ts = "$ts${iso_now.substring(5, 7)}";
    ts = "$ts${iso_now.substring(8, 10)}";
    if (stamp_seconds) {
      ts = "$ts${iso_now.substring(11, 13)}";
      ts = "$ts${iso_now.substring(14, 16)}";
      ts = "$ts${iso_now.substring(17, 19)}";
    }
    challenge = "$ver:$bits:$ts:$resource:$ext:${_salt(saltchars)}:";
    return "$challenge${_mint(challenge, bits)}";
  }

  static bool check(String stamp,
                    {String resource: null,
                    int bits: null,
                    Duration check_expiration: null}) {
    if (stamp.startsWith("1:")) {
      List<String> data = stamp.split(":");
      if (data.length == 7) {
        String ver = data[0];
        int claim = int.parse(data[1], onError: (e) => -1);
        String date_time = data[2];<commit_msg>improve timestamp generation<commit_after>// Copyright (c) 2015, <NAME>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

part of hashcash;

class HashCash {
  static String _salt(int l) {
    String _ascii_letters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ+/=";
    Random rand = new Random();
    String sb = "";
    for (int i = 0; i < l; i++) {
      sb = "$sb${_ascii_letters[rand.nextInt(_ascii_letters.length)]}";
    }
    return sb;
  }

  static String _mint(String challenge, int bits) {
    int counter = 0;
    int hex_digits = (bits / 4.0).ceil();
    String zeros = "0" * hex_digits;
    SHA1 sha1 = new SHA1();
    while (true) {
      String hex_counter = counter.toRadixString(16);
      sha1.add("$challenge$hex_counter".codeUnits);
      String digest = CryptoUtils.bytesToHex(sha1.close());
      if (digest.startsWith(zeros)) {
        return hex_counter;
      }
      counter++;
      sha1 = sha1.newInstance();
    }
  }

  static String mint(String resource,
                     {int bits: 20, DateTime now: null, String ext: '',
                     int saltchars: 16, bool stamp_seconds: true}) {
    String ts = "";
    String challenge;
    String ver = "1";
    String iso_now = now == null ?
        new DateTime.now().toIso8601String() : now.toIso8601String();
    iso_now = iso_now.replaceAll("-", "");
    iso_now = iso_now.replaceAll(":", "");
    List<String> date_time = iso_now.split("T");
    ts = date_time[0];
    if (stamp_seconds) {
      ts = "$ts${date_time[1].substring(0, 6)}";
    }
    challenge = "$ver:$bits:$ts:$resource:$ext:${_salt(saltchars)}:";
    return "$challenge${_mint(challenge, bits)}";
  }

  static bool check(String stamp,
                    {String resource: null,
                    int bits: null,
                    Duration check_expiration: null}) {
    if (stamp.startsWith("1:")) {
      List<String> data = stamp.split(":");
      if (data.length == 7) {
        String ver = data[0];
        int claim = int.parse(data[1], onError: (e) => -1);
        String date_time = data[2];
{'fraction_non_alphanumeric': 0.14945714945714947, 'fraction_numerical': 0.022407022407022407, 'mean_word_length': 2.5933609958506225, 'pattern_counts': {'":': 3, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3267720', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1410, 'n_words': 361}