|
2 | 2 |
|
3 | 3 | import asyncio
|
4 | 4 | import base64
|
5 |
| -import hashlib |
6 |
| -import hmac |
7 | 5 | import json
|
8 | 6 | import random
|
9 |
| -import string |
10 | 7 | import time
|
11 | 8 | from collections.abc import Callable
|
12 | 9 | from http import HTTPStatus
|
@@ -152,59 +149,3 @@ def encode_key_value_store_record_value(value: Any, content_type: str | None = N
|
152 | 149 | value = json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False, default=str).encode('utf-8')
|
153 | 150 |
|
154 | 151 | return (value, content_type)
|
155 |
| - |
156 |
| - |
157 |
| -# TODO: will be removed once create_hmac_signature is moved to apify_shared.utils |
158 |
| -# https://github.com/apify/apify-shared-python/pull/44 |
159 |
| -CHARSET = string.digits + string.ascii_letters |
160 |
| - |
161 |
| - |
162 |
| -def encode_base62(num: int) -> str: |
163 |
| - """Encode the given number to base62.""" |
164 |
| - if num == 0: |
165 |
| - return CHARSET[0] |
166 |
| - |
167 |
| - res = '' |
168 |
| - while num > 0: |
169 |
| - num, remainder = divmod(num, 62) |
170 |
| - res = CHARSET[remainder] + res |
171 |
| - return res |
172 |
| - |
173 |
| - |
174 |
| -def create_hmac_signature(secret_key: str, message: str) -> str: |
175 |
| - """Generate an HMAC signature and encodes it using Base62. Base62 encoding reduces the signature length. |
176 |
| -
|
177 |
| - HMAC signature is truncated to 30 characters to make it shorter. |
178 |
| -
|
179 |
| - Args: |
180 |
| - secret_key (str): Secret key used for signing signatures |
181 |
| - message (str): Message to be signed |
182 |
| -
|
183 |
| - Returns: |
184 |
| - str: Base62 encoded signature |
185 |
| - """ |
186 |
| - signature = hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()[:30] |
187 |
| - |
188 |
| - decimal_signature = int(signature, 16) |
189 |
| - |
190 |
| - return encode_base62(decimal_signature) |
191 |
| - |
192 |
| - |
193 |
| -def create_storage_signature( |
194 |
| - resource_id: str, url_signing_secret_key: str, expires_in_millis: int | None, version: int = 0 |
195 |
| -) -> str: |
196 |
| - """Create a storage signature for a resource, which can be used to generate signed URLs for accessing the resource. |
197 |
| -
|
198 |
| - The signature is created using HMAC with the provided secret key and includes |
199 |
| - the resource ID, expiration time, and version. |
200 |
| -
|
201 |
| - Note: expires_in_millis is optional. If not provided, the signature will not expire. |
202 |
| -
|
203 |
| - """ |
204 |
| - expires_at = int(time.time() * 1000) + expires_in_millis if expires_in_millis else 0 |
205 |
| - |
206 |
| - message_to_sign = f'{version}.{expires_at}.{resource_id}' |
207 |
| - hmac = create_hmac_signature(url_signing_secret_key, message_to_sign) |
208 |
| - |
209 |
| - base64url_encoded_payload = base64.urlsafe_b64encode(f'{version}.{expires_at}.{hmac}'.encode()) |
210 |
| - return base64url_encoded_payload.decode('utf-8') |
0 commit comments