Skip to content

Commit f4cd65b

Browse files
authored
Set hashlib.md5(usedforsecurity=False) when computing POTCAR hashes (#3094)
* set hashlib.md5(usedforsecurity=False) when computing POTCAR hashes * hashlib.md5(usedforsecurity=False) is py39+ * missed one
1 parent 893cad7 commit f4cd65b

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

pymatgen/io/abinit/pseudos.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def as_pseudo(cls, obj):
101101
return obj if isinstance(obj, cls) else cls.from_file(obj)
102102

103103
@staticmethod
104-
def from_file(filename):
104+
def from_file(filename) -> Pseudo:
105105
"""
106106
Build an instance of a concrete Pseudo subclass from filename.
107107
Note: the parser knows the concrete class that should be instantiated
@@ -118,20 +118,20 @@ def __eq__(self, other: object) -> bool:
118118
and self.__class__ == other.__class__
119119
)
120120

121-
def __repr__(self):
121+
def __repr__(self) -> str:
122122
try:
123123
return f"<{type(self).__name__} at {os.path.relpath(self.filepath)}>"
124124
except Exception:
125125
# relpath can fail if the code is executed in demon mode.
126126
return f"<{type(self).__name__} at {self.filepath}>"
127127

128-
def __str__(self):
128+
def __str__(self) -> str:
129129
return self.to_string()
130130

131-
def to_string(self, verbose=0):
131+
def to_string(self, verbose=0) -> str:
132132
"""String representation."""
133133
# pylint: disable=E1101
134-
lines = []
134+
lines: list[str] = []
135135
app = lines.append
136136
app(f"<{type(self).__name__}: {self.basename}>")
137137
app(" summary: " + self.summary.strip())
@@ -153,66 +153,64 @@ def to_string(self, verbose=0):
153153

154154
@property
155155
@abc.abstractmethod
156-
def summary(self):
156+
def summary(self) -> str:
157157
"""String summarizing the most important properties."""
158158

159159
@property
160-
def filepath(self):
160+
def filepath(self) -> str:
161161
"""Absolute path to pseudopotential file."""
162-
# pylint: disable=E1101
163162
return os.path.abspath(self.path)
164163

165164
@property
166-
def basename(self):
165+
def basename(self) -> str:
167166
"""File basename."""
168-
# pylint: disable=E1101
169167
return os.path.basename(self.filepath)
170168

171169
@property
172170
@abc.abstractmethod
173-
def Z(self):
171+
def Z(self) -> int:
174172
"""The atomic number of the atom."""
175173

176174
@property
177175
@abc.abstractmethod
178-
def Z_val(self):
176+
def Z_val(self) -> int:
179177
"""Valence charge."""
180178

181179
@property
182-
def type(self):
180+
def type(self) -> str:
183181
"""Type of pseudo."""
184182
return type(self).__name__
185183

186184
@property
187-
def element(self):
185+
def element(self) -> Element:
188186
"""Pymatgen :class:`Element`."""
189187
try:
190188
return Element.from_Z(self.Z)
191189
except (KeyError, IndexError):
192190
return Element.from_Z(int(self.Z))
193191

194192
@property
195-
def symbol(self):
193+
def symbol(self) -> str:
196194
"""Element symbol."""
197195
return self.element.symbol
198196

199197
@property
200198
@abc.abstractmethod
201-
def l_max(self):
199+
def l_max(self) -> int:
202200
"""Maximum angular momentum."""
203201

204202
@property
205203
@abc.abstractmethod
206-
def l_local(self):
204+
def l_local(self) -> int:
207205
"""Angular momentum used for the local part."""
208206

209207
@property
210-
def isnc(self):
208+
def isnc(self) -> bool:
211209
"""True if norm-conserving pseudopotential."""
212210
return isinstance(self, NcPseudo)
213211

214212
@property
215-
def ispaw(self):
213+
def ispaw(self) -> bool:
216214
"""True if PAW pseudopotential."""
217215
return isinstance(self, PawPseudo)
218216

@@ -223,14 +221,17 @@ def md5(self):
223221
return self.compute_md5()
224222

225223
def compute_md5(self):
226-
"""Compute and erturn MD5 hash value."""
224+
"""Compute and return MD5 hash value."""
227225
# pylint: disable=E1101
228226
import hashlib
229227

230228
with open(self.path) as fh:
231229
text = fh.read()
232-
m = hashlib.md5(text.encode("utf-8"))
233-
return m.hexdigest()
230+
# usedforsecurity=False needed in FIPS mode (Federal Information Processing Standards)
231+
# https://github.com/materialsproject/pymatgen/issues/2804
232+
md5 = hashlib.new("md5", usedforsecurity=False) # hashlib.md5(usedforsecurity=False) is py39+
233+
md5.update(text.encode("utf-8"))
234+
return md5.hexdigest()
234235

235236
@property
236237
@abc.abstractmethod
@@ -242,7 +243,6 @@ def supports_soc(self):
242243

243244
def as_dict(self, **kwargs):
244245
"""Return dictionary for MSONable protocol."""
245-
# pylint: disable=E1101
246246
return {
247247
"@module": type(self).__module__,
248248
"@class": type(self).__name__,

pymatgen/io/cp2k/inputs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
from __future__ import annotations
2525

2626
import copy
27+
import hashlib
2728
import itertools
2829
import os
2930
import re
3031
import textwrap
3132
import typing
3233
from dataclasses import dataclass, field
33-
from hashlib import md5
3434
from pathlib import Path
3535
from typing import TYPE_CHECKING, Any, Iterable, Literal, Sequence
3636

@@ -2416,7 +2416,11 @@ def softmatch(self, other):
24162416

24172417
def get_hash(self) -> str:
24182418
"""Get a hash of this object"""
2419-
return md5(self.get_string().lower().encode("utf-8")).hexdigest()
2419+
# usedforsecurity=False needed in FIPS mode (Federal Information Processing Standards)
2420+
# https://github.com/materialsproject/pymatgen/issues/2804
2421+
md5 = hashlib.new("md5", usedforsecurity=False) # hashlib.md5(usedforsecurity=False) is py39+
2422+
md5.update(self.get_string().lower().encode("utf-8"))
2423+
return md5.hexdigest()
24202424

24212425
def get_string(self):
24222426
"""Get string representation"""

pymatgen/io/vasp/inputs.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import annotations
77

8+
import hashlib
89
import itertools
910
import json
1011
import logging
@@ -16,7 +17,7 @@
1617
from collections import namedtuple
1718
from enum import Enum
1819
from glob import glob
19-
from hashlib import md5, sha256
20+
from hashlib import sha256
2021
from typing import TYPE_CHECKING, Any, Literal, Sequence
2122

2223
import numpy as np
@@ -2175,7 +2176,11 @@ def get_potcar_file_hash(self):
21752176
21762177
:return: Hash value.
21772178
"""
2178-
return md5(self.data.encode("utf-8")).hexdigest()
2179+
# usedforsecurity=False needed in FIPS mode (Federal Information Processing Standards)
2180+
# https://github.com/materialsproject/pymatgen/issues/2804
2181+
md5 = hashlib.new("md5", usedforsecurity=False) # hashlib.md5(usedforsecurity=False) is py39+
2182+
md5.update(self.data.encode("utf-8"))
2183+
return md5.hexdigest()
21792184

21802185
def get_potcar_hash(self):
21812186
"""
@@ -2212,7 +2217,11 @@ def get_potcar_hash(self):
22122217
hash_str += v.replace(" ", "")
22132218

22142219
self.hash_str = hash_str
2215-
return md5(hash_str.lower().encode("utf-8")).hexdigest()
2220+
# usedforsecurity=False needed in FIPS mode (Federal Information Processing Standards)
2221+
# https://github.com/materialsproject/pymatgen/issues/2804
2222+
md5 = hashlib.new("md5", usedforsecurity=False) # hashlib.md5(usedforsecurity=False) is py39+
2223+
md5.update(hash_str.lower().encode("utf-8"))
2224+
return md5.hexdigest()
22162225

22172226
def __getattr__(self, a):
22182227
"""

0 commit comments

Comments
 (0)