Skip to content

Commit c3dc4bc

Browse files
committed
set hashlib.md5(usedforsecurity=False) when computing POTCAR hashes
1 parent 47af172 commit c3dc4bc

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

pymatgen/io/abinit/pseudos.py

Lines changed: 22 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,16 @@ 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.md5(text.encode("utf-8"), usedforsecurity=False)
233+
return md5.hexdigest()
234234

235235
@property
236236
@abc.abstractmethod
@@ -242,7 +242,6 @@ def supports_soc(self):
242242

243243
def as_dict(self, **kwargs):
244244
"""Return dictionary for MSONable protocol."""
245-
# pylint: disable=E1101
246245
return {
247246
"@module": type(self).__module__,
248247
"@class": type(self).__name__,

pymatgen/io/cp2k/inputs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2416,7 +2416,9 @@ 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+
return md5(self.get_string().lower().encode("utf-8"), usedforsecurity=False).hexdigest()
24202422

24212423
def get_string(self):
24222424
"""Get string representation"""

pymatgen/io/vasp/inputs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,9 @@ def get_potcar_file_hash(self):
21752175
21762176
:return: Hash value.
21772177
"""
2178-
return md5(self.data.encode("utf-8")).hexdigest()
2178+
# usedforsecurity=False needed in FIPS mode (Federal Information Processing Standards)
2179+
# https://github.com/materialsproject/pymatgen/issues/2804
2180+
return md5(self.data.encode("utf-8"), usedforsecurity=False).hexdigest()
21792181

21802182
def get_potcar_hash(self):
21812183
"""
@@ -2212,7 +2214,9 @@ def get_potcar_hash(self):
22122214
hash_str += v.replace(" ", "")
22132215

22142216
self.hash_str = hash_str
2215-
return md5(hash_str.lower().encode("utf-8")).hexdigest()
2217+
# usedforsecurity=False needed in FIPS mode (Federal Information Processing Standards)
2218+
# https://github.com/materialsproject/pymatgen/issues/2804
2219+
return md5(hash_str.lower().encode("utf-8"), usedforsecurity=False).hexdigest()
22162220

22172221
def __getattr__(self, a):
22182222
"""

0 commit comments

Comments
 (0)