title: How to sign and verify signature with ecdsa in python
body: <p>I need to sign a hash of 256 bits with ECDSA using a private key of 256 bits, just as bitcoin does, and I am reaching desperation because of the lack of documentation of ecdsa in python.</p>
  
  <p>I found a lot of codes on the internet, but there was nothing as easy as just <code>ecdsa.sign(msg, privkey)</code> or similar, everything I found is a lot of code of mathematical stuff I don't understand, but yet they use the ecdsa library (I don't know why they wouldn't add a signing function in a library that is going to be used to sign stuff, instead a page of code is needed when using the library?).</p>
  
  <p>This is the best code I found so far:</p>
  
  <pre><code>def ecdsa_sign(val, secret_exponent):
      """Return a signature for the provided hash, using the provided
      random nonce. It is absolutely vital that random_k be an unpredictable
      number in the range [1, self.public_key.point.order()-1].  If
      an attacker can guess random_k, he can compute our private key from a
      single signature. Also, if an attacker knows a few high-order
      bits (or a few low-order bits) of random_k, he can compute our private
      key from many signatures. The generation of nonces with adequate
      cryptographic strength is very difficult and far beyond the scope
      of this comment.
  
      May raise RuntimeError, in which case retrying with a new
      random value k is in order.
      """
      G = ecdsa.SECP256k1
      n = G.order()
      k = deterministic_generate_k(n, secret_exponent, val)
      p1 = k * G
      r = p1.x()
      if r == 0: raise RuntimeError("amazingly unlucky random number r")
      s = ( ecdsa.numbertheory.inverse_mod( k, n ) * ( val + ( secret_exponent * r ) % n ) ) % n
      if s == 0: raise RuntimeError("amazingly unlucky random number s")
  
      return signature_to_der(r, s)
  
  def deterministic_generate_k(generator_order, secret_exponent, val, hash_f=hashlib.sha256):
      """
      Generate K value according to https://tools.ietf.org/html/rfc6979
      """
      n = generator_order
      order_size = (bit_length(n) + 7) // 8
      hash_size = hash_f().digest_size
      v = b'\x01' * hash_size
      k = b'\x00' * hash_size
      priv = intbytes.to_bytes(secret_exponent, length=order_size)
      shift = 8 * hash_size - bit_length(n)
      if shift &gt; 0:
          val &gt;&gt;= shift
      if val &gt; n:
          val -= n
      h1 = intbytes.to_bytes(val, length=order_size)
      k = hmac.new(k, v + b'\x00' + priv + h1, hash_f).digest()
      v = hmac.new(k, v, hash_f).digest()
      k = hmac.new(k, v + b'\x01' + priv + h1, hash_f).digest()
      v = hmac.new(k, v, hash_f).digest()
  
      while 1:
          t = bytearray()
  
          while len(t) &lt; order_size:
              v = hmac.new(k, v, hash_f).digest()
              t.extend(v)
  
          k1 = intbytes.from_bytes(bytes(t))
  
          k1 &gt;&gt;= (len(t)*8 - bit_length(n))
          if k1 &gt;= 1 and k1 &lt; n:
              return k1
  
          k = hmac.new(k, v + b'\x00', hash_f).digest()
          v = hmac.new(k, v, hash_f).digest()
  </code></pre>
  
  <p>But I just can't trust a code like that because I have no idea what it does. Also, the comments in ecdsa_sign says that returns a signature given the value, the secret exponent, <strong>and a nonce</strong>. It says its very important to have a nonce, but I just can't figure out where that nonce is.</p>
  
  <p>Is there any simple, one-line way to sign and verify ECDSA signatures using whatever trusted library in python on windows?</p>