DESCRIPTION.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ========================
  2. hashids for Python 2.6–3
  3. ========================
  4. 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/
  5. Compatibility
  6. =============
  7. hashids is tested with python 2.6, 2.7, 3.2, 3.3 and 3.4. PyPy and PyPy 3 work as well.
  8. .. image:: https://travis-ci.org/davidaurelio/hashids-python.svg?branch=master
  9. :target: https://travis-ci.org/davidaurelio/hashids-python
  10. Compatibility with the JavaScript implementation
  11. ------------------------------------------------
  12. ================== ==============
  13. hashids/JavaScript hashids/Python
  14. ------------------ --------------
  15. v0.1.x v0.8.x
  16. v0.3.x and v1.0.x v1.0.2+
  17. ================== ==============
  18. 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.
  19. Installation
  20. ============
  21. Install the module from PyPI, e. g. with pip:
  22. .. code:: bash
  23. pip install hashids
  24. pip install hashids==0.8.4 # for compatibility with hashids.js 0.1.x
  25. Run the tests
  26. =============
  27. The tests are written with `pytest <http://pytest.org/latest/>`_. The pytest module has to be installed.
  28. .. code:: bash
  29. python -m pytest
  30. Usage
  31. =====
  32. Import the constructor from the ``hashids`` module:
  33. .. code:: python
  34. from hashids import Hashids
  35. hashids = Hashids()
  36. Basic Usage
  37. -----------
  38. Encode a single integer:
  39. .. code:: python
  40. hashid = hashids.encode(123) # 'Mj3'
  41. Decode a hash:
  42. .. code:: python
  43. ints = hashids.decode('xoz') # (456,)
  44. To encode several integers, pass them all at once:
  45. .. code:: python
  46. hashid = hashids.encode(123, 456, 789) # 'El3fkRIo3'
  47. Decoding is done the same way:
  48. .. code:: python
  49. ints = hashids.decode('1B8UvJfXm') # (517, 729, 185)
  50. Using A Custom Salt
  51. -------------------
  52. 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.
  53. .. code:: python
  54. hashids = Hashids(salt='this is my salt 1')
  55. hashid = hashids.encode(123) # 'nVB'
  56. The generated hash changes whenever the salt is changed:
  57. .. code:: python
  58. hashids = Hashids(salt='this is my salt 2')
  59. hashid = hashids.encode(123) # 'ojK'
  60. A salt string between 6 and 32 characters provides decent randomization.
  61. Controlling Hash Length
  62. -----------------------
  63. 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.
  64. This is done by passing the minimum hash length to the constructor. Hashes are padded with extra characters to make them seem longer.
  65. .. code:: python
  66. hashids = Hashids(min_length=16)
  67. hashid = hashids.encode(1) # '4q2VolejRejNmGQB'
  68. Using A Custom Alphabet
  69. -----------------------
  70. It’s possible to set a custom alphabet for your hashes. The default alphabet is ``'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'``.
  71. To have only lowercase letters in your hashes, pass in the following custom alphabet:
  72. .. code:: python
  73. hashids = Hashids(alphabet='abcdefghijklmnopqrstuvwxyz')
  74. hashid = hashids.encode(123456789) # 'kekmyzyk'
  75. A custom alphabet must contain at least 16 characters.
  76. Randomness
  77. ==========
  78. 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:
  79. Repeating numbers
  80. -----------------
  81. There are no repeating patterns that might show that there are 4 identical numbers in the hash:
  82. .. code:: python
  83. hashids = Hashids("this is my salt")
  84. hashids.encode(5, 5, 5, 5) # '1Wc8cwcE'
  85. The same is valid for incremented numbers:
  86. .. code:: python
  87. hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # 'kRHnurhptKcjIDTWC3sx'
  88. hashids.encode(1) # 'NV'
  89. hashids.encode(2) # '6m'
  90. hashids.encode(3) # 'yD'
  91. hashids.encode(4) # '2l'
  92. hashids.encode(5) # 'rD'
  93. Curses! #$%@
  94. ============
  95. 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.
  96. 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.**
  97. License
  98. =======
  99. MIT license, see the LICENSE file. You can use hashids in open source projects and commercial products.