File: signer.rst

package info (click to toggle)
python-itsdangerous 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: python: 1,055; makefile: 21; sh: 9
file content (49 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
.. module:: itsdangerous.signer

Signing Interface
=================

The most basic interface is the signing interface. The :class:`Signer`
class can be used to attach a signature to a specific string:

.. code-block:: python

    from itsdangerous import Signer
    s = Signer("secret-key")
    s.sign("my string")
    b'my string.wh6tMHxLgJqB6oY1uT73iMlyrOA'

The signature is appended to the string, separated by a dot. To validate
the string, use the :meth:`~Signer.unsign` method:

.. code-block:: python

    s.unsign(b"my string.wh6tMHxLgJqB6oY1uT73iMlyrOA")
    b'my string'

If unicode strings are provided, an implicit encoding to UTF-8 happens.
However after unsigning you won't be able to tell if it was unicode or
a bytestring.

If the value is changed, the signature will no longer match, and
unsigning will raise a :exc:`~itsdangerous.exc.BadSignature` exception:

.. code-block:: python

    s.unsign(b"different string.wh6tMHxLgJqB6oY1uT73iMlyrOA")
    Traceback (most recent call last):
      ...
    BadSignature: Signature does not match

To record and validate the age of a signature, see :doc:`/timed`.

.. autoclass:: Signer
    :members:


Signing Algorithms
------------------

.. autoclass:: NoneAlgorithm

.. autoclass:: HMACAlgorithm