]> gitweb.factorcode.org Git - factor.git/blob - extra/hashcash/hashcash.factor
basis/,core/,extra/: many new tags
[factor.git] / extra / hashcash / hashcash.factor
1 ! Copyright (C) 2009 Diego Martinelli.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar checksums checksums.openssl classes.tuple
4 formatting fry io.encodings.ascii io.encodings.string kernel math
5 math.functions math.parser math.ranges present random sequences
6 splitting ;
7 IN: hashcash
8
9 ! Hashcash implementation
10 ! Reference materials listed below:
11 !
12 ! http://hashcash.org
13 ! http://en.wikipedia.org/wiki/Hashcash
14 ! http://www.ibm.com/developerworks/linux/library/l-hashcash.html?ca=dgr-lnxw01HashCash
15 !
16 ! And the reference implementation (in python):
17 ! http://www.gnosis.cx/download/gnosis/util/hashcash.py
18
19 <PRIVATE
20
21 ! Return a string with today's date in the form YYMMDD
22 : get-date ( -- str )
23     now "%y%m%d" strftime ;
24
25 ! Random salt is formed by ascii characters
26 ! between 33 and 126
27 : available-chars ( -- seq )
28     33 126 [a,b] [ CHAR: : = ] reject ;
29
30 PRIVATE>
31
32 ! Generate a 'length' long random salt
33 : salt ( length -- salted )
34     available-chars '[ _ random ] "" replicate-as ;
35
36 TUPLE: hashcash version bits date resource ext salt suffix ;
37
38 : <hashcash> ( -- tuple )
39     hashcash new
40         1 >>version
41         20 >>bits
42         get-date >>date
43         8 salt >>salt ;
44
45 M: hashcash string>>
46     tuple-slots [ present ] map ":" join ;
47
48 <PRIVATE
49
50 : sha1-checksum ( str -- bytes )
51     ascii encode openssl-sha1 checksum-bytes ; inline
52
53 : set-suffix ( tuple guess -- tuple )
54     >hex >>suffix ;
55
56 : get-bits ( bytes -- str )
57     [ >bin 8 CHAR: 0 pad-head ] { } map-as concat ;
58
59 : checksummed-bits ( tuple -- relevant-bits )
60     dup string>> sha1-checksum
61     swap bits>> 8 / ceiling head get-bits ;
62
63 : all-char-zero? ( seq -- ? )
64     [ CHAR: 0 = ] all? ; inline
65
66 : valid-guess? ( checksum tuple -- ? )
67     bits>> head all-char-zero? ;
68
69 : (mint) ( tuple counter -- tuple )
70     2dup set-suffix checksummed-bits pick
71     valid-guess? [ drop ] [ 1 + (mint) ] if ;
72
73 PRIVATE>
74
75 : mint* ( tuple -- stamp )
76     0 (mint) string>> ;
77
78 : mint ( resource -- stamp )
79     <hashcash>
80         swap >>resource
81     mint* ;
82
83 ! One might wanna add check based on the date,
84 ! passing a 'good-until' duration param
85 : check-stamp ( stamp -- ? )
86     dup ":" split [ sha1-checksum get-bits ] dip
87     second string>number head all-char-zero? ;