pyca / pynacl

Python binding to the Networking and Cryptography (NaCl) library
https://pynacl.readthedocs.io/
Apache License 2.0
1.07k stars 232 forks source link

Error (binascii.Error: Non-hexadecimal digit found) when running example in doc #635

Open max-l opened 4 years ago

max-l commented 4 years ago

I get an encoding/decoding error when running this example without changes:

https://pynacl.readthedocs.io/en/latest/signing/#id1

The version of python is 3.7.4

The stack trace is:


Traceback (most recent call last):
  File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 1448, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/vagrant/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/vagrant/python_src/test_nacl.py", line 29, in <module>
    encoder=HexEncoder)
  File "/g2_venv/lib/python3.7/site-packages/nacl/signing.py", line 110, in verify
    smessage = encoder.decode(smessage)
  File "/g2_venv/lib/python3.7/site-packages/nacl/encoding.py", line 40, in decode
    return binascii.unhexlify(data)
binascii.Error: Non-hexadecimal digit found
python-BaseException

The code is :


from nacl.encoding import HexEncoder
from nacl.signing import SigningKey

# Generate a new random signing key
signing_key = SigningKey.generate()

# Sign a message with the signing key
signed_hex = signing_key.sign(b"Attack at Dawn", encoder=HexEncoder)

# Obtain the verify key for a given signing key
verify_key = signing_key.verify_key

# Serialize the verify key to send it to a third party
verify_key_hex = verify_key.encode(encoder=HexEncoder)

from nacl.encoding import HexEncoder
from nacl.signing import VerifyKey

# Create a VerifyKey object from a hex serialized public key
verify_key = VerifyKey(verify_key_hex, encoder=HexEncoder)

# Check the validity of a message's signature
# The message and the signature can either be passed together, or
# separately if the signature is decoded to raw bytes.
# These are equivalent:
verify_key.verify(signed_hex, encoder=HexEncoder)
signature_bytes = HexEncoder.decode(signed_hex.signature)
verify_key.verify(signed_hex.message, signature_bytes,
                  encoder=HexEncoder)

# Alter the signed message text
forged = signed_hex[:-1] + bytes([int(signed_hex[-1]) ^ 1])
# Will raise nacl.exceptions.BadSignatureError, since the signature check
# is failing
verify_key.verify(forged)
ljluestc commented 1 year ago

from nacl.encoding import HexEncoder from nacl.signing import SigningKey

Generate a new random signing key

signing_key = SigningKey.generate()

Sign a message with the signing key

signed_hex = signing_key.sign(b"Attack at Dawn", encoder=HexEncoder)

Obtain the verify key for a given signing key

verify_key = signing_key.verify_key

Serialize the verify key to send it to a third party

verify_key_hex = verify_key.encode(encoder=HexEncoder)

Create a VerifyKey object from a hex serialized public key

verify_key = VerifyKey(verify_key_hex, encoder=HexEncoder)

Check the validity of a message's signature

Use the hex-encoded signature directly

verify_key.verify(signed_hex.signature, signed_hex.message, encoder=HexEncoder)

Alter the signed message text

forged = signed_hex[:-1] + bytes([int(signed_hex[-1]) ^ 1])

Will raise nacl.exceptions.BadSignatureError, since the signature check is failing

verify_key.verify(forged)