本文整理匯總了Python中hashlib.algorithms方法的典型用法代碼示例。如果您正苦於以下問題:Python hashlib.algorithms方法的具體用法?Python hashlib.algorithms怎麽用?Python hashlib.algorithms使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類hashlib
的用法示例。
在下文中一共展示了hashlib.algorithms方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: do_hashes
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def do_hashes(self,line):
try:
l = line.split(" ")
if (l[0] == ""):
self.help_hashes()
else:
id = int(l[0])
body, sz = CTCore.get_response_and_size(id, "all")
name = CTCore.get_name(id)
print " Hashes of object {} ({}):".format(str(id),name) + newLine
for alg in hashlib.algorithms:
hashfunc = getattr(hashlib, alg)
hash = hashfunc(StringIO.StringIO(body).getvalue()).hexdigest()
print " {0:8} : {1}".format(alg, hash)
print ""
except Exception,e:
print str(e)
示例2: to_native
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def to_native(self, value, context=None):
value = super(HashType, self).to_native(value, context)
if ':' not in value:
raise ValidationError(self.messages['hash_invalid'])
hash_type, hash_value = value.split(':', 1)
if hash_type not in algorithms:
raise ValidationError(self.messages['hash_invalid'])
if len(hash_value) != hash_new(hash_type).digest_size * 2:
raise ValidationError(self.messages['hash_length'])
try:
int(hash_value, 16)
except ValueError:
raise ConversionError(self.messages['hash_hex'])
return value
示例3: test_algorithms_attribute
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def test_algorithms_attribute(self):
self.assertEqual(hashlib.algorithms,
tuple([_algo for _algo in self.supported_hash_names if
_algo.islower()]))
示例4: _sync_remote_entries_with_existing_fetch
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def _sync_remote_entries_with_existing_fetch(self):
payload_entries = self.payload_entries()
for url, length, filename in self.fetch_entries():
entry_path = os.path.normpath(filename.lstrip("*"))
if entry_path in payload_entries:
for alg in self.algorithms:
digest = payload_entries[entry_path].get(alg, None)
remote_entry = self.remote_entries.get(filename)
if remote_entry:
continue
self.add_remote_file(filename, url, length, alg, digest)
示例5: add_remote_file
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def add_remote_file(self, filename, url, length, alg, digest):
if alg not in self.algorithms:
self.algorithms.append(alg)
make_remote_file_entry(self.remote_entries, filename, url, length, alg, digest)
示例6: crack
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def crack(hashstr, wordlist):
for word in wordlist:
for hashtype in hashlib.algorithms:
func = getattr(hashlib, hashtype)
if func(word).hexdigest().lower() == hashstr.lower():
return word, hashtype
return None, None
示例7: __init__
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def __init__(self, *args, **kwargs):
algorithms = set()
for algorithm in self.supported_hash_names:
algorithms.add(algorithm.lower())
self.constructors_to_test = {}
for algorithm in algorithms:
self.constructors_to_test[algorithm] = set()
# For each algorithm, test the direct constructor and the use
# of hashlib.new given the algorithm name.
for algorithm, constructors in self.constructors_to_test.items():
constructors.add(getattr(hashlib, algorithm))
def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
if data is None:
return hashlib.new(_alg)
return hashlib.new(_alg, data)
constructors.add(_test_algorithm_via_hashlib_new)
_hashlib = self._conditional_import_module('_hashlib')
if _hashlib:
# These two algorithms should always be present when this module
# is compiled. If not, something was compiled wrong.
assert hasattr(_hashlib, 'openssl_md5')
assert hasattr(_hashlib, 'openssl_sha1')
for algorithm, constructors in self.constructors_to_test.items():
constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
if constructor:
constructors.add(constructor)
_md5 = self._conditional_import_module('_md5')
if _md5:
self.constructors_to_test['md5'].add(_md5.new)
_sha = self._conditional_import_module('_sha')
if _sha:
self.constructors_to_test['sha1'].add(_sha.new)
_sha256 = self._conditional_import_module('_sha256')
if _sha256:
self.constructors_to_test['sha224'].add(_sha256.sha224)
self.constructors_to_test['sha256'].add(_sha256.sha256)
_sha512 = self._conditional_import_module('_sha512')
if _sha512:
self.constructors_to_test['sha384'].add(_sha512.sha384)
self.constructors_to_test['sha512'].add(_sha512.sha512)
super(HashLibTestCase, self).__init__(*args, **kwargs)
示例8: _validate_entries
# 需要導入模塊: import hashlib [as 別名]
# 或者: from hashlib import algorithms [as 別名]
def _validate_entries(self, processes, callback=None):
"""
Verify that the actual file contents match the recorded hashes stored in the manifest files
"""
errors = list()
if os.name == 'posix':
worker_init = posix_multiprocessing_worker_initializer
else:
worker_init = None
args = ((self.path,
self.normalized_filesystem_names.get(rel_path, rel_path),
hashes,
self.algorithms) for rel_path, hashes in self.entries.items())
try:
if processes == 1:
count = 0
hash_results = []
totalHashes = len(self.entries.items())
for i in args:
hash_results.append(_calc_hashes(i))
count += 1
if callback:
if not callback(count, totalHashes):
raise BaggingInterruptedError("Bag validation interrupted!")
else: # pragma: no cover
pool = None
try:
pool = multiprocessing.Pool(processes if processes else None, initializer=worker_init)
hash_results = pool.map(_calc_hashes, args)
finally:
if pool:
pool.terminate()
except BaggingInterruptedError:
raise
# Any unhandled exceptions are probably fatal
except:
LOGGER.error(_("Unable to calculate file hashes for %s"), self)
raise
for rel_path, f_hashes, hashes in hash_results:
for alg, computed_hash in f_hashes.items():
stored_hash = hashes[alg]
if stored_hash.lower() != computed_hash:
e = ChecksumMismatch(rel_path, alg, stored_hash.lower(), computed_hash)
LOGGER.warning(force_unicode(e))
errors.append(e)
if errors:
raise BagValidationError(_("Bag validation failed"), errors)