core: migrate uses of hashlib.sha1 to hashutil.sha1
authorAugie Fackler <augie@google.com>
Mon, 13 Jan 2020 17:15:14 -0500
changeset 44060 a61287a95dc3
parent 44059 7126d8b8e0e6
child 44061 cbc5755df6bf
core: migrate uses of hashlib.sha1 to hashutil.sha1 Differential Revision: https://phab.mercurial-scm.org/D7849
mercurial/chgserver.py
mercurial/exchange.py
mercurial/hg.py
mercurial/localrepo.py
mercurial/merge.py
mercurial/obsolete.py
mercurial/patch.py
mercurial/repair.py
mercurial/revlogutils/sidedata.py
mercurial/scmutil.py
mercurial/sparse.py
mercurial/store.py
mercurial/subrepo.py
mercurial/util.py
mercurial/utils/storageutil.py
mercurial/wireprotov1peer.py
mercurial/wireprotov2server.py
--- a/mercurial/chgserver.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/chgserver.py	Mon Jan 13 17:15:14 2020 -0500
@@ -41,7 +41,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import inspect
 import os
 import re
@@ -67,6 +66,7 @@
 )
 
 from .utils import (
+    hashutil,
     procutil,
     stringutil,
 )
@@ -74,7 +74,7 @@
 
 def _hashlist(items):
     """return sha1 hexdigest for a list"""
-    return node.hex(hashlib.sha1(stringutil.pprint(items)).digest())
+    return node.hex(hashutil.sha1(stringutil.pprint(items)).digest())
 
 
 # sensitive config sections affecting confighash
--- a/mercurial/exchange.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/exchange.py	Mon Jan 13 17:15:14 2020 -0500
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import collections
-import hashlib
 
 from .i18n import _
 from .node import (
@@ -40,7 +39,10 @@
     wireprototypes,
 )
 from .interfaces import repository
-from .utils import stringutil
+from .utils import (
+    hashutil,
+    stringutil,
+)
 
 urlerr = util.urlerr
 urlreq = util.urlreq
@@ -2705,7 +2707,7 @@
     Used by peer for unbundling.
     """
     heads = repo.heads()
-    heads_hash = hashlib.sha1(b''.join(sorted(heads))).digest()
+    heads_hash = hashutil.sha1(b''.join(sorted(heads))).digest()
     if not (
         their_heads == [b'force']
         or their_heads == heads
--- a/mercurial/hg.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/hg.py	Mon Jan 13 17:15:14 2020 -0500
@@ -9,7 +9,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import os
 import shutil
 import stat
@@ -48,7 +47,7 @@
     verify as verifymod,
     vfs as vfsmod,
 )
-
+from .utils import hashutil
 from .interfaces import repository as repositorymod
 
 release = lock.release
@@ -738,7 +737,7 @@
                 )
         elif sharenamemode == b'remote':
             sharepath = os.path.join(
-                sharepool, node.hex(hashlib.sha1(source).digest())
+                sharepool, node.hex(hashutil.sha1(source).digest())
             )
         else:
             raise error.Abort(
--- a/mercurial/localrepo.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/localrepo.py	Mon Jan 13 17:15:14 2020 -0500
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import os
 import random
 import sys
@@ -74,6 +73,7 @@
 )
 
 from .utils import (
+    hashutil,
     procutil,
     stringutil,
 )
@@ -2007,7 +2007,7 @@
             )
 
         idbase = b"%.40f#%f" % (random.random(), time.time())
-        ha = hex(hashlib.sha1(idbase).digest())
+        ha = hex(hashutil.sha1(idbase).digest())
         txnid = b'TXN:' + ha
         self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
 
--- a/mercurial/merge.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/merge.py	Mon Jan 13 17:15:14 2020 -0500
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import shutil
 import stat
 import struct
@@ -39,6 +38,7 @@
     util,
     worker,
 )
+from .utils import hashutil
 
 _pack = struct.pack
 _unpack = struct.unpack
@@ -512,7 +512,7 @@
         """hash the path of a local file context for storage in the .hg/merge
         directory."""
 
-        return hex(hashlib.sha1(path).digest())
+        return hex(hashutil.sha1(path).digest())
 
     def add(self, fcl, fco, fca, fd):
         """add a new (potentially?) conflicting file the merge state
--- a/mercurial/obsolete.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/obsolete.py	Mon Jan 13 17:15:14 2020 -0500
@@ -70,7 +70,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 import struct
 
 from .i18n import _
@@ -85,7 +84,10 @@
     pycompat,
     util,
 )
-from .utils import dateutil
+from .utils import (
+    dateutil,
+    hashutil,
+)
 
 parsers = policy.importmod('parsers')
 
@@ -1028,7 +1030,7 @@
 
 def makefoldid(relation, user):
 
-    folddigest = hashlib.sha1(user)
+    folddigest = hashutil.sha1(user)
     for p in relation[0] + relation[1]:
         folddigest.update(b'%d' % p.rev())
         folddigest.update(p.node())
--- a/mercurial/patch.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/patch.py	Mon Jan 13 17:15:14 2020 -0500
@@ -12,7 +12,6 @@
 import contextlib
 import copy
 import errno
-import hashlib
 import os
 import re
 import shutil
@@ -41,6 +40,7 @@
 )
 from .utils import (
     dateutil,
+    hashutil,
     procutil,
     stringutil,
 )
@@ -2943,7 +2943,7 @@
         if not text:
             text = b""
         l = len(text)
-        s = hashlib.sha1(b'blob %d\0' % l)
+        s = hashutil.sha1(b'blob %d\0' % l)
         s.update(text)
         return hex(s.digest())
 
--- a/mercurial/repair.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/repair.py	Mon Jan 13 17:15:14 2020 -0500
@@ -9,7 +9,6 @@
 from __future__ import absolute_import
 
 import errno
-import hashlib
 
 from .i18n import _
 from .node import (
@@ -29,7 +28,10 @@
     pycompat,
     util,
 )
-from .utils import stringutil
+from .utils import (
+    hashutil,
+    stringutil,
+)
 
 
 def backupbundle(
@@ -45,7 +47,7 @@
     # Include a hash of all the nodes in the filename for uniqueness
     allcommits = repo.set(b'%ln::%ln', bases, heads)
     allhashes = sorted(c.hex() for c in allcommits)
-    totalhash = hashlib.sha1(b''.join(allhashes)).digest()
+    totalhash = hashutil.sha1(b''.join(allhashes)).digest()
     name = b"%s/%s-%s-%s.hg" % (
         backupdir,
         short(node),
--- a/mercurial/revlogutils/sidedata.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/revlogutils/sidedata.py	Mon Jan 13 17:15:14 2020 -0500
@@ -33,10 +33,10 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import struct
 
 from .. import error
+from ..utils import hashutil
 
 ## sidedata type constant
 # reserve a block for testing purposes.
@@ -64,7 +64,7 @@
     sidedata.sort()
     rawtext = [SIDEDATA_HEADER.pack(len(sidedata))]
     for key, value in sidedata:
-        digest = hashlib.sha1(value).digest()
+        digest = hashutil.sha1(value).digest()
         rawtext.append(SIDEDATA_ENTRY.pack(key, len(value), digest))
     for key, value in sidedata:
         rawtext.append(value)
@@ -85,7 +85,7 @@
         # read the data associated with that entry
         nextdataoffset = dataoffset + size
         entrytext = text[dataoffset:nextdataoffset]
-        readdigest = hashlib.sha1(entrytext).digest()
+        readdigest = hashutil.sha1(entrytext).digest()
         if storeddigest != readdigest:
             raise error.SidedataHashError(key, storeddigest, readdigest)
         sidedata[key] = entrytext
--- a/mercurial/scmutil.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/scmutil.py	Mon Jan 13 17:15:14 2020 -0500
@@ -9,7 +9,6 @@
 
 import errno
 import glob
-import hashlib
 import os
 import posixpath
 import re
@@ -48,6 +47,7 @@
 )
 
 from .utils import (
+    hashutil,
     procutil,
     stringutil,
 )
@@ -366,7 +366,7 @@
     key = None
     revs = sorted(r for r in cl.filteredrevs if r <= maxrev)
     if revs:
-        s = hashlib.sha1()
+        s = hashutil.sha1()
         for rev in revs:
             s.update(b'%d;' % rev)
         key = s.digest()
--- a/mercurial/sparse.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/sparse.py	Mon Jan 13 17:15:14 2020 -0500
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import os
 
 from .i18n import _
@@ -24,6 +23,7 @@
     scmutil,
     util,
 )
+from .utils import hashutil
 
 # Whether sparse features are enabled. This variable is intended to be
 # temporary to facilitate porting sparse to core. It should eventually be
@@ -205,12 +205,12 @@
         tempsignature = b'0'
 
     if signature is None or (includetemp and tempsignature is None):
-        signature = hex(hashlib.sha1(repo.vfs.tryread(b'sparse')).digest())
+        signature = hex(hashutil.sha1(repo.vfs.tryread(b'sparse')).digest())
         cache[b'signature'] = signature
 
         if includetemp:
             raw = repo.vfs.tryread(b'tempsparse')
-            tempsignature = hex(hashlib.sha1(raw).digest())
+            tempsignature = hex(hashutil.sha1(raw).digest())
             cache[b'tempsignature'] = tempsignature
 
     return b'%s %s' % (signature, tempsignature)
--- a/mercurial/store.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/store.py	Mon Jan 13 17:15:14 2020 -0500
@@ -9,7 +9,6 @@
 
 import errno
 import functools
-import hashlib
 import os
 import stat
 
@@ -25,6 +24,7 @@
     util,
     vfs as vfsmod,
 )
+from .utils import hashutil
 
 parsers = policy.importmod('parsers')
 # how much bytes should be read from fncache in one read
@@ -273,7 +273,7 @@
 
 
 def _hashencode(path, dotencode):
-    digest = node.hex(hashlib.sha1(path).digest())
+    digest = node.hex(hashutil.sha1(path).digest())
     le = lowerencode(path[5:]).split(b'/')  # skips prefix 'data/' or 'meta/'
     parts = _auxencode(le, dotencode)
     basename = parts[-1]
--- a/mercurial/subrepo.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/subrepo.py	Mon Jan 13 17:15:14 2020 -0500
@@ -9,7 +9,6 @@
 
 import copy
 import errno
-import hashlib
 import os
 import re
 import stat
@@ -37,6 +36,7 @@
 )
 from .utils import (
     dateutil,
+    hashutil,
     procutil,
     stringutil,
 )
@@ -61,7 +61,7 @@
 
 def _getstorehashcachename(remotepath):
     '''get a unique filename for the store hash cache of a remote repository'''
-    return node.hex(hashlib.sha1(_expandedabspath(remotepath)).digest())[0:12]
+    return node.hex(hashutil.sha1(_expandedabspath(remotepath)).digest())[0:12]
 
 
 class SubrepoAbort(error.Abort):
@@ -514,7 +514,7 @@
         yield b'# %s\n' % _expandedabspath(remotepath)
         vfs = self._repo.vfs
         for relname in filelist:
-            filehash = node.hex(hashlib.sha1(vfs.tryread(relname)).digest())
+            filehash = node.hex(hashutil.sha1(vfs.tryread(relname)).digest())
             yield b'%s = %s\n' % (relname, filehash)
 
     @propertycache
--- a/mercurial/util.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/util.py	Mon Jan 13 17:15:14 2020 -0500
@@ -53,6 +53,7 @@
 )
 from .utils import (
     compression,
+    hashutil,
     procutil,
     stringutil,
 )
@@ -197,7 +198,7 @@
 
 DIGESTS = {
     b'md5': hashlib.md5,
-    b'sha1': hashlib.sha1,
+    b'sha1': hashutil.sha1,
     b'sha512': hashlib.sha512,
 }
 # List of digest types from strongest to weakest
--- a/mercurial/utils/storageutil.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/utils/storageutil.py	Mon Jan 13 17:15:14 2020 -0500
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import re
 import struct
 
@@ -24,8 +23,9 @@
     pycompat,
 )
 from ..interfaces import repository
+from ..utils import hashutil
 
-_nullhash = hashlib.sha1(nullid)
+_nullhash = hashutil.sha1(nullid)
 
 
 def hashrevisionsha1(text, p1, p2):
@@ -48,7 +48,7 @@
         else:
             a = p2
             b = p1
-        s = hashlib.sha1(a)
+        s = hashutil.sha1(a)
         s.update(b)
     s.update(text)
     return s.digest()
--- a/mercurial/wireprotov1peer.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/wireprotov1peer.py	Mon Jan 13 17:15:14 2020 -0500
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import hashlib
 import sys
 import weakref
 
@@ -31,6 +30,7 @@
     repository,
     util as interfaceutil,
 )
+from .utils import hashutil
 
 urlreq = util.urlreq
 
@@ -489,7 +489,7 @@
 
         if heads != [b'force'] and self.capable(b'unbundlehash'):
             heads = wireprototypes.encodelist(
-                [b'hashed', hashlib.sha1(b''.join(sorted(heads))).digest()]
+                [b'hashed', hashutil.sha1(b''.join(sorted(heads))).digest()]
             )
         else:
             heads = wireprototypes.encodelist(heads)
--- a/mercurial/wireprotov2server.py	Mon Jan 13 17:14:19 2020 -0500
+++ b/mercurial/wireprotov2server.py	Mon Jan 13 17:15:14 2020 -0500
@@ -8,7 +8,6 @@
 
 import collections
 import contextlib
-import hashlib
 
 from .i18n import _
 from .node import (
@@ -31,6 +30,7 @@
 from .interfaces import util as interfaceutil
 from .utils import (
     cborutil,
+    hashutil,
     stringutil,
 )
 
@@ -858,7 +858,7 @@
 
         cacher.adjustcachekeystate(state)
 
-        hasher = hashlib.sha1()
+        hasher = hashutil.sha1()
         for chunk in cborutil.streamencode(state):
             hasher.update(chunk)