Skip to content

Commit 580f118

Browse files
zeshengzongSkylion007
authored andcommitted
Enable ruff rule S324 (pytorch#147665)
Fixes pytorch#147627 - Add `S324` in `pyproject.toml ` - Running check and clean warnings ```bash lintrunner --take RUFF --all-files ``` Pull Request resolved: pytorch#147665 Approved by: https://github.com/Skylion007 Co-authored-by: Aaron Gokaslan <[email protected]>
1 parent 6061664 commit 580f118

File tree

10 files changed

+19
-9
lines changed

10 files changed

+19
-9
lines changed

.github/scripts/pytest_caching_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PRIdentifier(str):
3333
__slots__ = ()
3434

3535
def __new__(cls, value: str) -> "PRIdentifier":
36-
md5 = hashlib.md5(value.encode("utf-8")).hexdigest()
36+
md5 = hashlib.md5(value.encode("utf-8"), usedforsecurity=False).hexdigest()
3737
return super().__new__(cls, md5)
3838

3939

benchmarks/instruction_counts/applications/ci.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def main(argv: list[str]) -> None:
4444
)
4545

4646
keys = tuple({str(work_order): None for work_order in work_orders}.keys())
47-
md5 = hashlib.md5()
47+
md5 = hashlib.md5(usedforsecurity=False)
4848
for key in keys:
4949
md5.update(key.encode("utf-8"))
5050

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ select = [
149149
"RUF024", # from keys mutable
150150
"RUF026", # default factory kwarg
151151
"RUF030", # No print statement in assert
152+
"S324", # for hashlib FIPS compliance
152153
"SLOT",
153154
"TCH",
154155
"TRY002", # ban vanilla raise (todo fix NOQAs)

tools/stats/upload_dynamo_perf_stats.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def generate_partition_key(repo: str, doc: dict[str, Any]) -> str:
9595
test_name = doc["test_name"]
9696
filename = doc["filename"]
9797

98-
hash_content = hashlib.md5(json.dumps(doc).encode("utf-8")).hexdigest()
98+
hash_content = hashlib.md5(
99+
json.dumps(doc).encode("utf-8"), usedforsecurity=False
100+
).hexdigest()
99101
return f"{repo}/{workflow_id}/{job_id}/{test_name}/{filename}/{hash_content}"
100102

101103

torch/_logging/_internal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def json_default(obj):
12751275

12761276
# force newlines so we are unlikely to overflow line limit
12771277
payload = json.dumps(payload, default=json_default, indent=0)
1278-
h = hashlib.md5()
1278+
h = hashlib.md5(usedforsecurity=False)
12791279
h.update(payload.encode("utf-8"))
12801280
record["has_payload"] = h.hexdigest()
12811281
trace_log.debug(

torch/distributed/distributed_c10d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4736,7 +4736,7 @@ def _hash_ranks_to_str(ranks: list[int]) -> str:
47364736
rank_join: str = "_".join(map(str, ranks))
47374737
# In case there is already a PG with the same rank composition
47384738
unique_str = "_".join([rank_join, str(len(_world.pg_names))])
4739-
return hashlib.sha1(bytes(unique_str, "utf-8")).hexdigest()
4739+
return hashlib.sha1(bytes(unique_str, "utf-8"), usedforsecurity=False).hexdigest()
47404740

47414741

47424742
# Takes a list of ranks and computes an integer color

torch/fx/passes/graph_drawer.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ def _get_node_style(self, node: torch.fx.Node) -> dict[str, str]:
165165
else:
166166
# Use a random color for each node; based on its name so it's stable.
167167
target_name = node._pretty_print_target(node.target)
168-
target_hash = int(hashlib.md5(target_name.encode()).hexdigest()[:8], 16)
168+
target_hash = int(
169+
hashlib.md5(
170+
target_name.encode(), usedforsecurity=False
171+
).hexdigest()[:8],
172+
16,
173+
)
169174
template["fillcolor"] = _HASH_COLOR_MAP[
170175
target_hash % len(_HASH_COLOR_MAP)
171176
]

torch/utils/_config_module.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,9 @@ def get_hash(self) -> bytes:
584584
if self._is_dirty or self._hash_digest is None:
585585
dict_to_hash = self._get_dict(ignored_keys=list(self._compile_ignored_keys))
586586
string_to_hash = repr(sorted(dict_to_hash.items()))
587-
self._hash_digest = hashlib.md5(string_to_hash.encode("utf-8")).digest()
587+
self._hash_digest = hashlib.md5(
588+
string_to_hash.encode("utf-8"), usedforsecurity=False
589+
).digest()
588590
self._is_dirty = False
589591
return self._hash_digest
590592

torch/utils/_content_store.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def hash_storage(storage: torch.UntypedStorage, *, stable_hash: bool = False) ->
105105
buf = (ctypes.c_byte * cpu_storage.nbytes()).from_address(
106106
cpu_storage.data_ptr()
107107
)
108-
sha1 = hashlib.sha1()
108+
sha1 = hashlib.sha1(usedforsecurity=False)
109109
sha1.update(buf)
110110
return sha1.hexdigest()
111111

torchgen/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _read_template(template_fn: str) -> CodeTemplate:
110110

111111
# String hash that's stable across different executions, unlike builtin hash
112112
def string_stable_hash(s: str) -> int:
113-
sha1 = hashlib.sha1(s.encode("latin1")).digest()
113+
sha1 = hashlib.sha1(s.encode("latin1"), usedforsecurity=False).digest()
114114
return int.from_bytes(sha1, byteorder="little")
115115

116116

0 commit comments

Comments
 (0)