1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#[macro_use]
extern crate log;

use chrono::{DateTime, NaiveDateTime, Utc};
use digest::Digest;
use rand::distributions::{Alphanumeric, Distribution};
use rand::thread_rng;
use sha1::Sha1;
use sha3::Sha3_256;
use std::convert::TryFrom;
use std::fmt;
use std::time::SystemTime;

simpl::err!(HcError,
    {
        Int@std::num::ParseIntError;
        Time@std::time::SystemTimeError;
    }
);

fn to_iso_32bit_safe(timestamp_secs: u32, short: bool) -> String {
    let mut seconds = timestamp_secs;
    let mut minutes = seconds / 60;
    seconds -= minutes * 60;
    let mut hours = minutes / 60;
    minutes -= hours * 60;
    let mut days = hours / 24;
    hours -= days * 24;
    let mut year = 1970;
    let mut day_of_week = 4;
    let mut mnth = 0;
    loop {
        let leap_year = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
        let days_in_year = if leap_year { 366 } else { 365 };
        if days >= days_in_year {
            day_of_week += if leap_year { 2 } else { 1 };
            days -= days_in_year;
            if day_of_week >= 7 {
                day_of_week -= 7;
            }
            year += 1;
        } else {
            day_of_week += days;
            day_of_week %= 7;

            let days_in_month = vec![31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            for month in 0..12 {
                mnth = month;
                let mut dim = *days_in_month.get(month).unwrap();

                /* add a day to February if this is a leap year */
                if month == 1 && leap_year {
                    dim += 1;
                }

                if days >= dim {
                    days -= dim;
                } else {
                    break;
                }
            }
            mnth += 1;
            days += 1;
            break;
        }
    }
    if short {
        format!("{}-{:02}-{:02}", year, mnth, days)
    } else {
        format!(
            "{}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
            year, mnth, days, hours, minutes, seconds
        )
    }
}

fn _hash<T: Digest>(hasher: &mut T, challenge: &str, bits: u32) -> String {
    let mut counter = 0;
    let hex_digits = ((bits as f32) / 4.).ceil() as usize;
    let zeros = String::from_utf8(vec![b'0'; hex_digits]).unwrap();
    loop {
        hasher.input(&format!("{}:{:x}", challenge, counter).as_bytes());
        let result = hex::encode(hasher.result_reset());
        if result[..hex_digits] == zeros {
            debug!("{}", &result);
            return format!("{:x}", counter);
        };
        counter += 1
    }
}

/// Answer a generalized hashcash version 1 challenge
/// Hashcash requires stamps of form 'ver:bits:date:res:ext:rand:counter'
/// This internal function accepts a generalized prefix 'challenge',
/// and returns only a suffix that produces the requested SHA leading zeros.
///
/// NOTE: Number of requested bits is rounded up to the nearest multiple of 4
fn _mint(challenge: &str, bits: u32) -> String {
    if cfg!(feature = "sha1") {
        let mut hasher = Sha1::new();
        _hash(&mut hasher, challenge, bits)
    } else {
        let mut hasher = Sha3_256::new();
        _hash(&mut hasher, challenge, bits)
    }
}

/// Check whether a stamp is valid
///
/// Optionally, the stamp may be checked for a specific resource, and/or
/// it may require a minimum bit value, and/or it may be checked for
/// expiration, and/or it may be checked for double spending.
///
/// If 'check_expiration' is specified, it should contain an expiration DateTime<Utc>
///
/// NOTE: Every valid (version 1) stamp must meet its claimed bit value
/// NOTE: Check floor of 4-bit multiples (overly permissive in acceptance)
///     """
pub fn check_with_params(
    stamp: &str,
    resource: Option<&str>,
    bits: Option<u32>,
    expiration: Option<DateTime<Utc>>,
) -> Result<bool, HcError> {
    let stamp = Stamp::try_from(stamp)?;
    if !stamp.check_version() {
        return Err(HcError::from(
            format!(
                "Can only check version 1 stamp, got version {}",
                stamp.version
            )
            .as_str(),
        ));
    }
    if !stamp.check_resource(resource) {
        return Ok(false);
    }
    if !stamp.check_bits(bits) {
        return Ok(false);
    }
    if !stamp.check_expiration(expiration) {
        return Ok(false);
    }
    Ok(stamp.check())
}

/// Check whether a stamp is valid
pub fn check(stamp: &str) -> Result<bool> {
    check_with_params(stamp, None, None, None)
}

#[derive(Debug)]
pub struct Stamp {
    version: String,
    claim: u32,
    ts: String,
    resource: String,
    ext: String,
    rand: String,
    counter: String,
}

impl Stamp {
    fn check_version(&self) -> bool {
        self.version == "1"
    }

    fn check_resource(&self, resource: Option<&str>) -> bool {
        if let Some(resource) = resource {
            self.resource == resource
        } else {
            true
        }
    }

    fn check_bits(&self, bits: Option<u32>) -> bool {
        if let Some(bits) = bits {
            bits <= self.claim
        } else {
            true
        }
    }

    fn check_expiration(&self, expiration: Option<DateTime<Utc>>) -> bool {
        if let Some(expiration) = expiration {
            Utc::now() < expiration
        } else {
            true
        }
    }

    fn hex_digits(&self) -> usize {
        ((self.claim as f32) / 4.).floor() as usize
    }

    fn zeroes(&self) -> String {
        String::from_utf8(vec![b'0'; self.hex_digits()]).unwrap()
    }

    fn _check<T: Digest>(&self, hasher: &mut T) -> bool {
        debug!("{}", self.to_string());
        hasher.input(&self.to_string().as_bytes());
        let result = hex::encode(hasher.result_reset());
        debug!("{}", &result);
        result[..self.hex_digits()] == self.zeroes()
    }

    fn check(&self) -> bool {
        if cfg!(feature = "sha1") {
            let mut hasher = Sha1::new();
            self._check(&mut hasher)
        } else {
            let mut hasher = Sha3_256::new();
            self._check(&mut hasher)
        }
    }

    fn format(&self) -> String {
        format!(
            "{}:{}:{}:{}:{}:{}:{}",
            self.version, self.claim, self.ts, self.resource, self.ext, self.rand, self.counter
        )
    }

    /// Like mint() but for webassembly, 32bit system safe
    pub fn mint_wasm(
        resource: Option<&str>,
        bits: Option<u32>,
        now: Option<u32>,
        ext: Option<&str>,
        saltchars: Option<usize>,
        stamp_seconds: bool,
    ) -> Result<Self> {
        let version = "1";

        let timestamp_secs = if let Some(now) = now {
            now
        } else {
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)?
                .as_secs() as u32
        };
        let ts = if stamp_seconds {
            to_iso_32bit_safe(timestamp_secs, false)
        } else {
            to_iso_32bit_safe(timestamp_secs, true)
        };
        let bits = bits.unwrap_or(20);
        let ext = ext.unwrap_or("");
        let saltchars = saltchars.unwrap_or(8);
        let rand = Alphanumeric
            .sample_iter(thread_rng())
            .take(saltchars)
            .collect();
        let resource = resource.unwrap_or("");
        let challenge = format!("{}:{}:{}:{}:{}:{}", version, bits, ts, resource, ext, rand);

        Ok(Stamp {
            version: version.to_string(),
            claim: bits,
            ts,
            resource: resource.to_string(),
            ext: ext.to_string(),
            rand,
            counter: _mint(&challenge, bits),
        })
    }

    /// 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'
    ///
    /// '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.
    pub fn mint(
        resource: Option<&str>,
        bits: Option<u32>,
        now: Option<i64>,
        ext: Option<&str>,
        saltchars: Option<usize>,
        stamp_seconds: bool,
    ) -> Result<Self> {
        let version = "1";
        let now = if let Some(now) = now {
            DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(now, 0), Utc)
        } else {
            Utc::now()
        };
        let ts = if stamp_seconds {
            now.format("%Y%m%d%H%M%S")
        } else {
            now.format("%Y%m%d")
        };
        let bits = bits.unwrap_or(20);
        let ext = ext.unwrap_or("");
        let saltchars = saltchars.unwrap_or(8);
        let rand = Alphanumeric
            .sample_iter(thread_rng())
            .take(saltchars)
            .collect();
        let resource = resource.unwrap_or("");
        let challenge = format!("{}:{}:{}:{}:{}:{}", version, bits, ts, resource, ext, rand);

        Ok(Stamp {
            version: version.to_string(),
            claim: bits,
            ts: ts.to_string(),
            resource: resource.to_string(),
            ext: ext.to_string(),
            rand,
            counter: _mint(&challenge, bits),
        })
    }

    pub fn with_secs() -> Result<Self> {
        Self::mint(None, None, None, None, None, true)
    }

    pub fn with_resource(resource: &str, stamp_seconds: bool) -> Result<Self> {
        Self::mint(Some(resource), None, None, None, None, stamp_seconds)
    }

    pub fn with_bits(bits: u32, stamp_seconds: bool) -> Result<Self> {
        Self::mint(None, Some(bits), None, None, None, stamp_seconds)
    }

    pub fn with_resource_and_bits(resource: &str, bits: u32, stamp_seconds: bool) -> Result<Self> {
        Self::mint(Some(resource), Some(bits), None, None, None, stamp_seconds)
    }
}

impl TryFrom<&str> for Stamp {
    type Error = HcError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let stamp_vec = value.split(':').collect::<Vec<&str>>();
        if stamp_vec.len() != 7 {
            return Err(HcError::from(
                format!("Malformed stamp, expected 6 parts, got {}", stamp_vec.len()).as_str(),
            ));
        }
        Ok(Stamp {
            version: stamp_vec[0].to_string(),
            claim: stamp_vec[1].parse()?,
            ts: stamp_vec[2].to_string(),
            resource: stamp_vec[3].to_string(),
            ext: stamp_vec[4].to_string(),
            rand: stamp_vec[5].to_string(),
            counter: stamp_vec[6].to_string(),
        })
    }
}

impl fmt::Display for Stamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.format())
    }
}

impl Default for Stamp {
    fn default() -> Self {
        Self::mint(None, None, None, None, None, false).unwrap()
    }
}

mod test {
    use crate::check;
    use crate::Stamp;
    use crate::to_iso_32bit_safe;

    #[test]
    fn test_default() {
        let stamp = Stamp::default();
        let result = check(&stamp.to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_secs() {
        let stamp = Stamp::with_secs();
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_resource() {
        let stamp = Stamp::with_resource("test", false);
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_resource_and_seconds() {
        let stamp = Stamp::with_resource("test", true);
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_bits() {
        let stamp = Stamp::with_bits(16, false);
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_bits_and_seconds() {
        let stamp = Stamp::with_bits(16, true);
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_resource_and_bits() {
        let stamp = Stamp::with_resource_and_bits("test", 16, false);
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_with_resource_and_bits_and_seconds() {
        let stamp = Stamp::with_resource_and_bits("test", 16, true);
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_mint() {
        let stamp = Stamp::mint(
            Some("test"),
            Some(15),
            None,
            Some("name1=2"),
            Some(12),
            false,
        );
        assert!(stamp.is_ok());
        let result = check(&stamp.unwrap().to_string());
        assert!(result.is_ok());
        assert!(result.unwrap());
    }

    #[test]
    fn test_check() {
        assert!(check("1:20:20202116:test::Z4p8WaiO:31c14").unwrap());
        assert!(!check("1:20:20202116:test1::Z4p8WaiO:31c14").unwrap());
        assert!(!check("1:20:20202116:test::z4p8WaiO:31c14").unwrap());
        assert!(!check("1:20:20202116:test::Z4p8WaiO:31C14").unwrap());
        assert!(check("0:20:20202116:test::Z4p8WaiO:31c14").is_err());
        assert!(!check("1:19:20202116:test::Z4p8WaiO:31c14").unwrap());
        assert!(!check("1:20:20202115:test::Z4p8WaiO:31c14").unwrap());
    }

    #[test]
    fn test_to_iso() {
        assert_eq!(to_iso_32bit_safe(1592565184, false), "2020-06-19T11:13:04Z")
    }
}