コード例 #1
0
def sign(msg, d, curve=P256, hashfunc=sha256):
    """Sign a message using the elliptic curve digital signature algorithm.

    The elliptic curve signature algorithm is described in full in FIPS 186-4 Section 6. Please
    refer to http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf for more information.

    Args:
        |  msg (str|bytes|bytearray): A message to be signed.
        |  d (long): The ECDSA private key of the signer.
        |  curve (fastecdsa.curve.Curve): The curve to be used to sign the message.
        |  hashfunc (_hashlib.HASH): The hash function used to compress the message.
    """
    # generate a deterministic nonce per RFC6979
    rfc6979 = RFC6979(msg, d, curve.q, hashfunc)
    k = rfc6979.gen_nonce()

    hashed = hashfunc(msg_bytes(msg)).hexdigest()
    r, s = _ecdsa.sign(
        hashed,
        str(d),
        str(k),
        str(curve.p),
        str(curve.a),
        str(curve.b),
        str(curve.q),
        str(curve.gx),
        str(curve.gy)
    )
    return (int(r), int(s))
コード例 #2
0
ファイル: keys.py プロジェクト: testimx62/blockstack-core
def sign_digest(digest_hex,
                privkey_hex,
                curve=fastecdsa.curve.secp256k1,
                hashfunc=hashlib.sha256):
    """
    Sign a digest with ECDSA
    Return base64 signature
    """
    pk_i = decode_privkey_hex(str(privkey_hex))

    # generate a deterministic nonce per RFC6979
    rfc6979 = RFC6979_blockstack(pk_i, curve.q, hashfunc)
    k = rfc6979.gen_nonce_from_digest(digest_hex.decode('hex'))

    r, s = _ecdsa.sign(digest_hex, str(pk_i), str(k), curve.name)
    return encode_signature(int(r), int(s))
コード例 #3
0
def sign(msg: MsgTypes, d: int, curve: Curve = P256, hashfunc=sha256, prehashed: bool = False):
    """Sign a message using the elliptic curve digital signature algorithm.

    The elliptic curve signature algorithm is described in full in FIPS 186-4 Section 6. Please
    refer to http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf for more information.

    Args:
        |  msg (str|bytes|bytearray): A message to be signed.
        |  d (int): The ECDSA private key of the signer.
        |  curve (fastecdsa.curve.Curve): The curve to be used to sign the message.
        |  hashfunc (_hashlib.HASH): The hash function used to compress the message.
        |  prehashed (bool): The message being passed has already been hashed by :code:`hashfunc`.
    """
    # generate a deterministic nonce per RFC6979
    rfc6979 = RFC6979(msg, d, curve.q, hashfunc, prehashed=prehashed)
    k = rfc6979.gen_nonce()

    # Fix the bit-length of the random nonce,
    # so that it doesn't leak via timing.
    # This does not change that ks (mod n) = kt (mod n) = k (mod n)
    ks = k + curve.q
    kt = ks + curve.q
    if ks.bit_length() == curve.q.bit_length():
        k = kt
    else:
        k = ks

    if prehashed:
        hex_digest = hexlify(msg).decode()
    else:
        hex_digest = hashfunc(msg_bytes(msg)).hexdigest()

    r, s = _ecdsa.sign(
        hex_digest,
        str(d),
        str(k),
        str(curve.p),
        str(curve.a),
        str(curve.b),
        str(curve.q),
        str(curve.gx),
        str(curve.gy)
    )
    return int(r), int(s)
コード例 #4
0
def sign(msg, d, curve=P256, hashfunc=sha256):
    """Sign a message using the elliptic curve digital signature algorithm.

    The elliptic curve signature algorithm is described in full in FIPS 186-4 Section 6. Please
    refer to http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf for more information.

    Args:
        |  msg (str): A message to be signed.
        |  d (long): The ECDSA private key of the signer.
        |  curve (fastecdsa.curve.Curve): The curve to be used to sign the message.
        |  hashfunc (_hashlib.HASH): The hash function used to compress the message.
    """
    # generate a deterministic nonce per RFC6979
    #rfc6979 = RFC6979(msg, d, curve.q, hashfunc)
    #k = rfc6979.gen_nonce()
    #print format(k, '02x')

    #hashed = hashfunc(msg.encode()).hexdigest()
    k = 0xAF96F7CF8932223963ACEF6CFE675E0661E58B80D62CEF00C
    hashed = str(0x0d80b4919e2edb5e1ba3e619e8b60bfad25d16c4deec05a7)
    r, s = _ecdsa.sign(hashed, str(d), str(k), curve.name)
    return (int(r), int(s))