zid 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
zid/__init__.py ADDED
@@ -0,0 +1,69 @@
1
+ from __future__ import annotations
2
+
3
+ from io import BytesIO
4
+ from os import urandom
5
+ from time import time_ns
6
+ from typing import Final
7
+
8
+ RANDOM_BUFFER_SIZE: int = 128 * 1024 # 128 KiB
9
+ """
10
+ The size of the buffer when requesting random bytes from the operating system.
11
+ """
12
+
13
+ _random_buffer: Final[BytesIO] = BytesIO()
14
+
15
+ _last_time: int = -1
16
+ _last_sequence: int = -1
17
+
18
+
19
+ def zid() -> int:
20
+ """
21
+ Generate a unique identifier.
22
+ """
23
+ global _last_time, _last_sequence
24
+
25
+ # UNIX timestamp in milliseconds
26
+ time: int = time_ns() // 1_000_000
27
+ if time > 0x7FFFFFFFFFFF:
28
+ raise OverflowError('Time value is too large')
29
+
30
+ # CSPRNG-initialized sequence numbers
31
+ sequence: int
32
+ if _last_time == time:
33
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
34
+ else:
35
+ rand: bytes = _random_buffer.read(2)
36
+ if len(rand) < 2:
37
+ _random_buffer.write(urandom(RANDOM_BUFFER_SIZE))
38
+ _random_buffer.seek(0)
39
+ rand = _random_buffer.read(2)
40
+ _last_sequence = sequence = int.from_bytes(rand)
41
+ _last_time = time
42
+
43
+ return (time << 16) | sequence
44
+
45
+
46
+ def _zid_simple() -> int:
47
+ global _last_time, _last_sequence
48
+
49
+ # UNIX timestamp in milliseconds
50
+ time: int = time_ns() // 1_000_000
51
+ if time > 0x7FFFFFFFFFFF:
52
+ raise OverflowError('Time value is too large')
53
+
54
+ # CSPRNG-initialized sequence numbers
55
+ sequence: int
56
+ if _last_time == time:
57
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
58
+ else:
59
+ _last_sequence = sequence = int.from_bytes(urandom(2))
60
+ _last_time = time
61
+
62
+ return (time << 16) | sequence
63
+
64
+
65
+ def parse_zid_timestamp(zid: int) -> int:
66
+ """
67
+ Extract the UNIX timestamp in milliseconds from a ZID.
68
+ """
69
+ return zid >> 16
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.1
2
+ Name: zid
3
+ Version: 1.0.0
4
+ Summary: ZID is a unique identifier with nice properties
5
+ Home-page: https://github.com/Zaczero/zid
6
+ License: Unlicense
7
+ Keywords: uuid,unique identifier,identifier,primary key
8
+ Author: Kamil Monicz
9
+ Author-email: kamil@monicz.dev
10
+ Requires-Python: >=3.8,<4.0
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved
14
+ Classifier: License :: OSI Approved :: The Unlicense (Unlicense)
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.8
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Database
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Classifier: Typing :: Typed
26
+ Project-URL: Repository, https://github.com/Zaczero/zid
27
+ Description-Content-Type: text/markdown
28
+
29
+ # ZID
30
+
31
+ [![PyPI - Python Version](https://shields.monicz.dev/pypi/pyversions/zid)](https://pypi.org/project/zid)
32
+ [![Liberapay Patrons](https://shields.monicz.dev/liberapay/patrons/Zaczero?logo=liberapay&label=Patrons)](https://liberapay.com/Zaczero/)
33
+ [![GitHub Sponsors](https://shields.monicz.dev/github/sponsors/Zaczero?logo=github&label=Sponsors&color=%23db61a2)](https://github.com/sponsors/Zaczero)
34
+
35
+ **ZID** is a unique identifier with nice properties:
36
+
37
+ - It behaves like a 64-bit signed integer, so it can be safely used with external software, e.g., in a database. ZIDs will never overflow into negative values.
38
+
39
+ - ZIDs are numerically sortable since the timestamp is stored in the most significant bits. Additional randomness is stored only in the least significant bits.
40
+
41
+ - The specification is very simple, reducing the potential for bugs and making ZIDs highly efficient to generate and parse. Scroll down for the installation-free copy-and-paste code - it's that short!
42
+
43
+ - CSPRNG-initialized sequence numbers enhance the privacy of the generated identifiers while remaining collision-resistant. You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine.
44
+
45
+ ## Installation
46
+
47
+ ```sh
48
+ pip install zid
49
+ ```
50
+
51
+ ## Installation (copy & paste)
52
+
53
+ Alternatively, you can copy and paste the following code for an installation-free ZID generator. This code excludes some performance optimizations and utility methods for the sake of simplicity:
54
+
55
+ ```py
56
+ from os import urandom
57
+ from time import time_ns
58
+
59
+ _last_time: int = -1
60
+ _last_sequence: int = -1
61
+
62
+ def zid() -> int:
63
+ global _last_time, _last_sequence
64
+
65
+ # UNIX timestamp in milliseconds
66
+ time: int = time_ns() // 1_000_000
67
+ if time > 0x7FFFFFFFFFFF:
68
+ raise OverflowError('Time value is too large')
69
+
70
+ # CSPRNG-initialized sequence numbers
71
+ sequence: int
72
+ if _last_time == time:
73
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
74
+ else:
75
+ _last_sequence = sequence = int.from_bytes(urandom(2))
76
+ _last_time = time
77
+
78
+ return (time << 16) | sequence
79
+ ```
80
+
81
+ ## Basic Usage
82
+
83
+ ```py
84
+ from zid import zid
85
+ zid() # -> 112723768038396241
86
+ zid() # -> 112723768130153517
87
+ zid() # -> 112723768205368402
88
+
89
+ from zid import parse_zid_timestamp
90
+ parse_zid_timestamp(112723768038396241)
91
+ # -> 1720028198828 (UNIX timestamp in milliseconds)
92
+ ```
93
+
94
+ ## Format specification
95
+
96
+ ZID is 64 bits long in binary. Only 63 bits are used to fit in a signed integer. The first 47 bits are a UNIX timestamp in milliseconds. The remaining 16 bits are CSPRNG-initialized sequence numbers.
97
+
98
+ ```txt
99
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100
+ |0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|
101
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102
+ |0| timestamp (31 bits) |
103
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104
+ | timestamp (16 bits) | random+sequence (16 bits) |
105
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106
+ ```
107
+
108
+ ## Limitations
109
+
110
+ - Timestamps support years from 1970 to approx. 6429. To verify this, you can follow the formula *1970 + (2^47 − 1) / 1000 / (3600 * 24) / 365.25*
111
+
112
+ - If several ZIDs are generated with the same millisecond timestamp, knowing one of them will allow you to discover the others due to linearly increasing sequence numbers. Otherwise, guessing ZID values is difficult (but not impossible) due to the millisecond precision of the timestamp and the additional 16 bits of entropy. Do not rely on ZIDs alone for your security!
113
+
114
+ - You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine. With two separate machines, you will generate on average 16,384 ZIDs each before a collision occurs within the same millisecond timestamp. With three separate machines, the average number is 10,240 ZIDs each.
115
+
116
+ - ZIDs are not strictly increasing within the same millisecond timestamp due to the possible sequence number overflow.
117
+
@@ -0,0 +1,5 @@
1
+ zid/__init__.py,sha256=6kpbzkWPTFt4r0A9Whmz7S8rNhnj97zdiweB45Ss81U,1779
2
+ zid-1.0.0.dist-info/LICENSE,sha256=awOCsWJ58m_2kBQwBUGWejVqZm6wuRtCL2hi9rfa0X4,1211
3
+ zid-1.0.0.dist-info/METADATA,sha256=UdfDC6jokYwdacsaSpbp41C9V_GrSdw2mW8u8dV4m6U,5149
4
+ zid-1.0.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
5
+ zid-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any