Search code examples
pythonarraysnacl-cryptography

PyNaCl - Printing PrivateKey raw hex value


I am currently experimenting with elliptic curves and using python for simplicity.

I have an instance of this class (NaCl/PrivateKey) which is properly instantiated.

However looking at it's public class variables, I can only seem to query the size.

Is it my misunderstanding, is there a public variable I can query to fetch the underlying private key data (again I'm just playing and learning, not for production).

I've been able to print the privateKey instance and it prints a direct byte array like this: \xa6_\xe5\xa3\xc3\xdd\x96\x04C\x03%\x0f\xe7)y\x92\n\xf7#\xee\xcdo\xff\xaf%\xedZ\xd4\x0e\xecr\xb4

I can then prefix a b in a Python repl with a .toHex call to get:

a65fe5a3c3dd96044303250fe72979920af723eecd6fffaf25ed5ad40eec72b4

Is there any Python ninja that can guide me on a more direct access from the instance directly?

Best I can do is print(binascii.hexlify(privateKey.__bytes__())) - but that doesn't seem like an appropriate way to do it, accessing __bytes__ directly


Solution

  • print(bytes(privateKey).hex()) is the answer. The __bytes__ method is a magic method that is called when an object is converted to bytes. bytes objects have a method hex to convert them.