def sizehashes(f): size = os.fstat(f.fileno())[6] f.seek(0) sha1sum = apt_pkg.sha1sum(f) f.seek(0) sha256sum = apt_pkg.sha256sum(f) return (size, sha1sum, sha256sum)
def check(self, directory): """Validate hashes Check if size and hashes match the expected value. @type directory: str @param directory: directory the file is located in @raise InvalidHashException: hash mismatch """ path = os.path.join(directory, self.filename) fh = open(path, 'r') size = os.stat(path).st_size if size != self.size: raise InvalidHashException(self.filename, 'size', self.size, size) md5sum = apt_pkg.md5sum(fh) if md5sum != self.md5sum: raise InvalidHashException(self.filename, 'md5sum', self.md5sum, md5sum) fh.seek(0) sha1sum = apt_pkg.sha1sum(fh) if sha1sum != self.sha1sum: raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, sha1sum) fh.seek(0) sha256sum = apt_pkg.sha256sum(fh) if sha256sum != self.sha256sum: raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, sha256sum)
def testSHA1(self): # simple s = b"foo" s_hash = "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" res = apt_pkg.sha1sum(s) self.assertEqual(res, s_hash) # file with open(self.DATA_PATH) as fobj: self.assertEqual(apt_pkg.sha1sum(fobj), s_hash) # with zero (\0) in the string s = b"foo\0bar" s_hash = "e2c300a39311a2dfcaff799528415cb74c19317f" res = apt_pkg.sha1sum(s) self.assertEqual(res, s_hash) # file with open(self.DATA_WITH_ZERO_PATH) as fobj: self.assertEqual(apt_pkg.sha1sum(fobj), s_hash)
def testSHA1(self): # simple s = "foo" s_hash = "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" res = apt_pkg.sha1sum(s) self.assert_(res == s_hash) # file res = apt_pkg.sha1sum(open("hashsum_test.data")) self.assert_(res == s_hash) # with zero (\0) in the string s = "foo\0bar" s_hash = "e2c300a39311a2dfcaff799528415cb74c19317f" res = apt_pkg.sha1sum(s) self.assert_(res == s_hash) # file res = apt_pkg.sha1sum(open("hashsum_test_with_zero.data")) self.assert_(res == s_hash)
def fix_checksums(): """ Update missing checksums """ print "Getting file information from database..." session = DBConn().session() q = session.query(PoolFile) print "Checking file checksums & sizes..." for f in q: filename = f.fullpath try: fi = utils.open_file(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn( "**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn( "**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) continue fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if f.sha1sum is None: f.sha1sum = sha1sum print "Added missing sha1 checksum for {0}".format(f.filename) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if f.sha256sum is None: f.sha256sum = sha256sum print "Added missing sha256 checksum for {0}".format(f.filename) session.commit() print "Done."
def check_checksums(): """ Validate all files """ print("Getting file information from database...") q = DBConn().session().query(PoolFile) print("Checking file checksums & sizes...") for f in q: filename = f.fullpath try: fi = open(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn( "**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn( "**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if sha1sum != f.sha1sum: utils.warn( "**WARNING** sha1sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha1sum, f.sha1sum)) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if sha256sum != f.sha256sum: utils.warn( "**WARNING** sha256sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha256sum, f.sha256sum)) fi.close() print("Done.")
def fix_checksums(): """ Update missing checksums """ print "Getting file information from database..." session = DBConn().session(); q = session.query(PoolFile) print "Checking file checksums & sizes..." for f in q: filename = f.fullpath try: fi = utils.open_file(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) continue; fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if f.sha1sum is None: f.sha1sum = sha1sum print "Added missing sha1 checksum for {0}".format(f.filename) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if f.sha256sum is None: f.sha256sum = sha256sum print "Added missing sha256 checksum for {0}".format(f.filename) session.commit() print "Done."
def check_checksums(): """ Validate all files """ print "Getting file information from database..." q = DBConn().session().query(PoolFile) print "Checking file checksums & sizes..." for f in q: filename = f.fullpath try: fi = utils.open_file(filename) except: utils.warn("can't open '%s'." % (filename)) continue size = os.stat(filename)[stat.ST_SIZE] if size != f.filesize: utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize)) md5sum = apt_pkg.md5sum(fi) if md5sum != f.md5sum: utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum)) fi.seek(0) sha1sum = apt_pkg.sha1sum(fi) if sha1sum != f.sha1sum: utils.warn("**WARNING** sha1sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha1sum, f.sha1sum)) fi.seek(0) sha256sum = apt_pkg.sha256sum(fi) if sha256sum != f.sha256sum: utils.warn("**WARNING** sha256sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha256sum, f.sha256sum)) print "Done."
def contents_sha1(self): return apt_pkg.sha1sum(self.contents)
def signB64sha1(self, data): s1 = sha1sum(data) signature = self.privkey.sign(s1, "sha1") return base64.b64encode(str(signature[0]))
def test_sha1sum(self): """hashes: Test apt_pkg.sha1sum()""" self.assertEqual(apt_pkg.sha1sum(self.value), self.sha1) self.assertEqual(apt_pkg.sha1sum(self.file), self.sha1)
def __init__(self, session=None): cnf = Config() try: newq = get_policy_queue('new', session) for changes_fn in glob.glob(newq.path + "/*.changes"): changes_bn = os.path.basename(changes_fn) chg = get_dbchange(changes_bn, session) u = Upload() success = u.load_changes(changes_fn) u.pkg.changes_file = changes_bn u.check_hashes() if not chg: chg = u.pkg.add_known_changes(newq.path, newq.policy_queue_id, session) session.add(chg) if not success: log.critical("failed to load %s" % changes_fn) sys.exit(1) else: log.critical("ACCLAIM: %s" % changes_fn) files=[] for chg_fn in u.pkg.files.keys(): try: f = open(os.path.join(newq.path, chg_fn)) cpf = ChangePendingFile() cpf.filename = chg_fn cpf.size = u.pkg.files[chg_fn]['size'] cpf.md5sum = u.pkg.files[chg_fn]['md5sum'] if u.pkg.files[chg_fn].has_key('sha1sum'): cpf.sha1sum = u.pkg.files[chg_fn]['sha1sum'] else: log.warning("Having to generate sha1sum for %s" % chg_fn) f.seek(0) cpf.sha1sum = apt_pkg.sha1sum(f) if u.pkg.files[chg_fn].has_key('sha256sum'): cpf.sha256sum = u.pkg.files[chg_fn]['sha256sum'] else: log.warning("Having to generate sha256sum for %s" % chg_fn) f.seek(0) cpf.sha256sum = apt_pkg.sha256sum(f) session.add(cpf) files.append(cpf) f.close() except IOError: # Can't find the file, try to look it up in the pool poolname = poolify(u.pkg.changes["source"], u.pkg.files[chg_fn]["component"]) l = get_location(cnf["Dir::Pool"], u.pkg.files[chg_fn]["component"], session=session) if not l: log.critical("ERROR: Can't find location for %s (component %s)" % (chg_fn, u.pkg.files[chg_fn]["component"])) found, poolfile = check_poolfile(os.path.join(poolname, chg_fn), u.pkg.files[chg_fn]['size'], u.pkg.files[chg_fn]["md5sum"], l.location_id, session=session) if found is None: log.critical("ERROR: Found multiple files for %s in pool" % chg_fn) sys.exit(1) elif found is False and poolfile is not None: log.critical("ERROR: md5sum / size mismatch for %s in pool" % chg_fn) sys.exit(1) else: if poolfile is None: log.critical("ERROR: Could not find %s in pool" % chg_fn) sys.exit(1) else: chg.poolfiles.append(poolfile) chg.files = files session.commit() except KeyboardInterrupt: print("Caught C-c; terminating.") utils.warn("Caught C-c; terminating.") self.plsDie()
def sizesha1(f): size = os.fstat(f.fileno())[6] f.seek(0) sha1sum = apt_pkg.sha1sum(f) return (sha1sum, size)
def verifyB4sha1(self, data, signature): sig = base64.b64decode(signature) sh1 = sha1sum(str(data)) return self.pubkey.verify(sh1, [long(sig)])