Use MD5 usedforsecurity=False on Python 3.9 and higher to pass FIPS

Closes #2270
This commit is contained in:
Simon Willison 2024-02-13 18:23:54 -08:00 committed by GitHub
commit b89cac3b6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View file

@ -1,7 +1,6 @@
import asyncio
from collections import namedtuple
from pathlib import Path
import hashlib
import janus
import queue
import sys
@ -15,6 +14,7 @@ from .utils import (
detect_spatialite,
get_all_foreign_keys,
get_outbound_foreign_keys,
md5_not_usedforsecurity,
sqlite_timelimit,
sqlite3,
table_columns,
@ -74,7 +74,7 @@ class Database:
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:

View file

@ -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
@ -1401,3 +1401,11 @@ def redact_keys(original: dict, key_patterns: Iterable) -> dict:
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()