⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashcash-task.py

📁 idel虚拟机源码
💻 PY
📖 第 1 页 / 共 2 页
字号:
"""An example task.  Meant only as a demo and for rough performancecomparison.  In other words, it's buggy!  I suck.See http://www.dcs.ex.ac.uk/~aba/hashcash/"""import dealerclass HashcashTask:    """Use borednet to mint hashcash.  The search for collisions is    split up into fixed-size chunks."""        def __init__(self, string_to_collide, bits):        self.n = string_to_collide	self.bits = bits        self.lo = 0L        self.chunk_size = 2L**bits    def get_job(self, jobber_info):	"""Return a new job that will contribute to completing the	task.  jobber_info is an object supplied by the jobber saying	what it claims to be able to do.  If there are no more jobs	to issue, return None."""	new_lo = self.lo + self.chunk_size        job = HashcashJob(self.n, self.bits, self.lo, new_lo)        self.lo = new_lo        return job    def take_answer(self, job):	"""Called when a job has completed."""        if job.answer:            print 'New hashcash: %s' % (job.answer)    def take_failure(self, job, host, reason):	"""Called when a host tried but failed to run a job created 	by this task.  What's in `reason' is currently unspecified."""        pass    def clean_up(self):	"""Called when all jobs have completed and there are no more	jobs to issue."""        passclass HashcashJob:    """A job searching for a hashcash collision in a range."""    def __init__(self, str, bits, lo, hi):	"""bits must be >= 8"""        self.n = str	self.bits = bits        self.lo = lo        self.hi = hi    def push_code(self, session):	"""Push onto session an idel object file that will compute 	this job's result."""        source_code = """ints: H0 0 0 0 0;intsints: W0 0 0 00 0 0 00 0 0 00 0 0 0;intsints: hbits  0  ;intsints: lbits  0  ;intsints: SHA1-IV0x674523010xEFCDAB890x98BADCFE0x103254760xC3D2E1F0;intsbss-data: collision 100string: target "%s"bss-data: target-digest 20bss-data: try 100def 0 1 main  %d %d find-collision if    try  target.size 16 +  type  '\\n' emit  then  0 ;def 2 1 find-collision  { tries trial --    try target.addr target.size memcpy    try target.size +  7  trial  sprintf-hex    8  7 and    { partial-byte --      %d 3 >>>       1 %d partial-byte - <<  1 -  -1 xor      { partial-byte-index partial-byte-mask --        partial-byte-index  partial-byte if 1 + then        { check-bytes --          target-digest target.addr target.size digest          partial-byte if            target-digest partial-byte-index +            { a -- a c@  partial-byte-mask and  a c! }          then          try target.size + 8 +  7  0  sprintf-hex          target.size 16 +          tries 8 + 4 >>>          { try-strlen tries2 --            partial-byte partial-byte-index partial-byte-mask check-bytes            try-strlen tries2 trial 0 outer-loop } } } } } ;bss-data: precomputed-ctx 92def 8 1 outer-loop  { try-strlen tries2 trial i --    i tries2 < if      SHA1-init      try target.size + 8 +  7  trial  sprintf-hex      try  try-strlen 1 -  SHA1-update      precomputed-ctx H 92 wordcpy      try-strlen tries2 trial i 0 inner-loop    else      { partial-byte partial-byte-index partial-byte-mask check-bytes -- 0 }    then } ;bss-data: last-char 4string: hex "0123456789abcdef"def 9 1 inner-loop  { j --    j 16 < if      H precomputed-ctx 92 wordcpy      hex.addr j + c@  last-char c!      last-char 1 SHA1-update      SHA1-final      H c@  target-digest c@ - if        j 1 +  inner-loop      else        { partial-byte partial-byte-index partial-byte-mask check-bytes          try-strlen tries2 trial i --          partial-byte if            H partial-byte-index +             { a -- a c@  partial-byte-mask and  a c! }          then          target-digest H check-bytes memcmp if            partial-byte partial-byte-index partial-byte-mask check-bytes            try-strlen tries2 trial i  j 1 +  inner-loop          else            hex.addr j + c@  try try-strlen + 1 - c!            collision try try-strlen memcpy            i 4 << j + 1 +          then }      then    else      { trial i --        trial 16 +  i 1 +  outer-loop }    then } ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -