Advertisement
Guest User

sign_hash rfc6979

a guest
Dec 2nd, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. def sign_hash(hash,privkey):
  2.     try:
  3.         import ecdsa
  4.         k_value = int(ecdsa.rfc6979.generate_k(ecdsa.ecdsa.ellipticcurve.Point(ecdsa.ecdsa.curve_secp256k1,ecdsa.ecdsa._Gx,ecdsa.ecdsa._Gy,ecdsa.ecdsa._r),int(privkey,16),hashlib.sha256,hash))
  5.     except:
  6.         k_value = int(double_sha256(os.urandom(32)),16)
  7.     r = int(str(privkey_to_pubkey(k_value,output_compressed=True))[2:],16) % N_ORDER
  8.     r = str(hex(r)).replace("0x","").replace("L","").zfill(64)
  9.     assert len(r) == 64
  10.     s = ((int(hash,16) + (int(r,16) * int(privkey,16))) * (ec_modular_inverse(k_value,N_ORDER))) % N_ORDER
  11.     s = str(hex(s)).replace("0x","").replace("L","").zfill(64)
  12.     assert len(s) == 64
  13.     if int(r[:2],16) > 127:
  14.         r = str(str("00") + str(r))
  15.         assert len(r) == 66
  16.     if int(s[:2],16) > 127:
  17.         s = str(str("00") + str(s))
  18.         assert len(s) == 66
  19.     if len(r) == 66:
  20.         r_prefix = str("0221")
  21.     else:
  22.         r_prefix = str("0220")
  23.     if len(s) == 66:
  24.         s_prefix = str("0221")
  25.     else:
  26.         s_prefix = str("0220")
  27.     finalsig = r_prefix + r + s_prefix + s
  28.     len_byte = str(hex(int(len(finalsig) // 2))).replace("0x","").replace("L","").zfill(2)
  29.     assert len(len_byte) == 2
  30.     finalsig = str("30") + len_byte + finalsig  # Does NOT include 0x01 SIGHASH_ALL postfix
  31.     return str(finalsig)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement