Python hashlib.sha1() Examples

The following are code examples for showing how to use hashlib.sha1(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyblish-win   Author: pyblish   File: test_unicodedata.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_function_checksum(self):
        data = []
        h = hashlib.sha1()

        for i in range(0x10000):
            char = unichr(i)
            data = [
                # Properties
                str(self.db.digit(char, -1)),
                str(self.db.numeric(char, -1)),
                str(self.db.decimal(char, -1)),
                self.db.category(char),
                self.db.bidirectional(char),
                self.db.decomposition(char),
                str(self.db.mirrored(char)),
                str(self.db.combining(char)),
            ]
            h.update(''.join(data))
        result = h.hexdigest()
        self.assertEqual(result, self.expectedchecksum) 
Example 2
Project: pyblish-win   Author: pyblish   File: test_hmac.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_legacy_block_size_warnings(self):
        class MockCrazyHash(object):
            """Ain't no block_size attribute here."""
            def __init__(self, *args):
                self._x = hashlib.sha1(*args)
                self.digest_size = self._x.digest_size
            def update(self, v):
                self._x.update(v)
            def digest(self):
                return self._x.digest()

        with warnings.catch_warnings():
            warnings.simplefilter('error', RuntimeWarning)
            with self.assertRaises(RuntimeWarning):
                hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
                self.fail('Expected warning about missing block_size')

            MockCrazyHash.block_size = 1
            with self.assertRaises(RuntimeWarning):
                hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
                self.fail('Expected warning about small block_size') 
Example 3
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 6 votes vote down vote up
def pbkdf2_hex(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Like :func:`pbkdf2_bin`, but returns a hex-encoded string.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided,
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function, or a function
                     from the hashlib module.  Defaults to sha1.
    """
    rv = pbkdf2_bin(data, salt, iterations, keylen, hashfunc)
    return to_native(codecs.encode(rv, 'hex_codec')) 
Example 4
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 6 votes vote down vote up
def pbkdf2_hex(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Like :func:`pbkdf2_bin`, but returns a hex-encoded string.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided,
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function, or a function
                     from the hashlib module.  Defaults to sha1.
    """
    rv = pbkdf2_bin(data, salt, iterations, keylen, hashfunc)
    return to_native(codecs.encode(rv, 'hex_codec')) 
Example 5
Project: wikilinks   Author: trovdimi   File: decorators.py    MIT License 6 votes vote down vote up
def __call__(self, *args, **kwargs):
        fkey = args[0].__class__.__name__ + '_' + self.func.__name__
        self.filepath = os.path.join(CACHE_FOLDER, fkey + '.obj')

        if self.cache is None:
            if os.path.exists(self.filepath):
                print('loading', self.filepath, '...')
                with open(self.filepath, 'rb') as infile:
                    self.cache = pickle.load(infile)
            else:
                self.cache = {}

        pickled = pickle.dumps(args[1:], -1) + pickle.dumps(kwargs, -1)
        key = hashlib.sha1(pickled).hexdigest()
        try:
            return self.cache[key]
        except KeyError:
            # print(fkey, 'key not found...')
            self.modified = True
            value = self.func(*args, **kwargs)
            self.cache[key] = value
            return value 
Example 6
Project: annots   Author: Infernion   File: _utils.py    MIT License 6 votes vote down vote up
def _annotations_to_init(cls):
    """
    Add a variables from __annotations__ to __init__ method.
    """
    annotations = getattr(cls, '__annotations__', {})

    sha1 = hashlib.sha1()
    sha1.update(repr(annotations).encode("utf-8"))
    unique_filename = "<annots generated init {0}>".format(
        sha1.hexdigest()
    )

    script = _annotations_to_script(
        annotations,
        getattr(cls, "__annots_post_init__", False),
    )

    locs = {}
    bytecode = compile(script, unique_filename, "exec")
    eval(bytecode, None, locs)
    return locs["__init__"] 
Example 7
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: keys.py    MIT License 6 votes vote down vote up
def hash_algo(self):
        """
        Returns the name of the family of hash algorithms used to generate a
        DSA key

        :raises:
            ValueError - when the key is not a DSA key

        :return:
            A unicode string of "sha1" or "sha2"
        """

        if self.algorithm != 'dsa':
            raise ValueError(unwrap(
                '''
                Only DSA keys are generated using a hash algorithm, this key is
                %s
                ''',
                self.algorithm.upper()
            ))

        byte_len = math.log(self['private_key_algorithm']['parameters']['q'].native, 2) / 8

        return 'sha1' if byte_len <= 20 else 'sha2' 
Example 8
Project: esp-sdk-python   Author: EvidentSecurity   File: api_authentication.py    MIT License 6 votes vote down vote up
def sign_request(self):
        """
        :return: Sign request takes pieces of information about the request and
        generates a hmac hash digest for authentication. This will return a
        base64 string of the digest
        :rtype: str
        """
        url = urlparse(self.url)
        uri = url.path
        if url.query != '':
            uri += '?{}'.format(url.query)
        canonical = '{method},{content_type},{md5},{uri},{date}'.format(
            method=self.method.upper(),
            content_type=CONTENT_TYPE,
            md5=self.body_md5(),
            uri=uri,
            date=self.date)
        digest = hmac.new(self.config.secret_access_key.encode('utf-8'),
                          canonical.encode('utf-8'),
                          digestmod=hashlib.sha1).digest()
        return 'APIAuth {access_key}:{signature}'.format(
            access_key=self.config.access_key_id,
            signature=base64.b64encode(digest).decode()) 
Example 9
Project: flasky   Author: RoseOu   File: session.py    MIT License 6 votes vote down vote up
def generate_csrf_token(self, csrf_token_field):
        meta = self.form_meta
        if meta.csrf_secret is None:
            raise Exception('must set `csrf_secret` on class Meta for SessionCSRF to work')
        if meta.csrf_context is None:
            raise TypeError('Must provide a session-like object as csrf context')

        session = self.session

        if 'csrf' not in session:
            session['csrf'] = sha1(os.urandom(64)).hexdigest()

        if self.time_limit:
            expires = (self.now() + self.time_limit).strftime(self.TIME_FORMAT)
            csrf_build = '%s%s' % (session['csrf'], expires)
        else:
            expires = ''
            csrf_build = session['csrf']

        hmac_csrf = hmac.new(meta.csrf_secret, csrf_build.encode('utf8'), digestmod=sha1)
        return '%s##%s' % (expires, hmac_csrf.hexdigest()) 
Example 10
Project: flasky   Author: RoseOu   File: session.py    MIT License 6 votes vote down vote up
def validate_csrf_token(self, form, field):
        meta = self.form_meta
        if not field.data or '##' not in field.data:
            raise ValidationError(field.gettext('CSRF token missing'))

        expires, hmac_csrf = field.data.split('##', 1)

        check_val = (self.session['csrf'] + expires).encode('utf8')

        hmac_compare = hmac.new(meta.csrf_secret, check_val, digestmod=sha1)
        if hmac_compare.hexdigest() != hmac_csrf:
            raise ValidationError(field.gettext('CSRF failed'))

        if self.time_limit:
            now_formatted = self.now().strftime(self.TIME_FORMAT)
            if now_formatted > expires:
                raise ValidationError(field.gettext('CSRF token expired')) 
Example 11
Project: flasky   Author: RoseOu   File: session.py    MIT License 6 votes vote down vote up
def validate_csrf_token(self, field):
        if not field.data or '##' not in field.data:
            raise ValidationError(field.gettext('CSRF token missing'))

        expires, hmac_csrf = field.data.split('##')

        check_val = (field.csrf_key + expires).encode('utf8')

        hmac_compare = hmac.new(self.SECRET_KEY, check_val, digestmod=sha1)
        if hmac_compare.hexdigest() != hmac_csrf:
            raise ValidationError(field.gettext('CSRF failed'))

        if self.TIME_LIMIT:
            now_formatted = datetime.now().strftime(self.TIME_FORMAT)
            if now_formatted > expires:
                raise ValidationError(field.gettext('CSRF token expired')) 
Example 12
Project: flasky   Author: RoseOu   File: security.py    MIT License 6 votes vote down vote up
def pbkdf2_hex(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Like :func:`pbkdf2_bin`, but returns a hex-encoded string.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided,
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function, or a function
                     from the hashlib module.  Defaults to sha1.
    """
    rv = pbkdf2_bin(data, salt, iterations, keylen, hashfunc)
    return to_native(codecs.encode(rv, 'hex_codec')) 
Example 13
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    Apache License 2.0 6 votes vote down vote up
def check_sha1(filename, sha1_hash):
    """Check whether the sha1 hash of the file content matches the expected hash.

    Parameters
    ----------
    filename : str
        Path to the file.
    sha1_hash : str
        Expected sha1 hash in hexadecimal digits.

    Returns
    -------
    bool
        Whether the file content matches the expected hash.
    """
    sha1 = hashlib.sha1()
    with open(filename, 'rb') as f:
        while True:
            data = f.read(1048576)
            if not data:
                break
            sha1.update(data)

    return sha1.hexdigest() == sha1_hash 
Example 14
Project: FCOS_GluonCV   Author: DetectionTeamUCAS   File: download.py    Apache License 2.0 6 votes vote down vote up
def check_sha1(filename, sha1_hash):
    """Check whether the sha1 hash of the file content matches the expected hash.
    Parameters
    ----------
    filename : str
        Path to the file.
    sha1_hash : str
        Expected sha1 hash in hexadecimal digits.
    Returns
    -------
    bool
        Whether the file content matches the expected hash.
    """
    sha1 = hashlib.sha1()
    with open(filename, 'rb') as f:
        while True:
            data = f.read(1048576)
            if not data:
                break
            sha1.update(data)

    sha1_file = sha1.hexdigest()
    l = min(len(sha1_file), len(sha1_hash))
    return sha1.hexdigest()[0:l] == sha1_hash[0:l] 
Example 15
Project: fs_image   Author: facebookincubator   File: common.py    MIT License 5 votes vote down vote up
def in_shard(self, rpm):
        # Our contract is that the RPM filename is the global primary key,
        #
        # We use the last 8 bytes of SHA1, since we need a deterministic
        # hash for parallel downloads, and Python standard library lacks
        # fast non-cryptographic hashes like CityHash or SpookyHashV2.
        # adler32 is faster, but way too collision-prone to bother.
        h, = _UINT64_STRUCT.unpack_from(
            hashlib.sha1(byteme(rpm.filename())).digest(), 12
        )
        return h % self.modulo == self.shard 
Example 16
Project: fs_image   Author: facebookincubator   File: common.py    MIT License 5 votes vote down vote up
def hasher(self):
        # Certain repos use "sha" to refer to "SHA-1", whereas in `hashlib`,
        # "sha" goes through OpenSSL and refers to a different digest.
        if self.algorithm == 'sha':
            return hashlib.sha1()
        return hashlib.new(self.algorithm) 
Example 17
Project: leapp-repository   Author: oamg   File: vsftpdutils.py    Apache License 2.0 5 votes vote down vote up
def get_default_config_hash(read_func=read_file):
    '''
    Read the default vsftpd configuration file (/etc/vsftpd/vsftpd.conf) and return its hash.

    :param read_func: Function to use to read the file. This is meant to be overriden in tests.
    :return SHA1 hash of the configuration file, or None if the file could not be read.
    '''
    content = get_config_contents(VSFTPD_DEFAULT_CONFIG_PATH, read_func=read_func)
    if content is None:
        return None
    content = content.encode(encoding='utf-8')
    h = hashlib.sha1(content)
    return h.hexdigest() 
Example 18
Project: pyblish-win   Author: pyblish   File: urllib2.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_cnonce(self, nonce):
        # The cnonce-value is an opaque
        # quoted string value provided by the client and used by both client
        # and server to avoid chosen plaintext attacks, to provide mutual
        # authentication, and to provide some message integrity protection.
        # This isn't a fabulous effort, but it's probably Good Enough.
        dig = hashlib.sha1("%s:%s:%s:%s" % (self.nonce_count, nonce, time.ctime(),
                                            randombytes(8))).hexdigest()
        return dig[:16] 
Example 19
Project: pyblish-win   Author: pyblish   File: urllib2.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_algorithm_impls(self, algorithm):
        # algorithm should be case-insensitive according to RFC2617
        algorithm = algorithm.upper()
        # lambdas assume digest modules are imported at the top level
        if algorithm == 'MD5':
            H = lambda x: hashlib.md5(x).hexdigest()
        elif algorithm == 'SHA':
            H = lambda x: hashlib.sha1(x).hexdigest()
        # XXX MD5-sess
        KD = lambda s, d: H("%s:%s" % (s, d))
        return H, KD 
Example 20
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_case_sha1_0(self):
        self.check('sha1', "",
                   "da39a3ee5e6b4b0d3255bfef95601890afd80709") 
Example 21
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_case_sha1_1(self):
        self.check('sha1', "abc",
                   "a9993e364706816aba3e25717850c26c9cd0d89d") 
Example 22
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_case_sha1_2(self):
        self.check('sha1', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
                   "84983e441c3bd26ebaae4aa1f95129e5e54670f1") 
Example 23
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_case_sha1_3(self):
        self.check('sha1', "a" * 1000000,
                   "34aa973cd4c4daa4f61eeb2bdbad27316534016f") 
Example 24
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_case_sha1_huge(self, size):
        if size == _4G + 5:
            try:
                self.check('sha1', 'A'*size,
                        '87d745c50e6b2879ffa0fb2c930e9fbfe0dc9a5b')
            except OverflowError:
                pass # 32-bit arch 
Example 25
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_threaded_hashing(self):
        # Updating the same hash object from several threads at once
        # using data chunk sizes containing the same byte sequences.
        #
        # If the internal locks are working to prevent multiple
        # updates on the same object from running at once, the resulting
        # hash will be the same as doing it single threaded upfront.
        hasher = hashlib.sha1()
        num_threads = 5
        smallest_data = 'swineflu'
        data = smallest_data*200000
        expected_hash = hashlib.sha1(data*num_threads).hexdigest()

        def hash_in_chunks(chunk_size, event):
            index = 0
            while index < len(data):
                hasher.update(data[index:index+chunk_size])
                index += chunk_size
            event.set()

        events = []
        for threadnum in xrange(num_threads):
            chunk_size = len(data) // (10**threadnum)
            assert chunk_size > 0
            assert chunk_size % len(smallest_data) == 0
            event = threading.Event()
            events.append(event)
            threading.Thread(target=hash_in_chunks,
                             args=(chunk_size, event)).start()

        for event in events:
            event.wait()

        self.assertEqual(expected_hash, hasher.hexdigest()) 
Example 26
Project: pyblish-win   Author: pyblish   File: test_hmac.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_sha_vectors(self):
        def shatest(key, data, digest):
            h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
            self.assertEqual(h.hexdigest().upper(), digest.upper())

        shatest(chr(0x0b) * 20,
                "Hi There",
                "b617318655057264e28bc0b6fb378c8ef146be00")

        shatest("Jefe",
                "what do ya want for nothing?",
                "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")

        shatest(chr(0xAA)*20,
                chr(0xDD)*50,
                "125d7342b9ac11cd91a39af48aa17b4f63f175d3")

        shatest("".join([chr(i) for i in range(1, 26)]),
                chr(0xCD) * 50,
                "4c9007f4026250c6bc8414f9bf50c86c2d7235da")

        shatest(chr(0x0C) * 20,
                "Test With Truncation",
                "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")

        shatest(chr(0xAA) * 80,
                "Test Using Larger Than Block-Size Key - Hash Key First",
                "aa4ae5e15272d00e95705637ce8a3b55ed402112")

        shatest(chr(0xAA) * 80,
                ("Test Using Larger Than Block-Size Key "
                 "and Larger Than One Block-Size Data"),
                "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") 
Example 27
Project: pyblish-win   Author: pyblish   File: test_hmac.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_withmodule(self):
        # Constructor call with text and digest module.
        try:
            h = hmac.HMAC("key", "", hashlib.sha1)
        except:
            self.fail("Constructor call with hashlib.sha1 raised exception.") 
Example 28
Project: pyblish-win   Author: pyblish   File: uuid.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def uuid5(namespace, name):
    """Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
    from hashlib import sha1
    hash = sha1(namespace.bytes + name).digest()
    return UUID(bytes=hash[:16], version=5)

# The following standard UUIDs are for use with uuid3() or uuid5(). 
Example 29
Project: WebTeam   Author: shashankrnr32   File: GenerateAPI.py    MIT License 5 votes vote down vote up
def calculateSignature(string, private_key):
    hash_var = hmac.new(private_key, string, hashlib.sha1).digest()
    sig = urllib.parse.quote_plus(base64.b64encode(hash_var))
    return sig 
Example 30
Project: WebTeam   Author: shashankrnr32   File: GenerateAPI.py    MIT License 5 votes vote down vote up
def calculateSignature(string, private_key):
    hash_var = hmac.new(private_key, string, hashlib.sha1).digest()
    sig = urllib.parse.quote_plus(base64.b64encode(hash_var))
    return sig 
Example 31
Project: WebTeam   Author: shashankrnr32   File: GenerateAPI.py    MIT License 5 votes vote down vote up
def calculateSignature(string, private_key):
    hash_var = hmac.new(private_key, string, hashlib.sha1).digest()
    sig = urllib.parse.quote_plus(base64.b64encode(hash_var))
    return sig 
Example 32
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: bccache.py    Apache License 2.0 5 votes vote down vote up
def get_cache_key(self, name, filename=None):
        """Returns the unique hash key for this template name."""
        hash = sha1(name.encode('utf-8'))
        if filename is not None:
            filename = '|' + filename
            if isinstance(filename, text_type):
                filename = filename.encode('utf-8')
            hash.update(filename)
        return hash.hexdigest() 
Example 33
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: bccache.py    Apache License 2.0 5 votes vote down vote up
def get_source_checksum(self, source):
        """Returns a checksum for the source."""
        return sha1(source.encode('utf-8')).hexdigest() 
Example 34
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: loaders.py    Apache License 2.0 5 votes vote down vote up
def get_template_key(name):
        return 'tmpl_' + sha1(name.encode('utf-8')).hexdigest() 
Example 35
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: bccache.py    Apache License 2.0 5 votes vote down vote up
def get_cache_key(self, name, filename=None):
        """Returns the unique hash key for this template name."""
        hash = sha1(name.encode('utf-8'))
        if filename is not None:
            filename = '|' + filename
            if isinstance(filename, text_type):
                filename = filename.encode('utf-8')
            hash.update(filename)
        return hash.hexdigest() 
Example 36
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: bccache.py    Apache License 2.0 5 votes vote down vote up
def get_source_checksum(self, source):
        """Returns a checksum for the source."""
        return sha1(source.encode('utf-8')).hexdigest() 
Example 37
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 5 votes vote down vote up
def _find_hashlib_algorithms():
    algos = getattr(hashlib, 'algorithms', None)
    if algos is None:
        algos = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
    rv = {}
    for algo in algos:
        func = getattr(hashlib, algo, None)
        if func is not None:
            rv[algo] = func
    return rv 
Example 38
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 5 votes vote down vote up
def generate_password_hash(password, method='pbkdf2:sha1', salt_length=8):
    """Hash a password with the given method and salt with with a string of
    the given length.  The format of the string returned includes the method
    that was used so that :func:`check_password_hash` can check the hash.

    The format for the hashed string looks like this::

        method$salt$hash

    This method can **not** generate unsalted passwords but it is possible
    to set the method to plain to enforce plaintext passwords.  If a salt
    is used, hmac is used internally to salt the password.

    If PBKDF2 is wanted it can be enabled by setting the method to
    ``pbkdf2:method:iterations`` where iterations is optional::

        pbkdf2:sha1:2000$salt$hash
        pbkdf2:sha1$salt$hash

    :param password: the password to hash.
    :param method: the hash method to use (one that hashlib supports). Can
                   optionally be in the format ``pbkdf2:<method>[:iterations]``
                   to enable PBKDF2.
    :param salt_length: the length of the salt in letters.
    """
    salt = method != 'plain' and gen_salt(salt_length) or ''
    h, actual_method = _hash_internal(method, salt, password)
    return '%s$%s$%s' % (actual_method, salt, h) 
Example 39
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 5 votes vote down vote up
def check_password_hash(pwhash, password):
    """check a password against a given salted and hashed password value.
    In order to support unsalted legacy passwords this method supports
    plain text passwords, md5 and sha1 hashes (both salted and unsalted).

    Returns `True` if the password matched, `False` otherwise.

    :param pwhash: a hashed string like returned by
                   :func:`generate_password_hash`.
    :param password: the plaintext password to compare against the hash.
    """
    if pwhash.count('$') < 2:
        return False
    method, salt, hashval = pwhash.split('$', 2)
    return safe_str_cmp(_hash_internal(method, salt, password)[0], hashval) 
Example 40
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 5 votes vote down vote up
def _find_hashlib_algorithms():
    algos = getattr(hashlib, 'algorithms', None)
    if algos is None:
        algos = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
    rv = {}
    for algo in algos:
        func = getattr(hashlib, algo, None)
        if func is not None:
            rv[algo] = func
    return rv 
Example 41
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 5 votes vote down vote up
def generate_password_hash(password, method='pbkdf2:sha1', salt_length=8):
    """Hash a password with the given method and salt with with a string of
    the given length.  The format of the string returned includes the method
    that was used so that :func:`check_password_hash` can check the hash.

    The format for the hashed string looks like this::

        method$salt$hash

    This method can **not** generate unsalted passwords but it is possible
    to set the method to plain to enforce plaintext passwords.  If a salt
    is used, hmac is used internally to salt the password.

    If PBKDF2 is wanted it can be enabled by setting the method to
    ``pbkdf2:method:iterations`` where iterations is optional::

        pbkdf2:sha1:2000$salt$hash
        pbkdf2:sha1$salt$hash

    :param password: the password to hash.
    :param method: the hash method to use (one that hashlib supports). Can
                   optionally be in the format ``pbkdf2:<method>[:iterations]``
                   to enable PBKDF2.
    :param salt_length: the length of the salt in letters.
    """
    salt = method != 'plain' and gen_salt(salt_length) or ''
    h, actual_method = _hash_internal(method, salt, password)
    return '%s$%s$%s' % (actual_method, salt, h) 
Example 42
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 5 votes vote down vote up
def check_password_hash(pwhash, password):
    """check a password against a given salted and hashed password value.
    In order to support unsalted legacy passwords this method supports
    plain text passwords, md5 and sha1 hashes (both salted and unsalted).

    Returns `True` if the password matched, `False` otherwise.

    :param pwhash: a hashed string like returned by
                   :func:`generate_password_hash`.
    :param password: the plaintext password to compare against the hash.
    """
    if pwhash.count('$') < 2:
        return False
    method, salt, hashval = pwhash.split('$', 2)
    return safe_str_cmp(_hash_internal(method, salt, password)[0], hashval) 
Example 43
Project: btcchina-api-python-3   Author: aeromomo   File: btcchina.py    MIT License 5 votes vote down vote up
def _get_params_hash(self,pdict):
        pstring=""
        # The order of params is critical for calculating a correct hash
        fields=['tonce','accesskey','requestmethod','id','method','params']
        for f in fields:
            if pdict[f]:
                if f == 'params':
                    # Convert list to string, then strip brackets and spaces
                    # probably a cleaner way to do this
                    param_string=re.sub("[\[\] ]","",str(pdict[f]))
                    param_string=re.sub("'",'',param_string)
                    param_string=re.sub("True",'1',param_string)
                    param_string=re.sub("False",'',param_string)
                    param_string=re.sub("None",'',param_string)
                    pstring+=f+'='+param_string+'&'
                else:
                    pstring+=f+'='+str(pdict[f])+'&'
            else:
                pstring+=f+'=&'
        pstring=pstring.strip('&').encode('utf8')
        # print(bytearray(self.secret_key.encode('utf-8')))
        # now with correctly ordered param string, calculate hash
        # print(self.secret_key)
        # self.secret_key = self.secret_key.encode('utf8')
        # print(self.secret_key)
        phash = hmac.new(self.secret_key, pstring, hashlib.sha1).hexdigest()
        return phash 
Example 44
Project: ieml   Author: IEMLdev   File: export.py    GNU General Public License v3.0 5 votes vote down vote up
def _get_drupal_utils():
    global _drupal_utils
    if _drupal_utils is None:
        _drupal_utils = {
            'drupal_dico': [ieml_term_model(t) for t in Dictionary()]
        }
        _drupal_utils['all_uuid'] = bidict({
                d['IEML']: 1000 + int(hashlib.sha1(d['IEML'].encode()).hexdigest(), 16) % MAX_TERMS_DICTIONARY for d in _drupal_utils['drupal_dico']
        })

    return _drupal_utils 
Example 45
Project: pytuber   Author: tefra   File: models.py    MIT License 5 votes vote down vote up
def __attrs_post_init__(self):
        if self.id is None:
            self.id = hashlib.sha1(
                re.sub(
                    r"[\W_]+",
                    "",
                    "{}{}".format(self.artist, self.name).lower(),
                ).encode("utf-8")
            ).hexdigest()[:7] 
Example 46
Project: pytuber   Author: tefra   File: models.py    MIT License 5 votes vote down vote up
def __attrs_post_init__(self):
        if self.id is None:
            self.id = hashlib.sha1(
                json.dumps(
                    {
                        field: getattr(self, field)
                        for field in ["arguments", "provider", "type"]
                    }
                ).encode("utf-8")
            ).hexdigest()[:7] 
Example 47
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: crl.py    MIT License 5 votes vote down vote up
def sha1(self):
        """
        :return:
            The SHA1 hash of the DER-encoded bytes of this certificate list
        """

        if self._sha1 is None:
            self._sha1 = hashlib.sha1(self.dump()).digest()
        return self._sha1 
Example 48
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: keys.py    MIT License 5 votes vote down vote up
def hash_algo(self):
        """
        Returns the name of the family of hash algorithms used to generate a
        DSA key

        :raises:
            ValueError - when the key is not a DSA key

        :return:
            A unicode string of "sha1" or "sha2" or None if no parameters are
            present
        """

        if self.algorithm != 'dsa':
            raise ValueError(unwrap(
                '''
                Only DSA keys are generated using a hash algorithm, this key is
                %s
                ''',
                self.algorithm.upper()
            ))

        parameters = self['algorithm']['parameters']
        if parameters.native is None:
            return None

        byte_len = math.log(parameters['q'].native, 2) / 8

        return 'sha1' if byte_len <= 20 else 'sha2' 
Example 49
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: keys.py    MIT License 5 votes vote down vote up
def sha1(self):
        """
        :return:
            The SHA1 hash of the DER-encoded bytes of this public key info
        """

        if self._sha1 is None:
            self._sha1 = hashlib.sha1(byte_cls(self['public_key'])).digest()
        return self._sha1 
Example 50
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: x509.py    MIT License 5 votes vote down vote up
def sha1(self):
        """
        :return:
            The SHA1 hash of the DER-encoded bytes of this name
        """

        if self._sha1 is None:
            self._sha1 = hashlib.sha1(self.dump()).digest()
        return self._sha1 
Example 51
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: x509.py    MIT License 5 votes vote down vote up
def sha1(self):
        """
        :return:
            The SHA-1 hash of the DER-encoded bytes of this complete certificate
        """

        if self._sha1 is None:
            self._sha1 = hashlib.sha1(self.dump()).digest()
        return self._sha1 
Example 52
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: x509.py    MIT License 5 votes vote down vote up
def sha1_fingerprint(self):
        """
        :return:
            A unicode string of the SHA-1 hash, formatted using hex encoding
            with a space between each pair of characters, all uppercase
        """

        return ' '.join('%02X' % c for c in bytes_to_list(self.sha1)) 
Example 53
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bccache.py    MIT License 5 votes vote down vote up
def get_cache_key(self, name, filename=None):
        """Returns the unique hash key for this template name."""
        hash = sha1(name.encode('utf-8'))
        if filename is not None:
            filename = '|' + filename
            if isinstance(filename, text_type):
                filename = filename.encode('utf-8')
            hash.update(filename)
        return hash.hexdigest() 
Example 54
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bccache.py    MIT License 5 votes vote down vote up
def get_source_checksum(self, source):
        """Returns a checksum for the source."""
        return sha1(source.encode('utf-8')).hexdigest() 
Example 55
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: loaders.py    MIT License 5 votes vote down vote up
def get_template_key(name):
        return 'tmpl_' + sha1(name.encode('utf-8')).hexdigest() 
Example 56
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bigip_ssl_key.py    MIT License 5 votes vote down vote up
def _get_hash(self, content):
        k = hashlib.sha1()
        s = StringIO(content)
        while True:
            data = s.read(1024)
            if not data:
                break
            k.update(data.encode('utf-8'))
        return k.hexdigest() 
Example 57
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: dnsmadeeasy.py    MIT License 5 votes vote down vote up
def _create_hash(self, rightnow):
        return hmac.new(self.secret.encode(), rightnow.encode(), hashlib.sha1).hexdigest() 
Example 58
Project: ps4-remote-play   Author: kingcreek   File: registry.py    Apache License 2.0 5 votes vote down vote up
def _computed_basename(console):
    return hashlib.sha1(console.name.encode('utf-8')).hexdigest() 
Example 59
Project: django-qcloud-cos   Author: jasonham   File: cos_auth.py    Apache License 2.0 5 votes vote down vote up
def get_signkey(self):
        q_time = '%s;%s' % (self.q_sign_time, self.q_key_time)
        signkey = hmac.new(bytearray(self.SecretKey, 'utf-8'), bytearray(q_time, 'utf-8'), hashlib.sha1).hexdigest()
        return signkey 
Example 60
Project: django-qcloud-cos   Author: jasonham   File: cos_auth.py    Apache License 2.0 5 votes vote down vote up
def get_stringtosign(self):
        q_sign_time = '%s;%s' % (self.q_sign_time, self.q_key_time)
        formatstring = self.get_formatstring()
        sh1hash_formatstring = hashlib.sha1(formatstring.encode('utf-8')).hexdigest()
        stringtosign = "sha1\n%s\n%s\n" % (q_sign_time, sh1hash_formatstring)
        return stringtosign 
Example 61
Project: django-qcloud-cos   Author: jasonham   File: cos_auth.py    Apache License 2.0 5 votes vote down vote up
def get_signature(self):
        signature = hmac.new(
            bytearray(self.get_signkey(), 'utf-8'),
            bytearray(self.get_stringtosign(), 'utf-8'),
            hashlib.sha1
        ).hexdigest()
        return signature 
Example 62
Project: django-qcloud-cos   Author: jasonham   File: cos_auth.py    Apache License 2.0 5 votes vote down vote up
def get_authorization(self):
        header_list = self.format_args(self.head, False)
        signed_parameter = self.format_args(self.parameters, False, False)
        q_time = '%s;%s' % (self.q_sign_time, self.q_key_time)

        t_tuple = (self.SecretID, q_time, q_time, header_list, signed_parameter, self.get_signature())
        authorization = "q-sign-algorithm=sha1&" \
                        "q-ak=%s&" \
                        "q-sign-time=%s&" \
                        "q-key-time=%s&" \
                        "q-header-list=%s&" \
                        "q-url-param-list=%s&" \
                        "q-signature=%s" % t_tuple
        return authorization 
Example 63
Project: flasky   Author: RoseOu   File: bccache.py    MIT License 5 votes vote down vote up
def get_cache_key(self, name, filename=None):
        """Returns the unique hash key for this template name."""
        hash = sha1(name.encode('utf-8'))
        if filename is not None:
            filename = '|' + filename
            if isinstance(filename, text_type):
                filename = filename.encode('utf-8')
            hash.update(filename)
        return hash.hexdigest() 
Example 64
Project: flasky   Author: RoseOu   File: bccache.py    MIT License 5 votes vote down vote up
def get_source_checksum(self, source):
        """Returns a checksum for the source."""
        return sha1(source.encode('utf-8')).hexdigest() 
Example 65
Project: flasky   Author: RoseOu   File: loaders.py    MIT License 5 votes vote down vote up
def get_template_key(name):
        return 'tmpl_' + sha1(name.encode('utf-8')).hexdigest() 
Example 66
Project: flasky   Author: RoseOu   File: security.py    MIT License 5 votes vote down vote up
def _find_hashlib_algorithms():
    algos = getattr(hashlib, 'algorithms', None)
    if algos is None:
        algos = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
    rv = {}
    for algo in algos:
        func = getattr(hashlib, algo, None)
        if func is not None:
            rv[algo] = func
    return rv 
Example 67
Project: flasky   Author: RoseOu   File: security.py    MIT License 5 votes vote down vote up
def generate_password_hash(password, method='pbkdf2:sha1', salt_length=8):
    """Hash a password with the given method and salt with with a string of
    the given length.  The format of the string returned includes the method
    that was used so that :func:`check_password_hash` can check the hash.

    The format for the hashed string looks like this::

        method$salt$hash

    This method can **not** generate unsalted passwords but it is possible
    to set the method to plain to enforce plaintext passwords.  If a salt
    is used, hmac is used internally to salt the password.

    If PBKDF2 is wanted it can be enabled by setting the method to
    ``pbkdf2:method:iterations`` where iterations is optional::

        pbkdf2:sha1:2000$salt$hash
        pbkdf2:sha1$salt$hash

    :param password: the password to hash.
    :param method: the hash method to use (one that hashlib supports). Can
                   optionally be in the format ``pbkdf2:<method>[:iterations]``
                   to enable PBKDF2.
    :param salt_length: the length of the salt in letters.
    """
    salt = method != 'plain' and gen_salt(salt_length) or ''
    h, actual_method = _hash_internal(method, salt, password)
    return '%s$%s$%s' % (actual_method, salt, h) 
Example 68
Project: flasky   Author: RoseOu   File: sessions.py    MIT License 5 votes vote down vote up
def generate_key(salt=None):
    if salt is None:
        salt = repr(salt).encode('ascii')
    return sha1(b''.join([
        salt,
        str(time()).encode('ascii'),
        _urandom()
    ])).hexdigest() 
Example 69
Project: flasky   Author: RoseOu   File: csrf.py    MIT License 5 votes vote down vote up
def generate_csrf(secret_key=None, time_limit=None):
    """Generate csrf token code.

    :param secret_key: A secret key for mixing in the token,
                       default is Flask.secret_key.
    :param time_limit: Token valid in the time limit,
                       default is 3600s.
    """
    if not secret_key:
        secret_key = current_app.config.get(
            'WTF_CSRF_SECRET_KEY', current_app.secret_key
        )

    if not secret_key:
        raise Exception('Must provide secret_key to use csrf.')

    if time_limit is None:
        time_limit = current_app.config.get('WTF_CSRF_TIME_LIMIT', 3600)

    if 'csrf_token' not in session:
        session['csrf_token'] = hashlib.sha1(os.urandom(64)).hexdigest()

    if time_limit:
        expires = time.time() + time_limit
        csrf_build = '%s%s' % (session['csrf_token'], expires)
    else:
        expires = ''
        csrf_build = session['csrf_token']

    hmac_csrf = hmac.new(
        to_bytes(secret_key),
        to_bytes(csrf_build),
        digestmod=hashlib.sha1
    ).hexdigest()
    return '%s##%s' % (expires, hmac_csrf) 
Example 70
Project: flasky   Author: RoseOu   File: csrf.py    MIT License 5 votes vote down vote up
def validate_csrf(data, secret_key=None, time_limit=None):
    """Check if the given data is a valid csrf token.

    :param data: The csrf token value to be checked.
    :param secret_key: A secret key for mixing in the token,
                       default is Flask.secret_key.
    :param time_limit: Check if the csrf token is expired.
                       default is True.
    """
    if not data or '##' not in data:
        return False

    expires, hmac_csrf = data.split('##', 1)
    try:
        expires = float(expires)
    except:
        return False

    if time_limit is None:
        time_limit = current_app.config.get('WTF_CSRF_TIME_LIMIT', 3600)

    if time_limit:
        now = time.time()
        if now > expires:
            return False

    if not secret_key:
        secret_key = current_app.config.get(
            'WTF_CSRF_SECRET_KEY', current_app.secret_key
        )

    if 'csrf_token' not in session:
        return False

    csrf_build = '%s%s' % (session['csrf_token'], expires)
    hmac_compare = hmac.new(
        to_bytes(secret_key),
        to_bytes(csrf_build),
        digestmod=hashlib.sha1
    ).hexdigest()
    return hmac_compare == hmac_csrf 
Example 71
Project: parasweep   Author: eviatarbach   File: namers.py    MIT License 5 votes vote down vote up
def generate_id(self, param_set, sweep_id):
        from hashlib import sha1

        h = sha1()
        h.update(bytes(json.dumps(param_set), 'utf-8'))
        h.update(bytes(sweep_id, 'utf-8'))

        return h.hexdigest()[:self.hash_length] 
Example 72
Project: extractors   Author: pudo-attic   File: cache.py    MIT License 5 votes vote down vote up
def get_cache(data):
    if CACHE_DIR is None:
        log.debug('No EXTRACTORS_CACHE_DIR is set, not caching.')
        return None, None
    key = sha1(data).hexdigest()
    try:
        with open(key_path(key), 'rb') as fh:
            return key, pickle.load(fh)
    except IOError:
        return key, None 
Example 73
Project: gitreformat   Author: ghtdak   File: gitreformat.py    MIT License 5 votes vote down vote up
def githash(data):  # how git computes the hash of a file
    s = sha1()
    s.update("blob %u\0" % len(data))
    s.update(data)
    return s.digest() 
Example 74
Project: pyblish-win   Author: pyblish   File: test_unicodedata.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_method_checksum(self):
        h = hashlib.sha1()
        for i in range(0x10000):
            char = unichr(i)
            data = [
                # Predicates (single char)
                u"01"[char.isalnum()],
                u"01"[char.isalpha()],
                u"01"[char.isdecimal()],
                u"01"[char.isdigit()],
                u"01"[char.islower()],
                u"01"[char.isnumeric()],
                u"01"[char.isspace()],
                u"01"[char.istitle()],
                u"01"[char.isupper()],

                # Predicates (multiple chars)
                u"01"[(char + u'abc').isalnum()],
                u"01"[(char + u'abc').isalpha()],
                u"01"[(char + u'123').isdecimal()],
                u"01"[(char + u'123').isdigit()],
                u"01"[(char + u'abc').islower()],
                u"01"[(char + u'123').isnumeric()],
                u"01"[(char + u' \t').isspace()],
                u"01"[(char + u'abc').istitle()],
                u"01"[(char + u'ABC').isupper()],

                # Mappings (single char)
                char.lower(),
                char.upper(),
                char.title(),

                # Mappings (multiple chars)
                (char + u'abc').lower(),
                (char + u'ABC').upper(),
                (char + u'abc').title(),
                (char + u'ABC').title(),

                ]
            h.update(u''.join(data).encode(encoding))
        result = h.hexdigest()
        self.assertEqual(result, self.expectedchecksum) 
Example 75
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
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) 
Example 76
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 4 votes vote down vote up
def pbkdf2_bin(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Returns a binary digest for the PBKDF2 hash algorithm of `data`
    with the given `salt`. It iterates `iterations` times and produces a
    key of `keylen` bytes. By default, SHA-1 is used as hash function;
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha1.
    """
    if isinstance(hashfunc, string_types):
        hashfunc = _hash_funcs[hashfunc]
    elif not hashfunc:
        hashfunc = hashlib.sha1
    data = to_bytes(data)
    salt = to_bytes(salt)

    # If we're on Python with pbkdf2_hmac we can try to use it for
    # compatible digests.
    if _has_native_pbkdf2:
        _test_hash = hashfunc()
        if hasattr(_test_hash, 'name') and \
           _test_hash.name in _hash_funcs:
            return hashlib.pbkdf2_hmac(_test_hash.name,
                                       data, salt, iterations,
                                       keylen)

    mac = hmac.HMAC(data, None, hashfunc)
    if not keylen:
        keylen = mac.digest_size
    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        h.update(x)
        return bytearray(h.digest())
    buf = bytearray()
    for block in range_type(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range_type(iterations - 1):
            u = _pseudorandom(bytes(u))
            rv = bytearray(starmap(xor, izip(rv, u)))
        buf.extend(rv)
    return bytes(buf[:keylen]) 
Example 77
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: security.py    Apache License 2.0 4 votes vote down vote up
def pbkdf2_bin(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Returns a binary digest for the PBKDF2 hash algorithm of `data`
    with the given `salt`. It iterates `iterations` times and produces a
    key of `keylen` bytes. By default, SHA-1 is used as hash function;
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha1.
    """
    if isinstance(hashfunc, string_types):
        hashfunc = _hash_funcs[hashfunc]
    elif not hashfunc:
        hashfunc = hashlib.sha1
    data = to_bytes(data)
    salt = to_bytes(salt)

    # If we're on Python with pbkdf2_hmac we can try to use it for
    # compatible digests.
    if _has_native_pbkdf2:
        _test_hash = hashfunc()
        if hasattr(_test_hash, 'name') and \
           _test_hash.name in _hash_funcs:
            return hashlib.pbkdf2_hmac(_test_hash.name,
                                       data, salt, iterations,
                                       keylen)

    mac = hmac.HMAC(data, None, hashfunc)
    if not keylen:
        keylen = mac.digest_size
    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        h.update(x)
        return bytearray(h.digest())
    buf = bytearray()
    for block in range_type(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range_type(iterations - 1):
            u = _pseudorandom(bytes(u))
            rv = bytearray(starmap(xor, izip(rv, u)))
        buf.extend(rv)
    return bytes(buf[:keylen]) 
Example 78
Project: neural-fingerprinting   Author: StephanZheng   File: dataset_helper.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def enforce_epsilon_and_compute_hash(dataset_batch_dir, adv_dir, output_dir,
                                     epsilon):
  """Enforces size of perturbation on images, and compute hashes for all images.

  Args:
    dataset_batch_dir: directory with the images of specific dataset batch
    adv_dir: directory with generated adversarial images
    output_dir: directory where to copy result
    epsilon: size of perturbation

  Returns:
    dictionary with mapping form image ID to hash.
  """
  dataset_images = [f for f in os.listdir(dataset_batch_dir)
                    if f.endswith('.png')]
  image_hashes = {}
  resize_warning = False
  for img_name in dataset_images:
    if not os.path.exists(os.path.join(adv_dir, img_name)):
      logging.warning('Image %s not found in the output', img_name)
      continue
    image = np.array(
        Image.open(os.path.join(dataset_batch_dir, img_name)).convert('RGB'))
    image = image.astype('int32')
    image_max_clip = np.clip(image + epsilon, 0, 255).astype('uint8')
    image_min_clip = np.clip(image - epsilon, 0, 255).astype('uint8')
    # load and resize adversarial image if needed
    adv_image = Image.open(os.path.join(adv_dir, img_name)).convert('RGB')
    # Image.size is reversed compared to np.array.shape
    if adv_image.size[::-1] != image.shape[:2]:
      resize_warning = True
      adv_image = adv_image.resize((image.shape[1], image.shape[0]),
                                   Image.BICUBIC)
    adv_image = np.array(adv_image)
    clipped_adv_image = np.clip(adv_image,
                                image_min_clip,
                                image_max_clip)
    Image.fromarray(clipped_adv_image).save(os.path.join(output_dir, img_name))
    # compute hash
    image_hashes[img_name[:-4]] = hashlib.sha1(
        clipped_adv_image.view(np.uint8)).hexdigest()
  if resize_warning:
    logging.warning('One or more adversarial images had incorrect size')
  return image_hashes 
Example 79
Project: flasky   Author: RoseOu   File: security.py    MIT License 4 votes vote down vote up
def pbkdf2_bin(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,
               keylen=None, hashfunc=None):
    """Returns a binary digest for the PBKDF2 hash algorithm of `data`
    with the given `salt`. It iterates `iterations` times and produces a
    key of `keylen` bytes. By default, SHA-1 is used as hash function;
    a different hashlib `hashfunc` can be provided.

    .. versionadded:: 0.9

    :param data: the data to derive.
    :param salt: the salt for the derivation.
    :param iterations: the number of iterations.
    :param keylen: the length of the resulting key.  If not provided
                   the digest size will be used.
    :param hashfunc: the hash function to use.  This can either be the
                     string name of a known hash function or a function
                     from the hashlib module.  Defaults to sha1.
    """
    if isinstance(hashfunc, string_types):
        hashfunc = _hash_funcs[hashfunc]
    elif not hashfunc:
        hashfunc = hashlib.sha1
    data = to_bytes(data)
    salt = to_bytes(salt)

    # If we're on Python with pbkdf2_hmac we can try to use it for
    # compatible digests.
    if _has_native_pbkdf2:
        _test_hash = hashfunc()
        if hasattr(_test_hash, 'name') and \
           _test_hash.name in _hash_funcs:
            return hashlib.pbkdf2_hmac(_test_hash.name,
                                       data, salt, iterations,
                                       keylen)

    mac = hmac.HMAC(data, None, hashfunc)
    if not keylen:
        keylen = mac.digest_size
    def _pseudorandom(x, mac=mac):
        h = mac.copy()
        h.update(x)
        return bytearray(h.digest())
    buf = bytearray()
    for block in range_type(1, -(-keylen // mac.digest_size) + 1):
        rv = u = _pseudorandom(salt + _pack_int(block))
        for i in range_type(iterations - 1):
            u = _pseudorandom(bytes(u))
            rv = bytearray(starmap(xor, izip(rv, u)))
        buf.extend(rv)
    return bytes(buf[:keylen]) 
Example 80
Project: FCOS_GluonCV   Author: DetectionTeamUCAS   File: download.py    Apache License 2.0 4 votes vote down vote up
def download(url, path=None, overwrite=False, sha1_hash=None):
    """Download an given URL
    Parameters
    ----------
    url : str
        URL to download
    path : str, optional
        Destination path to store downloaded file. By default stores to the
        current directory with same name as in url.
    overwrite : bool, optional
        Whether to overwrite destination file if already exists.
    sha1_hash : str, optional
        Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified
        but doesn't match.
    Returns
    -------
    str
        The file path of the downloaded file.
    """
    if path is None:
        fname = url.split('/')[-1]
    else:
        path = os.path.expanduser(path)
        if os.path.isdir(path):
            fname = os.path.join(path, url.split('/')[-1])
        else:
            fname = path

    if overwrite or not os.path.exists(fname) or (sha1_hash and not check_sha1(fname, sha1_hash)):
        dirname = os.path.dirname(os.path.abspath(os.path.expanduser(fname)))
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        print('Downloading %s from %s...'%(fname, url))
        r = requests.get(url, stream=True)
        if r.status_code != 200:
            raise RuntimeError("Failed downloading url %s"%url)
        total_length = r.headers.get('content-length')
        with open(fname, 'wb') as f:
            if total_length is None: # no content length header
                for chunk in r.iter_content(chunk_size=1024):
                    if chunk: # filter out keep-alive new chunks
                        f.write(chunk)
            else:
                total_length = int(total_length)
                for chunk in tqdm(r.iter_content(chunk_size=1024),
                                  total=int(total_length / 1024. + 0.5),
                                  unit='KB', unit_scale=False, dynamic_ncols=True):
                    f.write(chunk)

        if sha1_hash and not check_sha1(fname, sha1_hash):
            raise UserWarning('File {} is downloaded but the content hash does not match. ' \
                              'The repo may be outdated or download may be incomplete. ' \
                              'If the "repo_url" is overridden, consider switching to ' \
                              'the default repo.'.format(fname))

    return fname