Skip to content

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

@max-l

Description

@max-l

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)



Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions