Python hashlib.algorithms_guaranteed() Examples

The following are 21 code examples of hashlib.algorithms_guaranteed(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module hashlib , or try the search function .
Example #1
Source File: downloader_tools.py    From astroNN with MIT License 7 votes vote down vote up
def filehash(filename, block_size=65536, algorithm='sha256'):
    """
    Computes the hash value for a file by using a specified hash algorithm.

    :param filename: filename
    :type filename: str
    :param block_size: blocksize used to compute file hash
    :type block_size: int
    :param algorithm: hash algorithms like 'sha256' or 'md5' etc.
    :type algorithm: str
    :return: None
    :History: 2019-Mar-12 - Written - Henry Leung (University of Toronto)
    """
    algorithm = algorithm.lower()
    if algorithm not in hashlib.algorithms_guaranteed:
        raise ValueError(f"{algorithm} is an unsupported hashing algorithm")

    func_algorithm = getattr(hashlib, algorithm)()
    with open(filename, 'rb') as f:
        for block in iter(lambda: f.read(block_size), b''):
            func_algorithm.update(block)
    return func_algorithm.hexdigest() 
Example #2
Source File: hash.py    From koku with GNU Affero General Public License v3.0 6 votes vote down vote up
def set_hash_function(self, hash_function):
        """Set the hash function used.

        Args:
            hash_function (str): String representation of hash function
                Ex. 'md5'

        Returns:
            (hashlib hash function)

        """
        if "shake" in hash_function and not self.length:
            errmsg = f"{hash_function} requires length to be set"
            raise HasherError(errmsg)

        try:
            self._hash_function = getattr(hashlib, hash_function)
        except AttributeError:
            if hash_function in hashlib.algorithms_guaranteed:
                errmsg = f"{hash_function} needs Hasher implementation."
                raise HasherError(errmsg)
            errmsg = f"{hash_function} is not currently supported."
            raise HasherError(errmsg) 
Example #3
Source File: fileio.py    From sregistry-cli with Mozilla Public License 2.0 6 votes vote down vote up
def get_file_hash(image_path, algorithm="sha256"):
    """return an md5 hash of the file based on a criteria level. This
       is intended to give the file a reasonable version.
    
       Parameters
       ==========
       image_path: full path to the singularity image

    """
    try:
        hasher = getattr(hashlib, algorithm)()
    except AttributeError:
        bot.error("%s is an invalid algorithm.")
        bot.exit(" ".join(hashlib.algorithms_guaranteed))

    with open(image_path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hasher.update(chunk)
    return hasher.hexdigest()


################################################################################
## FILE OPERATIONS #############################################################
################################################################################ 
Example #4
Source File: hasher.py    From pyFileFixity with MIT License 6 votes vote down vote up
def hash(self, mes):
        # use hashlib.algorithms_guaranteed to list algorithms
        if self.algo == "md5":
            return hashlib.md5(mes).hexdigest()
        elif self.algo == "shortmd5": # from: http://www.peterbe.com/plog/best-hashing-function-in-python
            return hashlib.md5(mes).hexdigest().encode('base64')[:8]
        elif self.algo == "shortsha256":
            return hashlib.sha256(mes).hexdigest().encode('base64')[:8]
        elif self.algo == "minimd5":
            return hashlib.md5(mes).hexdigest().encode('base64')[:4]
        elif self.algo == "minisha256":
            return hashlib.sha256(mes).hexdigest().encode('base64')[:4]
        elif self.algo == "none":
            return ''
        else:
            raise NameError('Hashing algorithm %s is unknown!' % self.algo) 
Example #5
Source File: test_valve_config.py    From faucet with Apache License 2.0 5 votes vote down vote up
def test_config_hash_func(self):
        """Verify that faucet_config_hash_func is set correctly"""
        labels = self._get_info(metric=self.metrics.faucet_config_hash_func,
                                name='faucet_config_hash_func')
        hash_funcs = list(labels.values())
        self.assertEqual(len(hash_funcs), 1, "found multiple hash functions")
        hash_func = hash_funcs[0]
        # Make sure that it matches and is supported in hashlib
        self.assertEqual(hash_func, config_parser_util.CONFIG_HASH_FUNC)
        self.assertTrue(hash_func in hashlib.algorithms_guaranteed) 
Example #6
Source File: auth.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def sign_request(sign_method, request, secret_key) -> str:
    try:
        mac_type, hash_type = map(lambda s: s.lower(), sign_method.split('-'))
        assert mac_type == 'hmac', 'Unsupported request signing method (MAC type)'
        assert hash_type in hashlib.algorithms_guaranteed, \
               'Unsupported request signing method (hash type)'

        new_api_version = request.headers.get('X-BackendAI-Version')
        legacy_api_version = request.headers.get('X-Sorna-Version')
        api_version = new_api_version or legacy_api_version
        assert api_version is not None, 'API version missing in request headers'
        body = b''
        if api_version < 'v4.20181215':
            if (request.can_read_body and
                request.content_type != 'multipart/form-data'):
                # read the whole body if neither streaming nor bodyless
                body = await request.read()
        body_hash = hashlib.new(hash_type, body).hexdigest()

        sign_bytes = ('{0}\n{1}\n{2}\nhost:{3}\ncontent-type:{4}\n'
                      'x-{name}-version:{5}\n{6}').format(
            request.method, str(request.raw_path), request['raw_date'],
            request.host, request.content_type, api_version,
            body_hash,
            name='backendai' if new_api_version is not None else 'sorna'
        ).encode()
        sign_key = hmac.new(secret_key.encode(),
                            request['date'].strftime('%Y%m%d').encode(),
                            hash_type).digest()
        sign_key = hmac.new(sign_key, request.host.encode(), hash_type).digest()
        return hmac.new(sign_key, sign_bytes, hash_type).hexdigest()
    except ValueError:
        raise AuthorizationFailed('Invalid signature')
    except AssertionError as e:
        raise InvalidAuthParameters(e.args[0]) 
Example #7
Source File: dao.py    From forseti-security with Apache License 2.0 5 votes vote down vote up
def _create_violation_hash(violation_full_name, resource_data, violation_data):
    """Create a hash of violation data.

    Args:
        violation_full_name (str): The full name of the violation.
        resource_data (str): The inventory data.
        violation_data (dict): A violation.

    Returns:
        str: The resulting hex digest or '' if we can't successfully create
        a hash.
    """

    # TODO: Intelligently choose from hashlib.algorithms_guaranteed if our
    # desired one is not available.
    algorithm = 'sha512'

    try:
        violation_hash = hashlib.new(algorithm)
    except ValueError:
        LOGGER.exception('Cannot create hash for a violation with algorithm: '
                         '%s', algorithm)
        return ''

    try:
        # Group resources do not have full name.  Issue #1072
        violation_hash.update(
            json.dumps(violation_full_name).encode() +
            json.dumps(resource_data, sort_keys=True).encode() +
            json.dumps(violation_data, sort_keys=True).encode()
        )
    except TypeError:
        LOGGER.exception('Cannot create hash for a violation: %s',
                         violation_full_name)
        return ''

    return violation_hash.hexdigest() 
Example #8
Source File: test_hashlib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_algorithms_available(self):
        self.assertTrue(set(hashlib.algorithms_guaranteed).
                            issubset(hashlib.algorithms_available)) 
Example #9
Source File: test_hashlib.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_algorithms_guaranteed(self):
        self.assertEqual(hashlib.algorithms_guaranteed,
            set(_algo for _algo in self.supported_hash_names
                  if _algo.islower())) 
Example #10
Source File: hash_fields.py    From django-GDPR with MIT License 5 votes vote down vote up
def __init__(self, algorithm: str, *args, **kwargs):
        if algorithm not in hashlib.algorithms_guaranteed:
            raise RuntimeError(f'Hash algorithm {algorithm} is not supported by python hashlib.')
        self.algorithm = algorithm

        super().__init__(*args, **kwargs) 
Example #11
Source File: test_hashlib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_algorithms_available(self):
        self.assertTrue(set(hashlib.algorithms_guaranteed).
                            issubset(hashlib.algorithms_available)) 
Example #12
Source File: test_hashlib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_algorithms_guaranteed(self):
        self.assertEqual(hashlib.algorithms_guaranteed,
            set(_algo for _algo in self.supported_hash_names
                  if _algo.islower())) 
Example #13
Source File: test_android.py    From chaquopy with MIT License 5 votes vote down vote up
def test_hashlib(self):
        import hashlib
        INPUT = b"The quick brown fox jumps over the lazy dog"
        TESTS = [
            ("sha1", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"),
            ("sha3_512", ("01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f"
                          "07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450")),
            ("blake2b", ("a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f8240"
                         "1cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918")),
            ("ripemd160", "37f332f68db77bd9d7edd4969571ad671cf9dd3b"),  # OpenSSL-only
        ]
        for name, expected in TESTS:
            with self.subTest(algorithm=name):
                # With initial data
                self.assertEqual(expected, hashlib.new(name, INPUT).hexdigest())
                # Without initial data
                h = hashlib.new(name)
                h.update(INPUT)
                self.assertEqual(expected, h.hexdigest())

                if name in hashlib.algorithms_guaranteed:
                    # With initial data
                    self.assertEqual(expected, getattr(hashlib, name)(INPUT).hexdigest())
                    # Without initial data
                    h = getattr(hashlib, name)()
                    h.update(INPUT)
                    self.assertEqual(expected, h.hexdigest())
                else:
                    self.assertFalse(hasattr(hashlib, name)) 
Example #14
Source File: test_hashlib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_algorithms_available(self):
        self.assertTrue(set(hashlib.algorithms_guaranteed).
                            issubset(hashlib.algorithms_available)) 
Example #15
Source File: test_hashlib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_algorithms_guaranteed(self):
        self.assertEqual(hashlib.algorithms_guaranteed,
            set(_algo for _algo in self.supported_hash_names
                  if _algo.islower())) 
Example #16
Source File: test_hashlib.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_algorithms_available(self):
        self.assertTrue(set(hashlib.algorithms_guaranteed).
                            issubset(hashlib.algorithms_available)) 
Example #17
Source File: test_hashlib.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_algorithms_guaranteed(self):
        self.assertEqual(hashlib.algorithms_guaranteed,
            set(_algo for _algo in self.supported_hash_names
                  if _algo.islower())) 
Example #18
Source File: pre_fetch_maven_artifacts.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def process_by_url(self, url_requests):
        download_queue = []
        errors = []

        for url_request in url_requests:
            url = url_request['url']

            if self.allowed_domains:
                parsed_file_url = urlparse(url.lower())
                file_url = parsed_file_url.netloc + parsed_file_url.path
                if not any(file_url.startswith(prefix) for prefix in self.allowed_domains):
                    errors.append('File URL {} is not in list of allowed domains: {}'
                                  .format(file_url, self.allowed_domains))
                    continue

            checksums = {algo: url_request[algo] for algo in hashlib.algorithms_guaranteed
                         if algo in url_request}

            target = url_request.get('target', url.rsplit('/', 1)[-1])
            download_queue.append(DownloadRequest(url, target, checksums))

        if errors:
            raise ValueError('Errors found while processing {}: {}'
                             .format(self.URL_REQUESTS_FILENAME, ', '.join(errors)))

        return download_queue 
Example #19
Source File: test_hash.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        """Shared variables for hasher tests."""
        super().setUp()
        self.encoding = "utf-8"
        self.hash_function = random.choice(list(hashlib.algorithms_guaranteed))
        if "shake" in self.hash_function:
            self.hasher = Hasher(
                hash_function=self.hash_function, length=random.randint(8, 64), encoding=self.encoding
            )
        else:
            self.hasher = Hasher(hash_function=self.hash_function, encoding=self.encoding)
        self.string_to_hash = "".join([random.choice(string.ascii_letters) for _ in range(16)]) 
Example #20
Source File: test_hashlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_algorithms_available(self):
        self.assertTrue(set(hashlib.algorithms_guaranteed).
                            issubset(hashlib.algorithms_available)) 
Example #21
Source File: test_hashlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_algorithms_guaranteed(self):
        self.assertEqual(hashlib.algorithms_guaranteed,
            set(_algo for _algo in self.supported_hash_names
                  if _algo.islower()))