Skip to content

Commit

Permalink
Add support for GNU coreutils sha224sum, sha256sum, sha384sum and sha…
Browse files Browse the repository at this point in the history
…512sum formats
  • Loading branch information
lxp committed Mar 13, 2018
1 parent aa4f620 commit 5a14438
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
72 changes: 64 additions & 8 deletions lib/cfv/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,6 @@ def filename_ok(fn):
return len((fn+'a').splitlines())==1


#---------- sha1sum ----------

class SHA1_MixIn:
def do_test_file(self, filename, filecrc):
c=getfilesha1(filename)[0]
if c!=filecrc:
return c

# Base class for md5sum/sha1sum style checksum file formats.
class FooSum_Base(TextChksumType):
def do_test_chksumfile_print_testingline(self, file):
Expand All @@ -688,6 +680,70 @@ def do_test_chksumline(self, l):
stats.textmode += 1
self.test_file(x.group(3),unhexlify(x.group(1)))

def gnu_sum(algo):
hasher, digestlen = hash.getfilechecksumgeneric(algo)
hexlen = digestlen * 2

class GnuSum_Base(FooSum_Base):
name = algo
description = 'GNU %ssum' % algo
descinfo = '%s,name' % algo

def do_test_file(self, filename, filecrc):
c = getfilehash(filename, algo, hasher)[0]
if c != filecrc:
return c

@staticmethod
def auto_chksumfile_match(file, _autorem=re.compile(r'[0-9a-fA-F]{%d} [ *].' % hexlen)):
l = file.peekline(4096)
while l:
if l[0] not in ';#':
return _autorem.match(l) is not None
l = file.peeknextline(4096)

auto_filename_match = algo

_foosum_rem = re.compile(r'([0-9a-fA-F]{%s}) ([ *])([^\r\n]+)[\r\n]*$' % hexlen)

@staticmethod
def make_std_filename(filename):
return '%s.%s' % (filename, algo)

def make_addfile(self, filename):
crc = hexlify(getfilehash(filename, algo, hasher)[0])
return (crc, -1), '%s *%s' % (crc, filename) + os.linesep

return GnuSum_Base

try:
cftypes.register_cftype(gnu_sum('sha512'))
except:
pass

try:
cftypes.register_cftype(gnu_sum('sha384'))
except:
pass

try:
cftypes.register_cftype(gnu_sum('sha256'))
except:
pass

try:
cftypes.register_cftype(gnu_sum('sha224'))
except:
pass


#---------- sha1sum ----------

class SHA1_MixIn:
def do_test_file(self, filename, filecrc):
c=getfilesha1(filename)[0]
if c!=filecrc:
return c

class SHA1(FooSum_Base, SHA1_MixIn):
name = 'sha1'
Expand Down
8 changes: 8 additions & 0 deletions lib/cfv/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ def finish(m,s):
except ImportError:
from md5 import new as md5_new

def getfilechecksumgeneric(algo):
import hashlib
if hasattr(hashlib, algo):
hasher = getattr(hashlib, algo)
else:
hasher = lambda: hashlib.new(algo)
return lambda filename, callback: _getfilechecksum(filename, hasher, callback), hasher().digest_size

def getfilesha1(filename, callback):
return _getfilechecksum(filename, sha_new, callback)

Expand Down

0 comments on commit 5a14438

Please sign in to comment.