SignedID
Generate a short, opaque, url-safe, unique code whose provenance can be verified statelessly.
Usage
The most basic usage looks like this:
>>> import signed_id
>>> secret = 'shhhh!'
>>> signed_id.generate('my_unique_id', secret)
'e6bed2a1'
>>> signed_id.verify('e6bed2a1', secret)
True
Overview
We need to be able to assign a unique code to a user that has the following properties:
- The code is unique to a user
- The code does not obviously reveal information about a user
- We can verify that we generated the code
- We do not need to store the code anywhere to verify it
- The code is succinct for embedding in SMS messages
- The code is a simple string, safe to pass in URLs
- Valid codes are difficult to guess
How it works
Code generation
Codes are generated with, at minimum, an arbitrary piece of data about an entity and a secret string.
The specific steps are:
- Codes are generated from a unique piece of information about an entity, such as a database ID.
- This information is made opaque--while preserving uniqueness--with a strong hash function.
- The hash is truncated according to generation parameters to make the code more succinct.
- The hashed input is then signed by computing an additional hash with a secret piece of information.
- The signature hash is also truncated for succinctness.
- The two truncated hashes are concatenated, encoded as a hex string, and returned.
Code verification
Verification works on the principle of proof-of-knowledge.
Nominally this means that the secret used for generation must also be known for verification.
In practice, the other generation parameters (hash function and byte lengths) also must be consistent across generation and validation.
The verification steps are:
- Decode the input from hex to a bytestring. If the input is not valid hex, verification fails.
- Parse the bytestring based on the byte length parameters to the input hash and the signature hash.
- Compute the expected signature hash based on the input hash, secret, and provided hash function.
- Truncate the expected signature based on the byte length parameter.
- Assert that the expected truncated signature matches the signature parsed from the code.
Notes
Code length
The code length is customizable, with the caveat that shorter codes have a higher risk for collision and guessability.
Codes contain exactly id_bytes + sig_bytes bytes of information.
Since the code bytes are represented as hex, the code length is 2 * (id_bytes + sig_bytes).
Defaults
By default we use id_bytes=2, sig_bytes=2, so the codes are 8-character ASCII strings.
Collisions
The probability of a collision (non-unique code generation) is 1 / 256^id_bytes.
Brute-forcing
The probability of generating a valid signature for a certain ID is 1 / 256^sig_bytes.
The probability of generating a valid ID completely at random is correspondingly smaller.
Reversing the input hash
The input hash is not inherently secure. For example, if the input ID is a simple database ID, and someone had access to a handful of codes, it would not be difficult to infer the hash function and recover the input with a lookup table. If it's necessary to guard against this, a more complicated (ideally salted) input should be used. For example, instead of a database ID, use a composite input of the database ID and record creation time.
Even if the input hash is reversible, brute-forcing the signature would still often be impractical, as it is effectively salted with the secret.
Release files for signed-id 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| signed_id-0.1.0.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| signed_id-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.2 kB
Release files / signed_id-0.1.0.tar.gz
| Download URL | signed_id-0.1.0.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4fd2d2da901fc3c3d4312957fb050a037ea85f89549f6c92d3b4c1bee44d3ca9
|
|
BLAKE2b-256 checksum How to use checksums |
4938b88a68a72e676eec074808d4c746e79da285ed384f399c85aa1e9be38406
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.0
|
Release files / signed_id-0.1.0-py3-none-any.whl
| Download URL | signed_id-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d3dfd6fdc123b96b5e7208fa6d4dafe5132647068f870883c52cadf10bf4ea0d
|
|
BLAKE2b-256 checksum How to use checksums |
caa960e8813633834135d603c219ed826d1d89c51bd13edb3cd9b1401a8b39de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.9.0
|