Paste: Hashcash
Author: | _martind |
Mode: | factor |
Date: | Fri, 8 May 2009 21:39:19 |
Plain Text |
USING: accessors byte-arrays calendar calendar.format
checksums checksums.sha1 classes.tuple fry kernel make math
math.functions math.parser math.ranges present random
sequences strings syntax ;
IN: hashcash
<PRIVATE
: get-date
[ now >date< [ pad-00 dup % ] tri@ ] "" make
[ 3drop ] dip 2 8 rot subseq ;
: available-chars
33 126 [a,b] [ 1string ] map [ ":" = not ] filter ;
PRIVATE>
: salt
'[ _ [ available-chars random % ] times ] "" make ;
TUPLE: hashcash version bits date resource ext salt suffix ;
: <hashcash>
hashcash new
1 >>version
16 >>bits
get-date >>date
"" >>ext
8 salt >>salt ;
M: hashcash string>>
tuple-slots [ present ] map ":" join ;
<PRIVATE
: next-suffix
>hex >>suffix ;
: get-checksum
string>> sha1 checksum-bytes hex-string ;
: extract-bits
bits>> 4 / ceiling 0 spin subseq ;
: is-valid?
[ CHAR: 0 = ] all? ;
: (mint)
2dup next-suffix get-checksum pick
extract-bits is-valid? [ drop ] [ 1+ (mint) ] if ;
PRIVATE>
: mint*
0 (mint) string>> ;
: mint
<hashcash>
swap >>resource
mint* ;
Author: | _martind |
Mode: | factor |
Date: | Fri, 8 May 2009 23:52:52 |
Plain Text |
USING: accessors byte-arrays calendar calendar.format
checksums checksums.openssl classes.tuple fry kernel make math
math.functions math.parser math.ranges present random
sequences strings syntax ;
IN: hashcash
<PRIVATE
: get-date
now [ year>> 100 mod pad-00 ]
[ month>> pad-00 ]
[ day>> pad-00 ]
tri 3append ;
: available-chars
33 126 [a,b] [ CHAR: : = not ] filter ;
PRIVATE>
: salt
available-chars '[ _ random ] "" replicate-as ;
TUPLE: hashcash version bits date resource ext salt suffix ;
: <hashcash>
hashcash new
1 >>version
20 >>bits
get-date >>date
8 salt >>salt ;
M: hashcash string>>
tuple-slots [ present ] map ":" join ;
<PRIVATE
: next-suffix
>hex >>suffix ;
: get-checksum
dup string>> openssl-sha1 checksum-bytes
swap bits>> 8 / ceiling head
[ >bin 8 CHAR: 0 pad-head ] { } map-as concat ;
: is-valid?
bits>> head [ CHAR: 0 = ] all? ;
: (mint)
2dup next-suffix get-checksum pick
is-valid? [ drop ] [ 1+ (mint) ] if ;
PRIVATE>
: mint*
0 (mint) string>> ;
: mint
<hashcash>
swap >>resource
mint* ;
New Annotation