Commit 2d49482d0dd by Augie Fackler

hgext: replace references to hashlib.sha1 with hashutil.sha1

When in a non-pure build of Mercurial, this will provide protections
against SHA1 collision attacks.

Differential Revision: https://phab.mercurial-scm.org/D7851
parent cbc5755df6b
......@@ -9,7 +9,6 @@ from __future__ import absolute_import
import collections
import contextlib
import hashlib
import os
from mercurial.i18n import _
......@@ -28,7 +27,10 @@ from mercurial import (
scmutil,
util,
)
from mercurial.utils import stringutil
from mercurial.utils import (
hashutil,
stringutil,
)
from . import (
error as faerror,
......@@ -148,7 +150,7 @@ def hashdiffopts(diffopts):
diffoptstr = stringutil.pprint(
sorted((k, getattr(diffopts, k)) for k in mdiff.diffopts.defaults)
)
return node.hex(hashlib.sha1(diffoptstr).digest())[:6]
return node.hex(hashutil.sha1(diffoptstr).digest())[:6]
_defaultdiffopthash = hashdiffopts(mdiff.defaultopts)
......
......@@ -108,7 +108,6 @@ created.
from __future__ import absolute_import
import codecs
import hashlib
import os
import stat
import sys
......@@ -132,7 +131,10 @@ from mercurial import (
util,
)
from mercurial import match as matchmod
from mercurial.utils import stringutil
from mercurial.utils import (
hashutil,
stringutil,
)
from . import (
pywatchman,
......@@ -235,7 +237,7 @@ def _hashignore(ignore):
copy.
"""
sha1 = hashlib.sha1()
sha1 = hashutil.sha1()
sha1.update(pycompat.byterepr(ignore))
return pycompat.sysbytes(sha1.hexdigest())
......
......@@ -6,7 +6,6 @@
from __future__ import absolute_import
import abc
import hashlib
import os
import subprocess
import tempfile
......@@ -16,7 +15,10 @@ from mercurial import (
node,
pycompat,
)
from mercurial.utils import procutil
from mercurial.utils import (
hashutil,
procutil,
)
NamedTemporaryFile = tempfile.NamedTemporaryFile
......@@ -87,7 +89,7 @@ class filebundlestore(object):
return os.path.join(self._dirpath(filename), filename)
def write(self, data):
filename = node.hex(hashlib.sha1(data).digest())
filename = node.hex(hashutil.sha1(data).digest())
dirpath = self._dirpath(filename)
if not os.path.exists(dirpath):
......
......@@ -10,7 +10,6 @@
from __future__ import absolute_import
import errno
import hashlib
import os
import shutil
......@@ -29,6 +28,7 @@ from mercurial import (
scmutil,
util,
)
from mercurial.utils import hashutil
from ..convert import (
convcmd,
......@@ -273,7 +273,7 @@ def _lfconvert_addchangeset(
)
# largefile was modified, update standins
m = hashlib.sha1(b'')
m = hashutil.sha1(b'')
m.update(ctx[f].data())
hash = node.hex(m.digest())
if f not in lfiletohash or lfiletohash[f] != hash:
......
......@@ -11,7 +11,6 @@ from __future__ import absolute_import
import contextlib
import copy
import hashlib
import os
import stat
......@@ -32,6 +31,7 @@ from mercurial import (
util,
vfs as vfsmod,
)
from mercurial.utils import hashutil
shortname = b'.hglf'
shortnameslash = shortname + b'/'
......@@ -432,7 +432,7 @@ def writestandin(repo, standin, hash, executable):
def copyandhash(instream, outfile):
'''Read bytes from instream (iterable) and write them to outfile,
computing the SHA-1 hash of the data along the way. Return the hash.'''
hasher = hashlib.sha1(b'')
hasher = hashutil.sha1(b'')
for data in instream:
hasher.update(data)
outfile.write(data)
......@@ -472,7 +472,7 @@ def urljoin(first, second, *arg):
def hexsha1(fileobj):
"""hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
object data"""
h = hashlib.sha1()
h = hashutil.sha1()
for chunk in util.filechunkiter(fileobj):
h.update(chunk)
return hex(h.digest())
......
......@@ -2,7 +2,6 @@ from __future__ import absolute_import
import collections
import errno
import hashlib
import mmap
import os
import struct
......@@ -20,6 +19,7 @@ from mercurial import (
util,
vfs as vfsmod,
)
from mercurial.utils import hashutil
from . import shallowutil
osutil = policy.importmod('osutil')
......@@ -392,7 +392,7 @@ class mutablebasepack(versionmixin):
)
self.packfp = os.fdopen(self.packfp, 'wb+')
self.idxfp = os.fdopen(self.idxfp, 'wb+')
self.sha = hashlib.sha1()
self.sha = hashutil.sha1()
self._closed = False
# The opener provides no way of doing permission fixup on files created
......
from __future__ import absolute_import
import errno
import hashlib
import os
import shutil
import stat
......@@ -15,6 +14,7 @@ from mercurial import (
pycompat,
util,
)
from mercurial.utils import hashutil
from . import (
constants,
shallowutil,
......@@ -166,7 +166,7 @@ class basestore(object):
# Start with a full manifest, since it'll cover the majority of files
for filename in self.repo[b'tip'].manifest():
sha = hashlib.sha1(filename).digest()
sha = hashutil.sha1(filename).digest()
if sha in missingfilename:
filenames[filename] = sha
missingfilename.discard(sha)
......@@ -178,7 +178,7 @@ class basestore(object):
break
files = cl.readfiles(cl.node(rev))
for filename in files:
sha = hashlib.sha1(filename).digest()
sha = hashutil.sha1(filename).digest()
if sha in missingfilename:
filenames[filename] = sha
missingfilename.discard(sha)
......
......@@ -6,7 +6,6 @@
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
import hashlib
import os
import zlib
......@@ -21,6 +20,7 @@ from mercurial import (
pycompat,
revlog,
)
from mercurial.utils import hashutil
from . import (
constants,
datapack,
......@@ -61,7 +61,7 @@ def debugremotefilelog(ui, path, **opts):
def buildtemprevlog(repo, file):
# get filename key
filekey = nodemod.hex(hashlib.sha1(file).digest())
filekey = nodemod.hex(hashutil.sha1(file).digest())
filedir = os.path.join(repo.path, b'store/data', filekey)
# sort all entries based on linkrev
......@@ -421,7 +421,7 @@ def dumpdeltachain(ui, deltachain, **opts):
% (
hashformatter(node),
hashformatter(deltabasenode),
nodemod.hex(hashlib.sha1(delta).digest()),
nodemod.hex(hashutil.sha1(delta).digest()),
len(delta),
)
)
......
......@@ -7,7 +7,6 @@
from __future__ import absolute_import
import hashlib
import io
import os
import threading
......@@ -25,7 +24,10 @@ from mercurial import (
util,
wireprotov1peer,
)
from mercurial.utils import procutil
from mercurial.utils import (
hashutil,
procutil,
)
from . import (
constants,
......@@ -45,12 +47,12 @@ _lfsmod = None
def getcachekey(reponame, file, id):
pathhash = node.hex(hashlib.sha1(file).digest())
pathhash = node.hex(hashutil.sha1(file).digest())
return os.path.join(reponame, pathhash[:2], pathhash[2:], id)
def getlocalkey(file, id):
pathhash = node.hex(hashlib.sha1(file).digest())
pathhash = node.hex(hashutil.sha1(file).digest())
return os.path.join(pathhash, id)
......
from __future__ import absolute_import
import hashlib
import struct
from mercurial.node import hex, nullid
......@@ -8,6 +7,7 @@ from mercurial import (
pycompat,
util,
)
from mercurial.utils import hashutil
from . import (
basepack,
constants,
......@@ -197,7 +197,7 @@ class historypack(basepack.basepack):
def _findsection(self, name):
params = self.params
namehash = hashlib.sha1(name).digest()
namehash = hashutil.sha1(name).digest()
fanoutkey = struct.unpack(
params.fanoutstruct, namehash[: params.fanoutprefix]
)[0]
......@@ -499,7 +499,7 @@ class mutablehistorypack(basepack.mutablebasepack):
# Record metadata for the index
self.files[filename] = (sectionstart, sectionlen)
node = hashlib.sha1(filename).digest()
node = hashutil.sha1(filename).digest()
self.entries[node] = node
def close(self, ledger=None):
......@@ -517,7 +517,7 @@ class mutablehistorypack(basepack.mutablebasepack):
nodeindexlength = self.NODEINDEXENTRYLENGTH
files = (
(hashlib.sha1(filename).digest(), filename, offset, size)
(hashutil.sha1(filename).digest(), filename, offset, size)
for filename, (offset, size) in pycompat.iteritems(self.files)
)
files = sorted(files)
......
......@@ -8,7 +8,6 @@ from __future__ import absolute_import
import collections
import errno
import hashlib
import os
import stat
import struct
......@@ -24,6 +23,7 @@ from mercurial import (
util,
)
from mercurial.utils import (
hashutil,
storageutil,
stringutil,
)
......@@ -39,12 +39,12 @@ def isenabled(repo):
def getcachekey(reponame, file, id):
pathhash = node.hex(hashlib.sha1(file).digest())
pathhash = node.hex(hashutil.sha1(file).digest())
return os.path.join(reponame, pathhash[:2], pathhash[2:], id)
def getlocalkey(file, id):
pathhash = node.hex(hashlib.sha1(file).digest())
pathhash = node.hex(hashutil.sha1(file).digest())
return os.path.join(pathhash, id)
......
......@@ -45,7 +45,6 @@ option to ``sqlite`` to enable new repositories to use SQLite for storage.
from __future__ import absolute_import
import hashlib
import sqlite3
import struct
import threading
......@@ -75,7 +74,10 @@ from mercurial.interfaces import (
repository,
util as interfaceutil,
)
from mercurial.utils import storageutil
from mercurial.utils import (
hashutil,
storageutil,
)
try:
from mercurial import zstd
......@@ -807,7 +809,7 @@ class sqlitefilestore(object):
self._db, pathid, node, {}, {-1: None}, zstddctx=self._dctx
)
deltahash = hashlib.sha1(fulltext).digest()
deltahash = hashutil.sha1(fulltext).digest()
if self._compengine == b'zstd':
deltablob = self._cctx.compress(fulltext)
......@@ -837,7 +839,7 @@ class sqlitefilestore(object):
# Now create the tombstone delta and replace the delta on the censored
# node.
deltahash = hashlib.sha1(tombstone).digest()
deltahash = hashutil.sha1(tombstone).digest()
tombstonedeltaid = insertdelta(
self._db, COMPRESSION_NONE, deltahash, tombstone
)
......@@ -1004,7 +1006,7 @@ class sqlitefilestore(object):
# us to de-duplicate. The table is configured to ignore conflicts
# and it is faster to just insert and silently noop than to look
# first.
deltahash = hashlib.sha1(delta).digest()
deltahash = hashutil.sha1(delta).digest()
if self._compengine == b'zstd':
deltablob = self._cctx.compress(delta)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment