File: sign.rst

package info (click to toggle)
python-libnacl 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488 kB
  • sloc: python: 2,634; makefile: 149; sh: 3
file content (43 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
==============================
Signing and Verifying Messages
==============================

The nacl libs have the capability to sign and verify messages. Please be
advised that public key encrypted messages do not need to be signed, the
nacl box construct verifies the validity of the sender.

To sign and verify messages use the Signer and Verifier classes:

.. code-block:: python

    import libnacl.sign

    msg = (b'Well, that\'s no ordinary rabbit.  That\'s the most foul, '
           b'cruel, and bad-tempered rodent you ever set eyes on.')
    # Create a Signer Object, if the key seed value is not passed in the
    # signing keys will be automatically generated
    signer = libnacl.sign.Signer()
    # Sign the message, the signed string is the message itself plus the
    # signature
    signed = signer.sign(msg)
    # If only the signature is desired without the message:
    signature = signer.signature(msg)
    # To create a verifier pass in the verify key:
    veri = libnacl.sign.Verifier(signer.hex_vk())
    # Verify the message!
    verified = veri.verify(signed)
    verified2 = veri.verify(signature + msg)

Saving Keys to Disk
===================

All libnacl key objects can be safely saved to disk via the save method. This
method changes the umask before saving the key file to ensure that the saved
file can only be read by the user creating it and cannot be written to.

.. code-block:: python

    import libnacl.sign

    signer = libnacl.sign.Signer()
    signer.save('/etc/nacl/signer.key')