Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
max-l opened this issue Oct 5, 2020 · 1 comment

Comments

@max-l
Copy link

max-l commented Oct 5, 2020

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
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants