pyquickxorhash 0.1.0__tar.gz
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.
- pyquickxorhash-0.1.0/LICENSE +22 -0
- pyquickxorhash-0.1.0/MANIFEST.in +4 -0
- pyquickxorhash-0.1.0/PKG-INFO +185 -0
- pyquickxorhash-0.1.0/README.md +155 -0
- pyquickxorhash-0.1.0/README.zh-TW.md +155 -0
- pyquickxorhash-0.1.0/pyproject.toml +61 -0
- pyquickxorhash-0.1.0/setup.cfg +4 -0
- pyquickxorhash-0.1.0/setup.py +12 -0
- pyquickxorhash-0.1.0/src/csrc/module.c +305 -0
- pyquickxorhash-0.1.0/src/csrc/quickxorhash.c +88 -0
- pyquickxorhash-0.1.0/src/csrc/quickxorhash.h +42 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash/__init__.py +15 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash/py.typed +0 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash/pyquickxorhash_core.pyi +77 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash.egg-info/PKG-INFO +185 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash.egg-info/SOURCES.txt +21 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash.egg-info/dependency_links.txt +1 -0
- pyquickxorhash-0.1.0/src/pyquickxorhash.egg-info/top_level.txt +1 -0
- pyquickxorhash-0.1.0/tests/reference.py +30 -0
- pyquickxorhash-0.1.0/tests/test_digest.py +244 -0
- pyquickxorhash-0.1.0/tests/test_docs.py +61 -0
- pyquickxorhash-0.1.0/tests/test_interface.py +258 -0
- pyquickxorhash-0.1.0/tests/test_threads.py +153 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OwOb
|
|
4
|
+
Copyright (c) Microsoft Corporation
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyquickxorhash
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: QuickXorHash, the file hash used by OneDrive, with a hashlib-style interface
|
|
5
|
+
Author: OwOb
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/OwOb/pyquickxorhash
|
|
8
|
+
Project-URL: Issues, https://github.com/OwOb/pyquickxorhash/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/OwOb/pyquickxorhash/releases
|
|
10
|
+
Keywords: quickxorhash,onedrive,sharepoint,microsoft-graph,hash,checksum
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: MacOS
|
|
14
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: C
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
|
+
Classifier: Programming Language :: Python :: Free Threading :: 2 - Beta
|
|
23
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.13
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# pyquickxorhash
|
|
32
|
+
|
|
33
|
+
English | [繁體中文](https://github.com/OwOb/pyquickxorhash/blob/main/README.zh-TW.md)
|
|
34
|
+
|
|
35
|
+
QuickXorHash for Python: a C extension with the same interface as the hash objects in [`hashlib`](https://docs.python.org/3/library/hashlib.html).
|
|
36
|
+
|
|
37
|
+
QuickXorHash is the hash that Microsoft Graph reports for files stored in OneDrive and SharePoint, as the [`quickXorHash`](https://learn.microsoft.com/en-us/graph/api/resources/hashes) property. This package computes the same value locally, so you can check that a file was uploaded or downloaded intact.
|
|
38
|
+
|
|
39
|
+
**QuickXorHash is not a cryptographic hash.** It detects accidental changes to a file. It does not protect against deliberate tampering.
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
- **`hashlib`-style objects.** `update()`, `digest()`, `hexdigest()` and `copy()`, with the `name`, `digest_size` and `block_size` attributes. The class also works with `hashlib.file_digest()`.
|
|
44
|
+
- **Any bytes-like input.** `bytes`, `bytearray`, `memoryview` and other objects that support the buffer protocol.
|
|
45
|
+
- **Thread-friendly.** The GIL is released while hashing 2048 bytes or more, and each object has its own lock, so one object can be shared between threads. Free-threaded builds of Python are supported.
|
|
46
|
+
- **Typed.** Type stubs are included.
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
Python 3.13 or later (CPython).
|
|
51
|
+
|
|
52
|
+
Prebuilt wheels are built and tested for:
|
|
53
|
+
|
|
54
|
+
- Linux: x86_64 and aarch64, with glibc or musl
|
|
55
|
+
- Windows: x64, x86 and ARM64
|
|
56
|
+
- macOS: x86_64 (Intel) and arm64 (Apple silicon)
|
|
57
|
+
|
|
58
|
+
They cover the free-threaded builds of Python 3.14 and later as well.
|
|
59
|
+
|
|
60
|
+
If there is no wheel for your platform or your build of Python, pip compiles the extension when it installs the package. That needs a C compiler:
|
|
61
|
+
|
|
62
|
+
- Windows: Microsoft C++ Build Tools
|
|
63
|
+
- Linux: GCC and the Python development headers (for example `python3-dev`)
|
|
64
|
+
- macOS: Xcode Command Line Tools
|
|
65
|
+
|
|
66
|
+
Platforms that are not listed above should work with a standard C compiler but have not been verified.
|
|
67
|
+
|
|
68
|
+
## Installation
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
pip install pyquickxorhash
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
To install the development version from GitHub instead, which always compiles the extension:
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
pip install git+https://github.com/OwOb/pyquickxorhash.git
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
|
|
82
|
+
### Hashing data
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
>>> from pyquickxorhash import quickxorhash
|
|
86
|
+
>>> h = quickxorhash()
|
|
87
|
+
>>> h.update(b"hello ")
|
|
88
|
+
>>> h.update(b"world")
|
|
89
|
+
>>> h.hexdigest()
|
|
90
|
+
'6828031bd8f00610dce10d726b03190000000000'
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Data can also be passed to the constructor:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
>>> quickxorhash(b"hello world").hexdigest()
|
|
97
|
+
'6828031bd8f00610dce10d726b03190000000000'
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Comparing with OneDrive
|
|
101
|
+
|
|
102
|
+
Microsoft Graph returns the hash as a base64 string, so encode the digest the same way before comparing:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
>>> import base64
|
|
106
|
+
>>> base64.b64encode(h.digest()).decode("ascii")
|
|
107
|
+
'aCgDG9jwBhDc4Q1yawMZAAAAAAA='
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Hashing a file
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import base64
|
|
114
|
+
import hashlib
|
|
115
|
+
|
|
116
|
+
from pyquickxorhash import quickxorhash
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def onedrive_hash(path):
|
|
120
|
+
"""Return the QuickXorHash of a file, in the base64 form used by Microsoft Graph."""
|
|
121
|
+
with open(path, "rb") as f:
|
|
122
|
+
digest = hashlib.file_digest(f, quickxorhash).digest()
|
|
123
|
+
return base64.b64encode(digest).decode("ascii")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
To hash data as it arrives, for example while downloading, call `update()` for each chunk:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
h = quickxorhash()
|
|
130
|
+
with open("report.docx", "rb") as f:
|
|
131
|
+
while chunk := f.read(1024 * 1024):
|
|
132
|
+
h.update(chunk)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## API
|
|
136
|
+
|
|
137
|
+
| Member | Description |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `quickxorhash(data=b"")` | Create a hash object, optionally hashing `data` straight away. |
|
|
140
|
+
| `update(data, /)` | Hash more data. Calling it repeatedly is the same as one call with the pieces joined together. |
|
|
141
|
+
| `digest()` | Return the hash of the data so far as 20 `bytes`. |
|
|
142
|
+
| `hexdigest()` | Return the same value as a string of 40 hexadecimal digits. |
|
|
143
|
+
| `copy()` | Return an independent copy of the object in its current state. |
|
|
144
|
+
| `name` | `"quickxorhash"` |
|
|
145
|
+
| `digest_size` | `20` |
|
|
146
|
+
| `block_size` | `160` |
|
|
147
|
+
|
|
148
|
+
`digest()` and `hexdigest()` do not change the object, so you can keep calling `update()` afterwards.
|
|
149
|
+
|
|
150
|
+
### Behaviour shared with `hashlib`
|
|
151
|
+
|
|
152
|
+
- A `str` must be encoded first. Passing one raises `TypeError`.
|
|
153
|
+
- Objects cannot be reset. Create a new object to start over, or use `copy()` to branch from the current state.
|
|
154
|
+
- Objects cannot be subclassed or pickled.
|
|
155
|
+
|
|
156
|
+
### Differences from `hashlib`
|
|
157
|
+
|
|
158
|
+
- The constructor accepts only `data`. The `usedforsecurity` and `string` parameters of the `hashlib` constructors are not supported.
|
|
159
|
+
- The algorithm is not registered with `hashlib`, so `hashlib.new("quickxorhash")` does not work. Import the class from this package instead.
|
|
160
|
+
|
|
161
|
+
## About the algorithm
|
|
162
|
+
|
|
163
|
+
QuickXorHash keeps a 160-bit state. Each input byte is XORed into the state at a bit offset that advances by 11 bits per byte, wrapping around at 160 bits. At the end, the total length in bytes is XORed into the last 8 bytes of the 20-byte result as a 64-bit little-endian integer.
|
|
164
|
+
|
|
165
|
+
Microsoft describes the algorithm, with a C# reference implementation, in the [OneDrive developer documentation](https://learn.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash).
|
|
166
|
+
|
|
167
|
+
## Running the tests
|
|
168
|
+
|
|
169
|
+
The tests use [pytest](https://docs.pytest.org/) and run against the installed package. From a local clone:
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
pip install .
|
|
173
|
+
pip install --group test
|
|
174
|
+
pytest
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`--group` needs pip 25.1 or later. With an older pip, run `pip install pytest` instead. Install the package again after changing the C code.
|
|
178
|
+
|
|
179
|
+
One test hashes more than 4 GiB of data and is not run by default. To run it, use `pytest -m slow`.
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
[MIT](https://github.com/OwOb/pyquickxorhash/blob/main/LICENSE).
|
|
184
|
+
|
|
185
|
+
The implementation is a C port of the C# reference implementation in Microsoft's OneDrive developer documentation (Copyright (c) Microsoft Corporation, MIT License).
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# pyquickxorhash
|
|
2
|
+
|
|
3
|
+
English | [繁體中文](https://github.com/OwOb/pyquickxorhash/blob/main/README.zh-TW.md)
|
|
4
|
+
|
|
5
|
+
QuickXorHash for Python: a C extension with the same interface as the hash objects in [`hashlib`](https://docs.python.org/3/library/hashlib.html).
|
|
6
|
+
|
|
7
|
+
QuickXorHash is the hash that Microsoft Graph reports for files stored in OneDrive and SharePoint, as the [`quickXorHash`](https://learn.microsoft.com/en-us/graph/api/resources/hashes) property. This package computes the same value locally, so you can check that a file was uploaded or downloaded intact.
|
|
8
|
+
|
|
9
|
+
**QuickXorHash is not a cryptographic hash.** It detects accidental changes to a file. It does not protect against deliberate tampering.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **`hashlib`-style objects.** `update()`, `digest()`, `hexdigest()` and `copy()`, with the `name`, `digest_size` and `block_size` attributes. The class also works with `hashlib.file_digest()`.
|
|
14
|
+
- **Any bytes-like input.** `bytes`, `bytearray`, `memoryview` and other objects that support the buffer protocol.
|
|
15
|
+
- **Thread-friendly.** The GIL is released while hashing 2048 bytes or more, and each object has its own lock, so one object can be shared between threads. Free-threaded builds of Python are supported.
|
|
16
|
+
- **Typed.** Type stubs are included.
|
|
17
|
+
|
|
18
|
+
## Requirements
|
|
19
|
+
|
|
20
|
+
Python 3.13 or later (CPython).
|
|
21
|
+
|
|
22
|
+
Prebuilt wheels are built and tested for:
|
|
23
|
+
|
|
24
|
+
- Linux: x86_64 and aarch64, with glibc or musl
|
|
25
|
+
- Windows: x64, x86 and ARM64
|
|
26
|
+
- macOS: x86_64 (Intel) and arm64 (Apple silicon)
|
|
27
|
+
|
|
28
|
+
They cover the free-threaded builds of Python 3.14 and later as well.
|
|
29
|
+
|
|
30
|
+
If there is no wheel for your platform or your build of Python, pip compiles the extension when it installs the package. That needs a C compiler:
|
|
31
|
+
|
|
32
|
+
- Windows: Microsoft C++ Build Tools
|
|
33
|
+
- Linux: GCC and the Python development headers (for example `python3-dev`)
|
|
34
|
+
- macOS: Xcode Command Line Tools
|
|
35
|
+
|
|
36
|
+
Platforms that are not listed above should work with a standard C compiler but have not been verified.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
pip install pyquickxorhash
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To install the development version from GitHub instead, which always compiles the extension:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
pip install git+https://github.com/OwOb/pyquickxorhash.git
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### Hashing data
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
>>> from pyquickxorhash import quickxorhash
|
|
56
|
+
>>> h = quickxorhash()
|
|
57
|
+
>>> h.update(b"hello ")
|
|
58
|
+
>>> h.update(b"world")
|
|
59
|
+
>>> h.hexdigest()
|
|
60
|
+
'6828031bd8f00610dce10d726b03190000000000'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Data can also be passed to the constructor:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
>>> quickxorhash(b"hello world").hexdigest()
|
|
67
|
+
'6828031bd8f00610dce10d726b03190000000000'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Comparing with OneDrive
|
|
71
|
+
|
|
72
|
+
Microsoft Graph returns the hash as a base64 string, so encode the digest the same way before comparing:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
>>> import base64
|
|
76
|
+
>>> base64.b64encode(h.digest()).decode("ascii")
|
|
77
|
+
'aCgDG9jwBhDc4Q1yawMZAAAAAAA='
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Hashing a file
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import base64
|
|
84
|
+
import hashlib
|
|
85
|
+
|
|
86
|
+
from pyquickxorhash import quickxorhash
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def onedrive_hash(path):
|
|
90
|
+
"""Return the QuickXorHash of a file, in the base64 form used by Microsoft Graph."""
|
|
91
|
+
with open(path, "rb") as f:
|
|
92
|
+
digest = hashlib.file_digest(f, quickxorhash).digest()
|
|
93
|
+
return base64.b64encode(digest).decode("ascii")
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
To hash data as it arrives, for example while downloading, call `update()` for each chunk:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
h = quickxorhash()
|
|
100
|
+
with open("report.docx", "rb") as f:
|
|
101
|
+
while chunk := f.read(1024 * 1024):
|
|
102
|
+
h.update(chunk)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## API
|
|
106
|
+
|
|
107
|
+
| Member | Description |
|
|
108
|
+
| --- | --- |
|
|
109
|
+
| `quickxorhash(data=b"")` | Create a hash object, optionally hashing `data` straight away. |
|
|
110
|
+
| `update(data, /)` | Hash more data. Calling it repeatedly is the same as one call with the pieces joined together. |
|
|
111
|
+
| `digest()` | Return the hash of the data so far as 20 `bytes`. |
|
|
112
|
+
| `hexdigest()` | Return the same value as a string of 40 hexadecimal digits. |
|
|
113
|
+
| `copy()` | Return an independent copy of the object in its current state. |
|
|
114
|
+
| `name` | `"quickxorhash"` |
|
|
115
|
+
| `digest_size` | `20` |
|
|
116
|
+
| `block_size` | `160` |
|
|
117
|
+
|
|
118
|
+
`digest()` and `hexdigest()` do not change the object, so you can keep calling `update()` afterwards.
|
|
119
|
+
|
|
120
|
+
### Behaviour shared with `hashlib`
|
|
121
|
+
|
|
122
|
+
- A `str` must be encoded first. Passing one raises `TypeError`.
|
|
123
|
+
- Objects cannot be reset. Create a new object to start over, or use `copy()` to branch from the current state.
|
|
124
|
+
- Objects cannot be subclassed or pickled.
|
|
125
|
+
|
|
126
|
+
### Differences from `hashlib`
|
|
127
|
+
|
|
128
|
+
- The constructor accepts only `data`. The `usedforsecurity` and `string` parameters of the `hashlib` constructors are not supported.
|
|
129
|
+
- The algorithm is not registered with `hashlib`, so `hashlib.new("quickxorhash")` does not work. Import the class from this package instead.
|
|
130
|
+
|
|
131
|
+
## About the algorithm
|
|
132
|
+
|
|
133
|
+
QuickXorHash keeps a 160-bit state. Each input byte is XORed into the state at a bit offset that advances by 11 bits per byte, wrapping around at 160 bits. At the end, the total length in bytes is XORed into the last 8 bytes of the 20-byte result as a 64-bit little-endian integer.
|
|
134
|
+
|
|
135
|
+
Microsoft describes the algorithm, with a C# reference implementation, in the [OneDrive developer documentation](https://learn.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash).
|
|
136
|
+
|
|
137
|
+
## Running the tests
|
|
138
|
+
|
|
139
|
+
The tests use [pytest](https://docs.pytest.org/) and run against the installed package. From a local clone:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
pip install .
|
|
143
|
+
pip install --group test
|
|
144
|
+
pytest
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`--group` needs pip 25.1 or later. With an older pip, run `pip install pytest` instead. Install the package again after changing the C code.
|
|
148
|
+
|
|
149
|
+
One test hashes more than 4 GiB of data and is not run by default. To run it, use `pytest -m slow`.
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
[MIT](https://github.com/OwOb/pyquickxorhash/blob/main/LICENSE).
|
|
154
|
+
|
|
155
|
+
The implementation is a C port of the C# reference implementation in Microsoft's OneDrive developer documentation (Copyright (c) Microsoft Corporation, MIT License).
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# pyquickxorhash
|
|
2
|
+
|
|
3
|
+
[English](README.md) | 繁體中文
|
|
4
|
+
|
|
5
|
+
Python 的 QuickXorHash 實作:以 C 擴充模組撰寫,介面與 [`hashlib`](https://docs.python.org/zh-tw/3/library/hashlib.html) 的雜湊物件相同。
|
|
6
|
+
|
|
7
|
+
QuickXorHash 是 Microsoft Graph 為 OneDrive 與 SharePoint 上的檔案回報的雜湊值,也就是 [`quickXorHash`](https://learn.microsoft.com/zh-tw/graph/api/resources/hashes) 屬性。這個套件可以在本機算出相同的值,用來確認檔案上傳或下載後內容完整無誤。
|
|
8
|
+
|
|
9
|
+
**QuickXorHash 不是密碼學雜湊。** 它能偵測檔案的意外變動,但無法防範蓄意竄改。
|
|
10
|
+
|
|
11
|
+
## 特色
|
|
12
|
+
|
|
13
|
+
- **`hashlib` 風格的物件。** 提供 `update()`、`digest()`、`hexdigest()`、`copy()`,以及 `name`、`digest_size`、`block_size` 屬性。這個類別也可以搭配 `hashlib.file_digest()` 使用。
|
|
14
|
+
- **接受任何類位元組物件。** `bytes`、`bytearray`、`memoryview`,以及其他支援緩衝區協定的物件。
|
|
15
|
+
- **適合多執行緒。** 雜湊 2048 位元組以上的資料時會釋放 GIL,而且每個物件都有自己的鎖,所以同一個物件可以在執行緒之間共用。也支援 free-threaded(無 GIL)版本的 Python。
|
|
16
|
+
- **附型別資訊。** 套件內含型別存根檔。
|
|
17
|
+
|
|
18
|
+
## 需求
|
|
19
|
+
|
|
20
|
+
Python 3.13 以上(CPython)。
|
|
21
|
+
|
|
22
|
+
以下平台提供預先編譯的 wheel,而且都經過測試:
|
|
23
|
+
|
|
24
|
+
- Linux:x86_64 與 aarch64,glibc 或 musl 皆可
|
|
25
|
+
- Windows:x64、x86 與 ARM64
|
|
26
|
+
- macOS:x86_64(Intel)與 arm64(Apple 晶片)
|
|
27
|
+
|
|
28
|
+
其中也包含 Python 3.14 以上的 free-threaded 版本。
|
|
29
|
+
|
|
30
|
+
如果沒有適用於你的平台或 Python 版本的 wheel,pip 會在安裝時編譯擴充模組,這時需要 C 編譯器:
|
|
31
|
+
|
|
32
|
+
- Windows:Microsoft C++ Build Tools
|
|
33
|
+
- Linux:GCC 與 Python 開發標頭檔(例如 `python3-dev`)
|
|
34
|
+
- macOS:Xcode Command Line Tools
|
|
35
|
+
|
|
36
|
+
上面沒有列出的平台,使用標準的 C 編譯器應該可以運作,但尚未驗證。
|
|
37
|
+
|
|
38
|
+
## 安裝
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
pip install pyquickxorhash
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
如果要改從 GitHub 安裝開發中的版本,一律會編譯擴充模組:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
pip install git+https://github.com/OwOb/pyquickxorhash.git
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 使用方式
|
|
51
|
+
|
|
52
|
+
### 雜湊資料
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
>>> from pyquickxorhash import quickxorhash
|
|
56
|
+
>>> h = quickxorhash()
|
|
57
|
+
>>> h.update(b"hello ")
|
|
58
|
+
>>> h.update(b"world")
|
|
59
|
+
>>> h.hexdigest()
|
|
60
|
+
'6828031bd8f00610dce10d726b03190000000000'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
也可以把資料直接傳給建構式:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
>>> quickxorhash(b"hello world").hexdigest()
|
|
67
|
+
'6828031bd8f00610dce10d726b03190000000000'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 與 OneDrive 的值比對
|
|
71
|
+
|
|
72
|
+
Microsoft Graph 回傳的雜湊是 base64 字串,所以比對前要用同樣的方式編碼:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
>>> import base64
|
|
76
|
+
>>> base64.b64encode(h.digest()).decode("ascii")
|
|
77
|
+
'aCgDG9jwBhDc4Q1yawMZAAAAAAA='
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 雜湊檔案
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import base64
|
|
84
|
+
import hashlib
|
|
85
|
+
|
|
86
|
+
from pyquickxorhash import quickxorhash
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def onedrive_hash(path):
|
|
90
|
+
"""回傳檔案的 QuickXorHash,格式為 Microsoft Graph 使用的 base64 字串。"""
|
|
91
|
+
with open(path, "rb") as f:
|
|
92
|
+
digest = hashlib.file_digest(f, quickxorhash).digest()
|
|
93
|
+
return base64.b64encode(digest).decode("ascii")
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
如果資料是陸續到達的(例如下載途中),對每一段資料呼叫 `update()` 即可:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
h = quickxorhash()
|
|
100
|
+
with open("report.docx", "rb") as f:
|
|
101
|
+
while chunk := f.read(1024 * 1024):
|
|
102
|
+
h.update(chunk)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## API
|
|
106
|
+
|
|
107
|
+
| 成員 | 說明 |
|
|
108
|
+
| --- | --- |
|
|
109
|
+
| `quickxorhash(data=b"")` | 建立雜湊物件,可以同時傳入要先雜湊的 `data`。 |
|
|
110
|
+
| `update(data, /)` | 加入更多資料。分多次呼叫的結果,與把各段資料接起來一次傳入相同。 |
|
|
111
|
+
| `digest()` | 以 20 個位元組的 `bytes` 回傳目前為止所有資料的雜湊值。 |
|
|
112
|
+
| `hexdigest()` | 以 40 個十六進位字元的字串回傳同一個值。 |
|
|
113
|
+
| `copy()` | 回傳一個狀態相同、彼此獨立的複本。 |
|
|
114
|
+
| `name` | `"quickxorhash"` |
|
|
115
|
+
| `digest_size` | `20` |
|
|
116
|
+
| `block_size` | `160` |
|
|
117
|
+
|
|
118
|
+
`digest()` 與 `hexdigest()` 不會改變物件,之後仍然可以繼續呼叫 `update()`。
|
|
119
|
+
|
|
120
|
+
### 與 `hashlib` 相同的行為
|
|
121
|
+
|
|
122
|
+
- `str` 必須先編碼,直接傳入會引發 `TypeError`。
|
|
123
|
+
- 物件無法重設。要重新開始請建立新物件,要從目前的狀態分支請使用 `copy()`。
|
|
124
|
+
- 物件不能被繼承,也不能被 pickle。
|
|
125
|
+
|
|
126
|
+
### 與 `hashlib` 不同之處
|
|
127
|
+
|
|
128
|
+
- 建構式只接受 `data`,不支援 `hashlib` 建構式的 `usedforsecurity` 與 `string` 參數。
|
|
129
|
+
- 這個演算法沒有向 `hashlib` 註冊,所以 `hashlib.new("quickxorhash")` 無法使用,請直接從本套件匯入類別。
|
|
130
|
+
|
|
131
|
+
## 關於演算法
|
|
132
|
+
|
|
133
|
+
QuickXorHash 維護一個 160 位元的狀態。每個輸入位元組會被 XOR 到狀態中的某個位元位置,這個位置每處理一個位元組就前進 11 位元,超過 160 位元時繞回開頭。最後,資料的總長度(以位元組計)會以 64 位元小端序整數的形式,XOR 到 20 位元組結果的最後 8 個位元組。
|
|
134
|
+
|
|
135
|
+
Microsoft 在 [OneDrive 開發人員文件](https://learn.microsoft.com/zh-tw/onedrive/developer/code-snippets/quickxorhash)中說明了這個演算法,並附有 C# 參考實作。
|
|
136
|
+
|
|
137
|
+
## 執行測試
|
|
138
|
+
|
|
139
|
+
測試使用 [pytest](https://docs.pytest.org/),並且是對已安裝的套件執行。在本機的複本中:
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
pip install .
|
|
143
|
+
pip install --group test
|
|
144
|
+
pytest
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`--group` 需要 pip 25.1 以上。pip 版本較舊時,請改用 `pip install pytest`。修改 C 程式碼之後,要重新安裝套件。
|
|
148
|
+
|
|
149
|
+
其中一個測試會雜湊超過 4 GiB 的資料,預設不會執行。要執行它,請使用 `pytest -m slow`。
|
|
150
|
+
|
|
151
|
+
## 授權
|
|
152
|
+
|
|
153
|
+
[MIT](LICENSE)。
|
|
154
|
+
|
|
155
|
+
本實作是 Microsoft OneDrive 開發人員文件中 C# 參考實作的 C 語言移植版(Copyright (c) Microsoft Corporation,MIT 授權)。
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=80.9.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pyquickxorhash"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "QuickXorHash, the file hash used by OneDrive, with a hashlib-style interface"
|
|
9
|
+
requires-python = ">=3.13"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
keywords = ["quickxorhash", "onedrive", "sharepoint", "microsoft-graph", "hash", "checksum"]
|
|
12
|
+
authors = [{ name = "OwOb" }]
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
# The license is given by `license` above, so there is no license classifier.
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: MacOS",
|
|
20
|
+
"Operating System :: Microsoft :: Windows",
|
|
21
|
+
"Operating System :: POSIX :: Linux",
|
|
22
|
+
"Programming Language :: C",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Programming Language :: Python :: 3.15",
|
|
28
|
+
"Programming Language :: Python :: Free Threading :: 2 - Beta",
|
|
29
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
31
|
+
"Typing :: Typed",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Repository = "https://github.com/OwOb/pyquickxorhash"
|
|
36
|
+
Issues = "https://github.com/OwOb/pyquickxorhash/issues"
|
|
37
|
+
Changelog = "https://github.com/OwOb/pyquickxorhash/releases"
|
|
38
|
+
|
|
39
|
+
[tool.setuptools]
|
|
40
|
+
packages = ["pyquickxorhash"]
|
|
41
|
+
package-dir = { "" = "src" }
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.package-data]
|
|
44
|
+
pyquickxorhash = ["py.typed", "*.pyi"]
|
|
45
|
+
|
|
46
|
+
[dependency-groups]
|
|
47
|
+
test = ["pytest>=8"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
testpaths = ["tests"]
|
|
51
|
+
pythonpath = ["tests"] # lets the test modules import tests/reference.py
|
|
52
|
+
addopts = ["-ra", "-m", "not slow"]
|
|
53
|
+
markers = ["slow: hashes more than 4 GiB of data (run with: pytest -m slow)"]
|
|
54
|
+
|
|
55
|
+
[tool.cibuildwheel]
|
|
56
|
+
test-groups = ["test"]
|
|
57
|
+
test-command = "pytest {project}/tests"
|
|
58
|
+
|
|
59
|
+
[tool.pylint.basic]
|
|
60
|
+
# Test functions are described by their names, so they need no docstring.
|
|
61
|
+
no-docstring-rgx = "^(_|test_)"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from setuptools import Extension, setup
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
ext_modules=[
|
|
5
|
+
Extension(
|
|
6
|
+
"pyquickxorhash.pyquickxorhash_core",
|
|
7
|
+
sources=["src/csrc/module.c", "src/csrc/quickxorhash.c"],
|
|
8
|
+
# Rebuild when the header changes, and ship it in the sdist.
|
|
9
|
+
depends=["src/csrc/quickxorhash.h"],
|
|
10
|
+
)
|
|
11
|
+
]
|
|
12
|
+
)
|