Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing profile to disk fails because hashlib does not support usedforsecurity keyword #581

Closed
Martin-Rey opened this issue Sep 7, 2020 · 3 comments

Comments

@Martin-Rey
Copy link
Contributor

Hi all,

I am trying to write a pynbody.profile.Profile object, using its build-in method write():

def write(self):

which fails with the following trace:

Traceback (most recent call last):
  File "/Users/martinrey/Documents/stellar_halos/plot_surface_brightness.py", line 70, in <module>
    profile2d.write()
  File "/opt/anaconda3/lib/python3.7/site-packages/pynbody-0.48-py3.7-macosx-10.9-x86_64.egg/pynbody/analysis/profile.py", line 551, in write
    filename = self._generate_hash_filename()
  File "/opt/anaconda3/lib/python3.7/site-packages/pynbody-0.48-py3.7-macosx-10.9-x86_64.egg/pynbody/analysis/profile.py", line 522, in _generate_hash_filename
    (hashlib.md5(self._x, usedforsecurity=False)).hexdigest()
TypeError: openssl_md5() takes no keyword arguments

Thanks to https://stackoverflow.com/questions/54717862/how-do-i-know-if-the-usedforsecurity-flag-is-supported-by-hashlib-md5 and sphinx-doc/sphinx#7611, I have tracked this to the fact that my platform (Mac OS) does not support the usedforsecurity keyword used in the hashing here:

def _generate_hash_filename(self):
"""Create a filename for the saved profile from a hash using the binning data"""
import hashlib
try:
filename = self.sim.base.filename + '.profile.' + \
(hashlib.md5(self._x, usedforsecurity=False)).hexdigest()
except AttributeError:
filename = self.sim.filename + '.profile.' + \
(hashlib.md5(self._x, usedforsecurity=False)).hexdigest()

From python, hash lib is indeed missing the keyword

In [1]: import hashlib, inspect                                                                                                        

In [2]: inspect.getfullargspec(hashlib.new)                                                                                            
Out[2]: FullArgSpec(args=['name', 'data'], varargs=None, varkw='kwargs', defaults=(b'',), kwonlyargs=[], kwonlydefaults=None, annotations={})

for the following platform configuration

In [3]: import sys, platform, pynbody, numpy; print("%s\r\n%s\r\npynbody %s\r\nnumpy %s\r\n"%(sys.version, platform.platform(), pynbody
   ...: .__version__, numpy.__version__))                                                                                              
3.7.4 (default, Aug 13 2019, 15:17:50) 
[Clang 4.0.1 (tags/RELEASE_401/final)]
Darwin-19.6.0-x86_64-i386-64bit
pynbody 0.47
numpy 1.17.2

From what I have understood, the keyword usedforsecurity is unsafe because not supported across all platforms. But my understanding of hashing algorithms is fairly limited (to say the least) and I don't understand whether it is necessary in this context.

Hope that helps,
Martin

@apontzen
Copy link
Member

Thanks, are you able to do a pull request and perhaps even a test? This is a feature I've never actually used.

@Martin-Rey
Copy link
Contributor Author

Yes, I can look and figure out a fix to this. As for a test, I will try but this seems to arise from differences in backend implementations on different operating systems, and I am unsure how to capture this in the Travis build. Will have a go anyway.

@Martin-Rey
Copy link
Contributor Author

Solved and merged in #599

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants