summaryrefslogtreecommitdiff
path: root/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py
diff options
context:
space:
mode:
authorShubham Saini <shubham6405@gmail.com>2018-12-11 10:01:23 +0000
committerShubham Saini <shubham6405@gmail.com>2018-12-11 10:01:23 +0000
commit68df54d6629ec019142eb149dd037774f2d11e7c (patch)
tree345bc22d46b4e01a4ba8303b94278952a4ed2b9e /venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py
First commit
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py')
-rw-r--r--venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py
new file mode 100644
index 0000000..8cf6367
--- /dev/null
+++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/hashes.py
@@ -0,0 +1,94 @@
1from __future__ import absolute_import
2
3import hashlib
4
5from pip._vendor.six import iteritems, iterkeys, itervalues
6
7from pip._internal.exceptions import (
8 HashMismatch, HashMissing, InstallationError,
9)
10from pip._internal.utils.misc import read_chunks
11
12# The recommended hash algo of the moment. Change this whenever the state of
13# the art changes; it won't hurt backward compatibility.
14FAVORITE_HASH = 'sha256'
15
16
17# Names of hashlib algorithms allowed by the --hash option and ``pip hash``
18# Currently, those are the ones at least as collision-resistant as sha256.
19STRONG_HASHES = ['sha256', 'sha384', 'sha512']
20
21
22class Hashes(object):
23 """A wrapper that builds multiple hashes at once and checks them against
24 known-good values
25
26 """
27 def __init__(self, hashes=None):
28 """
29 :param hashes: A dict of algorithm names pointing to lists of allowed
30 hex digests
31 """
32 self._allowed = {} if hashes is None else hashes
33
34 def check_against_chunks(self, chunks):
35 """Check good hashes against ones built from iterable of chunks of
36 data.
37
38 Raise HashMismatch if none match.
39
40 """
41 gots = {}
42 for hash_name in iterkeys(self._allowed):
43 try:
44 gots[hash_name] = hashlib.new(hash_name)
45 except (ValueError, TypeError):
46 raise InstallationError('Unknown hash name: %s' % hash_name)
47
48 for chunk in chunks:
49 for hash in itervalues(gots):
50 hash.update(chunk)
51
52 for hash_name, got in iteritems(gots):
53 if got.hexdigest() in self._allowed[hash_name]:
54 return
55 self._raise(gots)
56
57 def _raise(self, gots):
58 raise HashMismatch(self._allowed, gots)
59
60 def check_against_file(self, file):
61 """Check good hashes against a file-like object
62
63 Raise HashMismatch if none match.
64
65 """
66 return self.check_against_chunks(read_chunks(file))
67
68 def check_against_path(self, path):
69 with open(path, 'rb') as file:
70 return self.check_against_file(file)
71
72 def __nonzero__(self):
73 """Return whether I know any known-good hashes."""
74 return bool(self._allowed)
75
76 def __bool__(self):
77 return self.__nonzero__()
78
79
80class MissingHashes(Hashes):
81 """A workalike for Hashes used when we're missing a hash for a requirement
82
83 It computes the actual hash of the requirement and raises a HashMissing
84 exception showing it to the user.
85
86 """
87 def __init__(self):
88 """Don't offer the ``hashes`` kwarg."""
89 # Pass our favorite hash in to generate a "gotten hash". With the
90 # empty list, it will never match, so an error will always raise.
91 super(MissingHashes, self).__init__(hashes={FAVORITE_HASH: []})
92
93 def _raise(self, gots):
94 raise HashMissing(gots[FAVORITE_HASH].hexdigest())