SHA256不会产生相同的结果

时间:2017-08-14 17:39:09

标签: python bitcoin sha256 sha

我关注this教程和第2部分(下图),它显示SHA256产生的结果与我运行python代码时产生的结果不同:

字符串是:0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6

教程SHA256来到:600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408

我的短蟒蛇秀:

sha_result = sha256(bitconin_addresss).hexdigest().upper()
print sha_result

32511E82D56DCEA68EB774094E25BAB0F8BDD9BC1ECA1CEEDA38C7A43ACEDDCE

实际上,任何online sha256都显示相同的python结果;我在这里错过了什么吗?

enter image description here

2 个答案:

答案 0 :(得分:4)

当您应该对该字符串表示的字节进行哈希处理时,您将对字符串进行哈希处理。

>>> hashlib.sha256('0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'.decode('hex')).hexdigest().upper()
'600FFE422B4E00731A59557A5CCA46CC183944191006324A447BDB2D98D4B408'

答案 1 :(得分:0)

你可以使用Gavin的“base58.py”,我相信他不再在他的github页面上分享它。但是你可能很容易谷歌并从github找到它的不同版本。

这是我编辑的一个版本:

#!/usr/bin/env python

"""encode/decode base58 in the same way that Bitcoin does"""

import math
import sys

__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__b58base = len(__b58chars)

def b58encode(v):
  """ encode v, which is a string of bytes, to base58.
  """

  long_value = 0L
  for (i, c) in enumerate(v[::-1]):
    long_value += ord(c) << (8*i) # 2x speedup vs. exponentiation

  result = ''
  while long_value >= __b58base:
    div, mod = divmod(long_value, __b58base)
    result = __b58chars[mod] + result
    long_value = div
  result = __b58chars[long_value] + result

  # Bitcoin does a little leading-zero-compression:
  # leading 0-bytes in the input become leading-1s
  nPad = 0
  for c in v:
    if c == '\0': nPad += 1
    else: break

  return (__b58chars[0]*nPad) + result

def b58decode(v):
  """ decode v into a string of len bytes
  """
  long_value = 0L
  for (i, c) in enumerate(v[::-1]):
    long_value += __b58chars.find(c) * (__b58base**i)

  result = ''
  while long_value >= 256:
    div, mod = divmod(long_value, 256)
    result = chr(mod) + result
    long_value = div
  result = chr(long_value) + result

  nPad = 0
  for c in v:
    if c == __b58chars[0]: nPad += 1
    else: break

  result = chr(0)*nPad + result

  return result

try:
  import hashlib
  hashlib.new('ripemd160')
  have_crypto = True
except ImportError:
  have_crypto = False

def hash_160(public_key):
  if not have_crypto:
    return ''
  h1 = hashlib.sha256(public_key).digest()
  r160 = hashlib.new('ripemd160')
  r160.update(h1)
  h2 = r160.digest()
  return h2

def hash_160_to_bc_address(h160, version="\x00"):
  if not have_crypto:
    return ''
  vh160 = version+h160
  h3=hashlib.sha256(hashlib.sha256(vh160).digest()).digest()
  addr=vh160+h3[0:4]
  return b58encode(addr)

def public_key_to_bc_address(public_key, version="\x00"):
  if not have_crypto or public_key is None:
    return ''
  h160 = hash_160(public_key)
  return hash_160_to_bc_address(h160, version=version)

def sec_to_bc_key(sec, version="\x80"):
    if not have_crypto or sec is None:
      return ''
    vsec = version+sec +"\x01"
    hvsec=hashlib.sha256(hashlib.sha256(vsec).digest()).digest()
    return b58encode(vsec+hvsec[0:4])

def bc_key_to_sec(prv):
    return b58decode(prv)[1:33]

def bc_address_to_hash_160(addr):
  bytes = b58decode(addr)
  return bytes[1:21]

if __name__ == '__main__':

    if len(sys.argv) > 1:
        if sys.argv[1] == '-en':
            print b58encode(sys.argv[2].decode('hex_codec'))
        if sys.argv[1] == '-de':
            print b58decode(sys.argv[2]).encode('hex_codec')

        if sys.argv[1] == '-pub':
            print public_key_to_bc_address(sys.argv[2].decode('hex_codec'))
        if sys.argv[1] == '-adr':
            print bc_address_to_hash_160(sys.argv[2]).encode('hex_codec')

        if sys.argv[1] == '-sec':
            print sec_to_bc_key(sys.argv[2].decode('hex_codec'))
        if sys.argv[1] == '-prv':
            print bc_key_to_sec(sys.argv[2]).encode('hex_codec')

    else:
        print ''
        print 'Usage: ./base58.py [options]'
        print ''
        print '       -en  converts hex to base58'
        print '       -de  converts base58 to hex'
        print
        print '       -pub public_key_to_bc_address'
        print '       -adr bc_address_to_hash_160'
        print
        print '       -sec sec_to_bc_key'
        print '       -prv bc_key_to_sec'
        print

要回答您的具体问题,请根据以上代码使用此命令:

hashlib.sha256('0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6'.decode('hex_codec')).digest().encode('hex_codec').upper()