Skip to content

Commit

Permalink
Use MD5 usedforsecurity=False on Python 3.9 and higher, refs #2280
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 14, 2024
1 parent 5d79974 commit 2273a4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions datasette/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
from collections import namedtuple
from pathlib import Path
import hashlib
import janus
import queue
import sys
Expand All @@ -15,6 +14,7 @@
detect_spatialite,
get_all_foreign_keys,
get_outbound_foreign_keys,
md5_not_usedforsecurity,
sqlite_timelimit,
sqlite3,
table_columns,
Expand Down Expand Up @@ -74,7 +74,7 @@ def cached_table_counts(self):
def color(self):
if self.hash:
return self.hash[:6]
return hashlib.md5(self.name.encode("utf8")).hexdigest()[:6]
return md5_not_usedforsecurity(self.name)[:6]

def suggest_name(self):
if self.path:
Expand Down
10 changes: 9 additions & 1 deletion datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def to_css_class(s):
"""
if css_class_re.match(s):
return s
md5_suffix = hashlib.md5(s.encode("utf8")).hexdigest()[:6]
md5_suffix = md5_not_usedforsecurity(s)[:6]
# Strip leading _, -
s = s.lstrip("_").lstrip("-")
# Replace any whitespace with hyphens
Expand Down Expand Up @@ -1401,3 +1401,11 @@ def redact(data):
return data

return redact(original)


def md5_not_usedforsecurity(s):
try:
return hashlib.md5(s.encode("utf8"), usedforsecurity=False).hexdigest()
except TypeError:
# For Python 3.8 which does not support usedforsecurity=False
return hashlib.md5(s.encode("utf8")).hexdigest()

0 comments on commit 2273a4b

Please sign in to comment.