hashids


Namehashids JSON
Version 1.3.1 PyPI version JSON
download
home_pagehttps://hashids.org/python/
SummaryImplements the hashids algorithm in python. For more information, visit http://hashids.org/
upload_time2020-07-26 21:47:24
maintainer
docs_urlNone
authorDavid Aurelio
requires_python>=2.7
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ==========================
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.


            

Raw data

            {
    "_id": null,
    "home_page": "https://hashids.org/python/",
    "name": "hashids",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "David Aurelio",
    "author_email": "dev@david-aurelio.com",
    "download_url": "https://files.pythonhosted.org/packages/26/a2/6f38b1de47b41ad0420047ad03b0b8cd5d6572271a3e9c2ab70e373fe63a/hashids-1.3.1.tar.gz",
    "platform": "",
    "description": "==========================\nhashids for Python 2.7 & 3\n==========================\n\nA 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/\n\nCompatibility\n=============\n\nhashids is tested with python 2.7 and 3.5\u20133.8. PyPy and PyPy 3 work as well.\n\n.. image:: https://travis-ci.org/davidaurelio/hashids-python.svg?branch=master\n    :target: https://travis-ci.org/davidaurelio/hashids-python\n\nCompatibility with the JavaScript implementation\n------------------------------------------------\n\n==================   ==============\nhashids/JavaScript   hashids/Python\n------------------   --------------\nv0.1.x               v0.8.x\nv0.3.x+              v1.0.2+\n==================   ==============\n\nThe 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.\n\n\nInstallation\n============\n\nInstall the module from PyPI, e.\u00a0g. with pip:\n\n.. code:: bash\n\n  pip install hashids\n  pip install hashids==0.8.4 # for compatibility with hashids.js 0.1.x\n\nRun the tests\n=============\n\nThe tests are written with `pytest <http://pytest.org/latest/>`_. The pytest module has to be installed.\n\n.. code:: bash\n\n  python -m pytest\n\nUsage\n=====\n\nImport the constructor from the ``hashids`` module:\n\n.. code:: python\n\n  from hashids import Hashids\n  hashids = Hashids()\n\nBasic Usage\n-----------\n\nEncode a single integer:\n\n.. code:: python\n\n  hashid = hashids.encode(123) # 'Mj3'\n\nDecode a hash:\n\n.. code:: python\n\n  ints = hashids.decode('xoz') # (456,)\n\nTo encode several integers, pass them all at once:\n\n.. code:: python\n\n  hashid = hashids.encode(123, 456, 789) # 'El3fkRIo3'\n\nDecoding is done the same way:\n\n.. code:: python\n\n  ints = hashids.decode('1B8UvJfXm') # (517, 729, 185)\n\nUsing A Custom Salt\n-------------------\n\nHashids supports salting hashes by accepting a salt value. If you don\u2019t want others to decode your hashes, provide a unique string to the constructor.\n\n.. code:: python\n\n  hashids = Hashids(salt='this is my salt 1')\n  hashid = hashids.encode(123) # 'nVB'\n\nThe generated hash changes whenever the salt is changed:\n\n.. code:: python\n\n  hashids = Hashids(salt='this is my salt 2')\n  hashid = hashids.encode(123) # 'ojK'\n\nA salt string between 6 and 32 characters provides decent randomization.\n\nControlling Hash Length\n-----------------------\n\nBy 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.\n\nThis is done by passing the minimum hash length to the constructor. Hashes are padded with extra characters to make them seem longer.\n\n.. code:: python\n\n  hashids = Hashids(min_length=16)\n  hashid = hashids.encode(1) # '4q2VolejRejNmGQB'\n\nUsing A Custom Alphabet\n-----------------------\n\nIt\u2019s possible to set a custom alphabet for your hashes. The default alphabet is ``'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'``.\n\nTo have only lowercase letters in your hashes, pass in the following custom alphabet:\n\n.. code:: python\n\n  hashids = Hashids(alphabet='abcdefghijklmnopqrstuvwxyz')\n  hashid = hashids.encode(123456789) # 'kekmyzyk'\n\nA custom alphabet must contain at least 16 characters.\n\nRandomness\n==========\n\nThe 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:\n\nRepeating numbers\n-----------------\n\nThere are no repeating patterns that might show that there are 4 identical numbers in the hash:\n\n.. code:: python\n\n  hashids = Hashids(\"this is my salt\")\n  hashids.encode(5, 5, 5, 5) # '1Wc8cwcE'\n\nThe same is valid for incremented numbers:\n\n.. code:: python\n\n  hashids.encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # 'kRHnurhptKcjIDTWC3sx'\n\n  hashids.encode(1) # 'NV'\n  hashids.encode(2) # '6m'\n  hashids.encode(3) # 'yD'\n  hashids.encode(4) # '2l'\n  hashids.encode(5) # 'rD'\n\nCurses! #$%@\n============\n\nThis code was written with the intent of placing generated hashes in visible places \u2013 like the URL.  Which makes it unfortunate if generated hashes accidentally formed a bad word.\n\nTherefore, 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.**\n\nLicense\n=======\n\nMIT license, see the LICENSE file. You can use hashids in open source projects and commercial products.\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Implements the hashids algorithm in python. For more information, visit http://hashids.org/",
    "version": "1.3.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "1378a93063da3fd6fe87f9eb435c53ce",
                "sha256": "8bddd1acba501bfc9306e7e5a99a1667f4f2cacdc20cbd70bcc5ddfa5147c94c"
            },
            "downloads": -1,
            "filename": "hashids-1.3.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1378a93063da3fd6fe87f9eb435c53ce",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=2.7",
            "size": 6556,
            "upload_time": "2020-07-26T21:47:22",
            "upload_time_iso_8601": "2020-07-26T21:47:22.410777Z",
            "url": "https://files.pythonhosted.org/packages/6e/46/ffdf25b1f6dbb1ce588ccb818e983df9e3d30594679f5a08c865a59cead7/hashids-1.3.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "f5fc57027f1b9912b8917db5bc188217",
                "sha256": "6c3dc775e65efc2ce2c157a65acb776d634cb814598f406469abef00ae3f635c"
            },
            "downloads": -1,
            "filename": "hashids-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "f5fc57027f1b9912b8917db5bc188217",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7",
            "size": 7884,
            "upload_time": "2020-07-26T21:47:24",
            "upload_time_iso_8601": "2020-07-26T21:47:24.138778Z",
            "url": "https://files.pythonhosted.org/packages/26/a2/6f38b1de47b41ad0420047ad03b0b8cd5d6572271a3e9c2ab70e373fe63a/hashids-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-07-26 21:47:24",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "hashids"
}
        
Elapsed time: 0.02917s