Simple hashcash implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barnesc@engr.orst.edu

    Simple hashcash implementation


    Hi!

    Here's a simple hashcash implementation in Python.

    28 lines of actual code.

    Can be reduced to 17 lines for instructional purposes,
    if you don't want clustering, and use xrange() instead
    of irange().

    I tried to make this as simple and elegant as possible.
    Feel free to repost this code, if you can make the it
    more simple.

    I have made no attempt to optimize this code.

    # ----------------------------------------------
    # hashcash.py: Hashcash implementation
    # ----------------------------------------------

    """
    Hashcash is a "proof of work."

    Example:
    [color=blue][color=green][color=darkred]
    >>> import sha
    >>> sha.new('denmar k2890CF').hexdi gest()[/color][/color][/color]
    '000000cf896433 70c24e413ec0886 ab92bd7f6e8'

    Here we have a 24-bit partial SHA collision against the zero string.

    This proves to us that someone took the prefix 'denmark', and
    tried about 2**24 different suffixes. Thus we know that
    someone has burnt around 2**24 CPU cycles on the prefix string
    'denmark'. Usually, 'denmark' will be a unique challenge string,
    so old hashcash cannot be recycled.

    For speed, this library takes the hash of the string 'denmark' before
    doing the collision with the zero string. Otherwise, it is identical
    to the above example.

    Library examples:
    [color=blue][color=green][color=darkred]
    >>> import hashcash
    >>> hashcash.make_t oken('Denmark', 22)[/color][/color][/color]
    '59538D'[color=blue][color=green][color=darkred]
    >>> hashcash.verify _token('Denmark ', '59538D')[/color][/color][/color]
    22[color=blue][color=green][color=darkred]
    >>> t = hashcash.make_c luster('Denmark ', 18)
    >>> t[/color][/color][/color]
    'BC48-D5A1-F8C2-27F0-9CC0-DD31-2F04-2052-835-FFF1-E319-0E91-A9D0-D359
    -E996-70BA'[color=blue][color=green][color=darkred]
    >>> hashcash.verify _cluster('Denma rk', t)[/color][/color][/color]
    18

    Note that make_token() takes wildly varying amounts of CPU time.
    The make_cluster() function concatenates 16 hashcash tokens to even
    out the amount of CPU time spent.

    """

    import sha, math, itertools

    def trailing_zeros( n):
    """Number of trailing 0s in binary representation of integer n."""
    if n <= 0: return 0
    for i in itertools.count (0):
    if n & (1<<i): return i

    def irange(n):
    """Implementati on of xrange(n) that does not overflow."""
    i = 0
    while i < n:
    yield i; i += 1

    def all_strings(cha rset='012345678 9ABCDEF'):
    """Yields all strings in given character set, sorted by length."""
    m = len(charset)
    for n in itertools.count (0):
    for i in irange(m**n):
    yield ''.join([charset[(i//(m**j))%m] for j in xrange(n)])

    def hash(s):
    """Hash function used by hashcash. Returns an integer."""
    return int(sha.new(s). hexdigest(), 16)

    def make_token(s, n, charset='012345 6789ABCDEF'):
    """Makes hashcash token of value 'n' against basestring 's'."""
    h = sha.new(s).dige st()
    for token in all_strings(cha rset):
    if trailing_zeros( hash(h + token)) >= n: return token

    def verify_token(s, token):
    """Returns hashcash value of given token against basestring 's'."""
    return trailing_zeros( hash(sha.new(s) .digest() + token))

    def make_cluster(s, n, charset='012345 6789ABCDEF'):
    """Makes hashcash cluster of value 'n' against basestring 's'."""
    return '-'.join([make_token(s+st r(i),n-4,charset) for i in range(16)])

    def verify_cluster( s, token):
    """Hashcash value of the given cluster against basestring 's'."""
    T = token.split('-')
    return min([verify_token(s+ str(i), T[i]) for i in range(len(T))])+\
    int(math.log(le n(T)) / math.log(2.0))


    This document is in public domain (as are all of my past Usenet postings).

    - Connelly
  • Scott David Daniels

    #2
    Re: Simple hashcash implementation

    barnesc@engr.or st.edu wrote:
    [color=blue]
    > Here's a simple hashcash implementation in Python.
    > 28 lines of actual code....[/color]

    Looks like you've spent a bit of time on this. If you do think it
    is generally useful, why not put it in the Python Cookbook?
    <http://aspn.activestat e.com/ASPN/Python/Cookbook/>

    -Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    Working...