File: README.rst

package info (click to toggle)
python-hashids 1.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 124 kB
  • sloc: python: 443; makefile: 4
file content (166 lines) | stat: -rw-r--r-- 4,702 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
==========================
hashids for Python 2.7 & 3
==========================

A python port of the JavaScript *hashids* implementation. It generates YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user. Website: http://www.hashids.org/

Compatibility
=============

hashids is tested with python 2.7 and 3.5–3.8. PyPy and PyPy 3 work as well.

.. image:: https://travis-ci.org/davidaurelio/hashids-python.svg?branch=master
    :target: https://travis-ci.org/davidaurelio/hashids-python

Compatibility with the JavaScript implementation
------------------------------------------------

==================   ==============
hashids/JavaScript   hashids/Python
------------------   --------------
v0.1.x               v0.8.x
v0.3.x+              v1.0.2+
==================   ==============

The JavaScript implementation produces different hashes in versions 0.1.x and 0.3.x. For compatibility with the older 0.1.x version install hashids 0.8.4 from pip, otherwise the newest hashids.


Installation
============

Install the module from PyPI, e. g. with pip:

.. code:: bash

  pip install hashids
  pip install hashids==0.8.4 # for compatibility with hashids.js 0.1.x

Run the tests
=============

The tests are written with `pytest <http://pytest.org/latest/>`_. The pytest module has to be installed.

.. code:: bash

  python -m pytest

Usage
=====

Import the constructor from the ``hashids`` module:

.. code:: python

  from hashids import Hashids
  hashids = Hashids()

Basic Usage
-----------

Encode a single integer:

.. code:: python

  hashid = hashids.encode(123) # 'Mj3'

Decode a hash:

.. code:: python

  ints = hashids.decode('xoz') # (456,)

To encode several integers, pass them all at once:

.. code:: python

  hashid = hashids.encode(123, 456, 789) # 'El3fkRIo3'

Decoding is done the same way:

.. code:: python

  ints = hashids.decode('1B8UvJfXm') # (517, 729, 185)

Using A Custom Salt
-------------------

Hashids supports salting hashes by accepting a salt value. If you don’t want others to decode your hashes, provide a unique string to the constructor.

.. code:: python

  hashids = Hashids(salt='this is my salt 1')
  hashid = hashids.encode(123) # 'nVB'

The generated hash changes whenever the salt is changed:

.. code:: python

  hashids = Hashids(salt='this is my salt 2')
  hashid = hashids.encode(123) # 'ojK'

A salt string between 6 and 32 characters provides decent randomization.

Controlling Hash Length
-----------------------

By default, hashes are going to be the shortest possible. One reason you might want to increase the hash length is to obfuscate how large the integer behind the hash is.

This is done by passing the minimum hash length to the constructor. Hashes are padded with extra characters to make them seem longer.

.. code:: python

  hashids = Hashids(min_length=16)
  hashid = hashids.encode(1) # '4q2VolejRejNmGQB'

Using A Custom Alphabet
-----------------------

It’s possible to set a custom alphabet for your hashes. The default alphabet is ``'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'``.

To have only lowercase letters in your hashes, pass in the following custom alphabet:

.. code:: python

  hashids = Hashids(alphabet='abcdefghijklmnopqrstuvwxyz')
  hashid = hashids.encode(123456789) # 'kekmyzyk'

A custom alphabet must contain at least 16 characters.

Randomness
==========

The primary purpose of hashids is to obfuscate ids. It's not meant or tested to be used for security purposes or compression. Having said that, this algorithm does try to make these hashes unguessable and unpredictable:

Repeating numbers
-----------------

There are no repeating patterns that might show that there are 4 identical numbers in the hash:

.. code:: python

  hashids = Hashids("this is my salt")
  hashids.encode(5, 5, 5, 5) # '1Wc8cwcE'

The same is valid for incremented numbers:

.. code:: python

  hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # 'kRHnurhptKcjIDTWC3sx'

  hashids.encode(1) # 'NV'
  hashids.encode(2) # '6m'
  hashids.encode(3) # 'yD'
  hashids.encode(4) # '2l'
  hashids.encode(5) # 'rD'

Curses! #$%@
============

This code was written with the intent of placing generated hashes in visible places – like the URL.  Which makes it unfortunate if generated hashes accidentally formed a bad word.

Therefore, the algorithm tries to avoid generating most common English curse words by never placing the following letters next to each other: **c, C, s, S, f, F, h, H, u, U, i, I, t, T.**

License
=======

MIT license, see the LICENSE file. You can use hashids in open source projects and commercial products.