Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
  • dev
  • appimage
  • fix_gitlab
  • fixappveyor
  • gitlab
  • fix_ci
  • fix_dbus_error
  • fix_ci_osx
  • sakia020
  • fix_travis#1105
  • feature/backend
  • check_uniq_node_by_endpoints
  • qt5.7
  • feature/agent_architecture
  • translations
  • pyqt5.6
  • qtwebengine
  • pyinstaller
  • landscape
  • 0.53.2
  • 0.53.1
  • 0.53.0
  • 0.52.0
  • 0.51.1
  • 0.51.0
  • 0.50.5
  • 0.50.4
  • 0.50.3
  • 0.50.2
  • 0.50.1
  • 0.50.0
  • 0.33.0rc7
  • 0.33.0rc6
  • 0.33.0rc5
  • 0.33.0rc4
  • 0.33.0rc3
  • 0.33.0rc2
  • 0.33.0rc1
  • 0.32.10post1
40 results

encoding.rst

Blame
  • encoding.rst 1.23 KiB

    Encoders

    PyNaCl supports a simple method of encoding and decoding messages in different formats. Encoders are simple classes with staticmethods that encode/decode and are typically passed as a keyword argument encoder to various methods.

    For example you can generate a signing key and encode it in hex with:

    hex_key = nacl.signing.SigningKey.generate().encode(encoder=nacl.encoding.HexEncoder)

    Then you can later decode it from hex:

    signing_key = nacl.signing.SigningKey(hex_key, encoder=nacl.encoding.HexEncoder)

    Built in Encoders

    Defining your own Encoder

    Defining your own encoder is easy. Each encoder is simply a class with 2 static methods. For example here is the hex encoder:

    import binascii
    
    class HexEncoder(object):
    
        @staticmethod
        def encode(data):
            return binascii.hexlify(data)
    
        @staticmethod
        def decode(data):
            return binascii.unhexlify(data)