Python hashlib.md5() Examples

The following are code examples for showing how to use hashlib.md5(). 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: fs_image   Author: facebookincubator   File: update_package_db.py    MIT License 6 votes vote down vote up
def _with_generated_header(contents, how_to_generate):
    # We'll inject the MD5 of the contents of the file into the header.
    # Lint complains if the MD5 in the header does not match the contents.
    # This is not a security measure.  It is only intended to discourage
    # people from manually resolving merge conflicts, which is error-prone
    # and can break trunk if a bad merge is accidentally committed.
    hex_hash = hashlib.md5(_with_generated_header_impl(
        contents,
        # This is the same magic value that lint uses, yay.
        '<<SignedSource::*O*zOeWoEQle#[email protected]>>',
        how_to_generate,
    ).encode()).hexdigest()
    return _with_generated_header_impl(
        contents, f'SignedSource<<{hex_hash}>>', how_to_generate,
    )


# TARGETS auto-formatting requires double-quotes and trailing commas, so we
# need our own serializer :/ -- `repr` or JSON won't do. 
Example 2
Project: pyblish-win   Author: pyblish   File: poplib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def apop(self, user, secret):
        """Authorisation

        - only possible if server has supplied a timestamp in initial greeting.

        Args:
                user    - mailbox user;
                secret  - secret shared between client and server.

        NB: mailbox is locked by server from here to 'quit()'
        """
        m = self.timestamp.match(self.welcome)
        if not m:
            raise error_proto('-ERR APOP not supported by server')
        import hashlib
        digest = hashlib.md5(m.group(1)+secret).digest()
        digest = ''.join(map(lambda x:'%02x'%ord(x), digest))
        return self._shortcmd('APOP %s %s' % (user, digest)) 
Example 3
Project: pyblish-win   Author: pyblish   File: test_urllib2_localnet.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _validate_auth(self, auth_dict, password, method, uri):
        final_dict = {}
        final_dict.update(auth_dict)
        final_dict["password"] = password
        final_dict["method"] = method
        final_dict["uri"] = uri
        HA1_str = "%(username)s:%(realm)s:%(password)s" % final_dict
        HA1 = hashlib.md5(HA1_str).hexdigest()
        HA2_str = "%(method)s:%(uri)s" % final_dict
        HA2 = hashlib.md5(HA2_str).hexdigest()
        final_dict["HA1"] = HA1
        final_dict["HA2"] = HA2
        response_str = "%(HA1)s:%(nonce)s:%(nc)s:" \
                       "%(cnonce)s:%(qop)s:%(HA2)s" % final_dict
        response = hashlib.md5(response_str).hexdigest()

        return response == auth_dict["response"] 
Example 4
Project: drydock   Author: airshipit   File: deckhand.py    Apache License 2.0 6 votes vote down vote up
def ingest_data(self, **kwargs):
        """Parse and save design data.

        :param content: String of valid Deckhand YAML

        :returns: a tuple of a status response and a list of parsed objects from drydock_provisioner.objects
        """
        def local_parse():
            return self.parse_docs(kwargs.get('content'))

        if 'content' in kwargs:
            try:
                # Hash the input to use as the cache key. This is not a security
                # related hash, so use cheap and fast MD5
                hv = hashlib.md5(kwargs.get('content', b'')).hexdigest()
                local_cache = cache.get_cache('parsed_docs')
                results = local_cache.get(key=hv, createfunc=local_parse)
                parse_status, models = results
            except Exception as ex:
                self.logger.debug("Error parsing design - hash %s", hv, exc_info=ex)
                raise ex
        else:
            raise ValueError('Missing parameter "content"')

        return parse_status, models 
Example 5
Project: prediction-constrained-topic-models   Author: dtak   File: util_setup.py    MIT License 6 votes vote down vote up
def setup_random_seed(
        output_path=None,
        seed=None,
        **kwargs):
    if str(seed).lower() == 'none' or seed == 'from_output_path_and_taskid':
        if output_path is None:
            seed = 8675309
        else:
            output_folders = os.path.abspath(output_path).split(os.path.sep)
            # Discard trailing '/' if it exists
            while len(output_folders[-1]) == 0:
                output_folders.pop()
            seed_str = str(output_folders[-2])[:8].split('-')[0]
            taskidstr = output_folders[-1]
            seed_str = seed_str[:5] + taskidstr
            pprint('[setup_random_seed] seed_str=' + seed_str)
            seed = int(hashlib.md5(seed_str).hexdigest(), 16) % 10000000
    else:
        seed = int(seed)
    pprint('[setup_random_seed] seed=%d' % (seed))
    return seed 
Example 6
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: _s3.py    MIT License 6 votes vote down vote up
def keysum_compare(module, local_file, s3, bucket, obj, version=None):
    s3_keysum = keysum(s3, bucket, obj, version=version)
    if '-' in s3_keysum:  # Check for multipart, ETag is not a proper MD5 sum
        parts = int(s3_keysum.split('-')[1])
        md5s = []

        with open(local_file, 'rb') as f:
            for part_num in range(1, parts + 1):
                # Get the part size for every part of the multipart uploaded object
                if version:
                    key_head = s3.head_object(Bucket=bucket, Key=obj, VersionId=version, PartNumber=part_num)
                else:
                    key_head = s3.head_object(Bucket=bucket, Key=obj, PartNumber=part_num)
                part_size = int(key_head['ContentLength'])
                data = f.read(part_size)
                hash = hashlib.md5(data)
                md5s.append(hash)

        digests = b''.join(m.digest() for m in md5s)
        digests_md5 = hashlib.md5(digests)
        local_keysum = '{0}-{1}'.format(digests_md5.hexdigest(), len(md5s))
    else:  # Compute the MD5 sum normally
        local_keysum = module.md5(local_file)

    return s3_keysum == local_keysum 
Example 7
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: maven_artifact.py    MIT License 6 votes vote down vote up
def download(self, artifact, filename=None):
        filename = artifact.get_filename(filename)
        if not artifact.version or artifact.version == "latest":
            artifact = Artifact(artifact.group_id, artifact.artifact_id, self.find_latest_version_available(artifact),
                                artifact.classifier, artifact.extension)

        url = self.find_uri_for_artifact(artifact)
        result = True
        if not self.verify_md5(filename, url + ".md5"):
            response = self._request(url, "Failed to download artifact " + str(artifact), lambda r: r)
            if response:
                f = open(filename, 'wb')
                self._write_chunks(response, f, report_hook=self.chunk_report)
                f.close()
            else:
                result = False
        return result 
Example 8
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: ipa_user.py    MIT License 6 votes vote down vote up
def get_ssh_key_fingerprint(ssh_key):
    """
    Return the public key fingerprint of a given public SSH key
    in format "FB:0C:AC:0A:07:94:5B:CE:75:6E:63:32:13:AD:AD:D7 [[email protected]] (ssh-rsa)"
    :param ssh_key:
    :return:
    """
    parts = ssh_key.strip().split()
    if len(parts) == 0:
        return None
    key_type = parts[0]
    key = base64.b64decode(parts[1].encode('ascii'))

    fp_plain = hashlib.md5(key).hexdigest()
    key_fp = ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2])).upper()
    if len(parts) < 3:
        return "%s (%s)" % (key_fp, key_type)
    else:
        user_host = parts[2]
        return "%s %s (%s)" % (key_fp, user_host, key_type) 
Example 9
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 10
Project: flasky   Author: RoseOu   File: flask_httpauth.py    MIT License 6 votes vote down vote up
def authenticate(self, auth, stored_password_or_ha1):
        if not auth or not auth.username or not auth.realm or not auth.uri \
                or not auth.nonce or not auth.response \
                or not stored_password_or_ha1:
            return False
        if not(self.verify_nonce_callback(auth.nonce)) or \
                not(self.verify_opaque_callback(auth.opaque)):
            return False
        if self.use_ha1_pw:
            ha1 = stored_password_or_ha1
        else:
            a1 = auth.username + ":" + auth.realm + ":" + \
                stored_password_or_ha1
            ha1 = md5(a1.encode('utf-8')).hexdigest()
        a2 = request.method + ":" + auth.uri
        ha2 = md5(a2.encode('utf-8')).hexdigest()
        a3 = ha1 + ":" + auth.nonce + ":" + ha2
        response = md5(a3.encode('utf-8')).hexdigest()
        return response == auth.response 
Example 11
Project: flasky   Author: RoseOu   File: models.py    MIT License 6 votes vote down vote up
def change_email(self, token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return False
        if data.get('change_email') != self.id:
            return False
        new_email = data.get('new_email')
        if new_email is None:
            return False
        if self.query.filter_by(email=new_email).first() is not None:
            return False
        self.email = new_email
        self.avatar_hash = hashlib.md5(
            self.email.encode('utf-8')).hexdigest()
        db.session.add(self)
        return True 
Example 12
Project: core   Author: lifemapper   File: jsonFormatter.py    GNU General Public License v3.0 6 votes vote down vote up
def _getLifemapperMetadata(objectType, lmId, url, userId, status=None,
                           statusModTime=None, metadata=None):
    """Get general Lifemapper metadata that we want to return for each type
    """
    lmDict = {
        'objectType' : objectType,
        'id' : lmId,
        'url' : url,
        'user' : userId
    }
    if status is not None:
        lmDict['status'] = status
    if statusModTime is not None:
        lmDict['statusModTime'] = formatTimeHuman(statusModTime)
        lmDict['etag'] = md5('{}-{}'.format(url, statusModTime)).hexdigest()
    if metadata is not None:
        lmDict['metadata'] = metadata

    return lmDict

# ............................................................................. 
Example 13
Project: django-yandex-cash-register   Author: bzzzzzz   File: forms.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def clean(self):
        data = super(PaymentProcessingForm, self).clean()
        if self.errors:
            if 'md5' in self.errors:
                self.set_error(self.ERROR_CODE_MD5, _('MD5 is incorrect'))
            elif 'customerNumber' in self.errors or \
                    'orderNumber' in self.errors:
                self.set_error(self.ERROR_CODE_UNKNOWN_ORDER,
                               _('No such order'))
            else:
                self.set_error(self.ERROR_CODE_INTERNAL,
                               _('Cannot process payment'))

            return data

        if self._make_md5() != data['md5']:
            self.set_error(self.ERROR_CODE_MD5, _('MD5 is incorrect'),
                           raise_error=True)

        if self._round(self.payment_obj.order_sum) != self._round(
                data['orderSumAmount']):
            self.set_error(self.ERROR_CODE_UNKNOWN_ORDER,
                           _("Sum doesn't match"), raise_error=True)

        return data 
Example 14
Project: esjspy   Author: Dothion   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def _gen_md5(identifier: str) -> str:
    hashed = hashlib.md5(identifier.encode('utf8'))
    return hashed.hexdigest() 
Example 15
Project: esjspy   Author: Dothion   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def _dump(identifier: str, some_obj: Any, cache_dir: Path = Path(CACHE_DIR), use_cache=True) -> None:
    if not use_cache:
        return
    md5 = _gen_md5(identifier)
    cache_dir = cache_dir / md5[0:2] / md5[2:4]
    if not cache_dir.exists():
        cache_dir.mkdir(parents=True)
    cache_path = cache_dir / md5
    with cache_path.open('wb') as f:
        pickle.dump(some_obj, f, protocol=pickle.HIGHEST_PROTOCOL)
    logger.debug('保存了 %s 的内容到缓存。' % identifier if LANGUAGE in _simplified else
                 '保存了 %s 的內容到 cache。' % identifier) 
Example 16
Project: esjspy   Author: Dothion   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def _scan(identifier: str, cache_dir: Path = Path(CACHE_DIR)) -> Any:
    md5 = _gen_md5(identifier)
    cache_dir = cache_dir / md5[0:2] / md5[2:4]
    cache_path = cache_dir / md5
    logger.debug('从缓存中读取了 %s 的内容。' % identifier if LANGUAGE in _simplified else
                 '從 cache 中讀取了 %s 的內容。' % identifier)
    with cache_path.open('rb') as f:
        return pickle.load(f) 
Example 17
Project: esjspy   Author: Dothion   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def _has_cached(identifier: str, cache_dir: Path = Path(CACHE_DIR), use_cache=True) -> bool:
    if not use_cache:
        return False
    md5 = _gen_md5(identifier)
    cache_dir = cache_dir / md5[0:2] / md5[2:4]
    cache_path = cache_dir / md5
    return cache_path.exists() 
Example 18
Project: jumpserver-python-sdk   Author: jumpserver   File: utils.py    GNU General Public License v2.0 5 votes vote down vote up
def content_md5(data):
    """计算data的MD5值,经过Base64编码并返回str类型。

    返回值可以直接作为HTTP Content-Type头部的值
    """
    if isinstance(data, str):
        data = hashlib.md5(data.encode('utf-8'))
    value = base64.b64encode(data.hexdigest().encode('utf-8'))
    return value.decode('utf-8') 
Example 19
Project: jumpserver-python-sdk   Author: jumpserver   File: models.py    GNU General Public License v2.0 5 votes vote down vote up
def private_key_file(self):
        if not self.key_dir:
            self.key_dir = '.'

        if not self.private_key:
            return None
        key_name = '.' + md5(self.private_key.encode('utf-8')).hexdigest()
        key_path = os.path.join(self.key_dir, key_name)
        if not os.path.exists(key_path):
            with open(key_path, 'w') as f:
                f.write(self.private_key)
            os.chmod(key_path, 0o400)
        return key_path 
Example 20
Project: model-api-sequence   Author: evandowning   File: color.py    GNU General Public License v3.0 5 votes vote down vote up
def api_md5(api):
    return unpack('BBB', md5(api).digest()[:3])

# Extract API sequences and convert them to pixels 
Example 21
Project: telegram-innovation-chatbot   Author: zaoldyeck   File: olami.py    MIT License 5 votes vote down vote up
def _gen_sign(self, api, timestamp_ms):
        data = self.app_secret + 'api=' + api + 'appkey=' + self.app_key + 'timestamp=' + \
               str(timestamp_ms) + self.app_secret
        return md5(data.encode('ascii')).hexdigest() 
Example 22
Project: malcode   Author: moonsea   File: filtersamemd5.py    GNU General Public License v3.0 5 votes vote down vote up
def getMd5(filename):
    if not os.path.isfile(filename):
        return

    filehash = hashlib.md5()
    f = file(filename, 'rb')
    while True:
        b = f.read(4096)
        if not b:
            break
        filehash.update(b)
    f.close()
    return filehash.hexdigest() 
Example 23
Project: malcode   Author: moonsea   File: virusgenasm.py    GNU General Public License v3.0 5 votes vote down vote up
def getMd5(filename):
    if not os.path.isfile(filename):
        return

    filehash = hashlib.md5()
    f = file(filename, 'rb')
    while True:
        b = f.read(4096)
        if not b:
            break
        filehash.update(b)
    f.close()
    return filehash.hexdigest() 
Example 24
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 25
Project: pyblish-win   Author: pyblish   File: test_urllib2_localnet.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _generate_nonce(self):
        self._request_num += 1
        nonce = hashlib.md5(str(self._request_num)).hexdigest()
        self._nonces.append(nonce)
        return nonce 
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_default_is_md5(self):
        # Testing if HMAC defaults to MD5 algorithm.
        # NOTE: this whitebox test depends on the hmac class internals
        h = hmac.HMAC("key")
        self.assertTrue(h.digest_cons == hashlib.md5) 
Example 27
Project: pyblish-win   Author: pyblish   File: uuid.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def uuid3(namespace, name):
    """Generate a UUID from the MD5 hash of a namespace UUID and a name."""
    from hashlib import md5
    hash = md5(namespace.bytes + name).digest()
    return UUID(bytes=hash[:16], version=3) 
Example 28
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: http.py    Apache License 2.0 5 votes vote down vote up
def generate_etag(data):
    """Generate an etag for some data."""
    return md5(data).hexdigest() 
Example 29
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: cache.py    Apache License 2.0 5 votes vote down vote up
def _get_filename(self, key):
        if isinstance(key, text_type):
            key = key.encode('utf-8') #XXX unicode review
        hash = md5(key).hexdigest()
        return os.path.join(self._path, hash) 
Example 30
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: http.py    Apache License 2.0 5 votes vote down vote up
def generate_etag(data):
    """Generate an etag for some data."""
    return md5(data).hexdigest() 
Example 31
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: cache.py    Apache License 2.0 5 votes vote down vote up
def _get_filename(self, key):
        if isinstance(key, text_type):
            key = key.encode('utf-8') #XXX unicode review
        hash = md5(key).hexdigest()
        return os.path.join(self._path, hash) 
Example 32
Project: everyclass-server   Author: everyclass   File: ics_generator.py    Mozilla Public License 2.0 5 votes vote down vote up
def _build_event(card_name: str, times: Tuple[datetime, datetime], classroom: str, teacher: str, current_week: int,
                 week_string: str, cid: str) -> Event:
    """
    生成 `Event` 对象

    :param card_name: 课程名
    :param times: 开始和结束时间
    :param classroom: 课程地点
    :param teacher: 任课教师
    :return: `Event` 对象
    """

    event = Event()
    event.add('transp', 'TRANSPARENT')
    summary = card_name
    if classroom != 'None':
        summary = card_name + '@' + classroom
        event.add('location', classroom)

    description = week_string
    if teacher != 'None':
        description += '\n教师:' + teacher
    description += '\n由 EveryClass 每课 (https://everyclass.xyz) 导入'

    event.add('summary', summary)
    event.add('description', description)
    event.add('dtstart', times[0])
    event.add('dtend', times[1])
    event.add('last-modified', datetime.now())

    # 使用"cid-当前周"作为事件的超码
    event_sk = cid + '-' + str(current_week)
    event['uid'] = hashlib.md5(event_sk.encode('utf-8')).hexdigest() + '@everyclass.xyz'
    alarm = Alarm()
    alarm.add('action', 'none')
    alarm.add('trigger', datetime(1980, 1, 1, 3, 5, 0))
    event.add_component(alarm)
    return event 
Example 33
Project: kvmd   Author: pikvm   File: test_main.py    GNU General Public License v3.0 5 votes vote down vote up
def _make_passwd(user: str) -> str:
    return hashlib.md5(user.encode()).hexdigest() 
Example 34
Project: AutoDL   Author: tanguofu   File: name_dict.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def md5(data):
        if isinstance(data, str):
            data = data.encode('utf-8')
        hash_md5 = hashlib.md5(data)
        return hash_md5.hexdigest() 
Example 35
Project: slidoc   Author: mitotic   File: multiproxy.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_random60(hex=False, integer=False):
    """Return "60-bit" randomness as 10 Base64URL characters (default) or 15 hex digits (or as an integer)
   Uses HMAC for safer randomness (using MD5 for speed)
    """
    randstr = struct.pack("Q", random.randint(0, (1 << 60)-1) )
    hmac_obj = hmac.new(str(get_random_hmac_key), randstr, digestmod=hashlib.md5)
    if integer:
        return struct.unpack("Q", hmac_obj.digest()[:8])[0]   # 64 bits of the MD5 HMAC
    elif hex:
        return hmac_obj.hexdigest()[:15]                   # 60 bits of the MD5 HMAC
    else:
        return b64url_encode(hmac_obj.digest()[:9])[:10]   # 60 bits of the MD5 HMAC 
Example 36
Project: slidoc   Author: mitotic   File: sdviewer.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gen_hmac_token(message):
    token = base64.b64encode(hmac.new(HMAC_KEY, message, hashlib.md5).digest())
    return token[:TRUNCATE_DIGEST] 
Example 37
Project: ieml   Author: IEMLdev   File: commons.py    GNU General Public License v3.0 5 votes vote down vote up
def cache_file(self) -> str:
        """
        :return: The cache file absolute path
        """
        res = b""
        for file in self.files:
            with open(file, 'rb') as fp:
                res += file[len(self.db_path)+1:].encode('utf8') + b":" + fp.read()

        return os.path.join(self.cache_folder, ".{}-cache.{}".format(self.name, hashlib.md5(res).hexdigest())) 
Example 38
Project: ieml   Author: IEMLdev   File: git_interface.py    GNU General Public License v3.0 5 votes vote down vote up
def get_local_cache_dir(origin):
    return os.path.join(user_cache_dir(appname='ieml', appauthor=False, version=LIBRARY_VERSION),
                        hashlib.md5(origin.encode('utf8')).hexdigest()) 
Example 39
Project: crawler   Author: fst034356   File: spider_meizitu.py    MIT License 5 votes vote down vote up
def save(self, img_url):
        name = md5(str(img_url).encode(encoding='utf-8')).hexdigest()
        img = download.get(self, img_url, 3)
        print('正在下载:{0}'.format(img_url))
        with open(str(name) + '.jpg', 'ab')as f:
            f.write(img.content) 
Example 40
Project: zmirror   Author: aploium   File: custom_func.sample.py    MIT License 5 votes vote down vote up
def demo__custom_identity_verify(identity_dict):
    """
    For CC98 identity verify

    :type identity_dict: dict
    """
    import hashlib
    import requests
    import config

    if 'cc98_username' not in identity_dict or 'cc98_password' not in identity_dict:
        return False

    try:
        pass_md5 = hashlib.md5()
        pass_md5.update(identity_dict['cc98_password'].encode())
        pass_md5 = pass_md5.hexdigest()
        if config.is_use_proxy:
            proxy = config.requests_proxies
        else:
            proxy = None
        r = requests.post('http://www.cc98.org/sign.asp', data={
            'a': 'i',
            'u': identity_dict['cc98_username'],
            'p': pass_md5,
            'userhidden': 2
        }, proxies=proxy)
        if r.text == '9898':
            return True
        else:
            return False
    except:
        return False


# Demo for Twitter 
Example 41
Project: plugin.video.lynda   Author: davejm   File: lynda_api.py    GNU General Public License v2.0 5 votes vote down vote up
def _make_hash(self, url):
        return hashlib.md5(self.HASH_KEY + url + str(int(time.time()))).hexdigest() 
Example 42
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: pipeline.py    Apache License 2.0 5 votes vote down vote up
def put(self,txt,final=False):
        """Start parsing a job, return id which can be used to retrieve the result"""
        batch_id=hashlib.md5((str(random.random())+txt).encode("utf-8")).hexdigest()
        self.q_in.put((batch_id,txt)) #first job of 1 total
        self.job_counter+=1
        if final:
            self.q_in.put(("FINAL",""))
        return batch_id 
Example 43
Project: sbo-templates   Author: dslackw   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def sourceCheckSum(self, source):
        """md5sum sources
        """
        with open(self.pwd + source, "rb") as f:
            data = f.read()
            return hashlib.md5(data).hexdigest() 
Example 44
Project: subword-qac   Author: clovaai   File: dataset.py    MIT License 5 votes vote down vote up
def get_prefix_len(query, min_prefix_len, min_suffix_len, n=0):
    hasher = hashlib.md5()
    hasher.update(query.encode('utf-8'))
    hasher.update(str(n).encode('utf-8'))
    prefix_len = min_prefix_len + int(hasher.hexdigest(), 16) % (len(query) - min_prefix_len - min_suffix_len + 1)
    # if query[prefix_len - 1] == ' ': prefix_len -= 1
    return prefix_len 
Example 45
Project: neural-fingerprinting   Author: StephanZheng   File: version.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dev_version():
    """
    Returns a hexdigest of all the python files in the module.
    """

    md5_hash = hashlib.md5()
    py_files = sorted(list_files(suffix=".py"))
    if not py_files:
        return ''
    for filename in py_files:
        with open(filename, 'rb') as fobj:
            content = fobj.read()
        md5_hash.update(content)
    return md5_hash.hexdigest() 
Example 46
Project: models   Author: kipoi   File: upload_add_dataloaders.py    MIT License 5 votes vote down vote up
def get_md5(fpath):
    assert os.path.isfile(fpath)
    md5o = hashlib.md5()
    with open(fpath, 'rb') as f:
        # read in 1MB chunks
        for chunk in iter(lambda: f.read(1024 * 1024), b''):
            md5o.update(chunk)
    return md5o.hexdigest() 
Example 47
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: database.py    MIT License 5 votes vote down vote up
def get_hash(self, data, hasher=None):
        """
        Get the hash of some data, using a particular hash algorithm, if
        specified.

        :param data: The data to be hashed.
        :type data: bytes
        :param hasher: The name of a hash implementation, supported by hashlib,
                       or ``None``. Examples of valid values are ``'sha1'``,
                       ``'sha224'``, ``'sha384'``, '``sha256'``, ``'md5'`` and
                       ``'sha512'``. If no hasher is specified, the ``hasher``
                       attribute of the :class:`InstalledDistribution` instance
                       is used. If the hasher is determined to be ``None``, MD5
                       is used as the hashing algorithm.
        :returns: The hash of the data. If a hasher was explicitly specified,
                  the returned hash will be prefixed with the specified hasher
                  followed by '='.
        :rtype: str
        """
        if hasher is None:
            hasher = self.hasher
        if hasher is None:
            hasher = hashlib.md5
            prefix = ''
        else:
            hasher = getattr(hashlib, hasher)
            prefix = '%s=' % self.hasher
        digest = hasher(data).digest()
        digest = base64.urlsafe_b64encode(digest).rstrip(b'=').decode('ascii')
        return '%s%s' % (prefix, digest) 
Example 48
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: database.py    MIT License 5 votes vote down vote up
def list_installed_files(self):
        """
        Iterates over the ``installed-files.txt`` entries and returns a tuple
        ``(path, hash, size)`` for each line.

        :returns: a list of (path, hash, size)
        """

        def _md5(path):
            f = open(path, 'rb')
            try:
                content = f.read()
            finally:
                f.close()
            return hashlib.md5(content).hexdigest()

        def _size(path):
            return os.stat(path).st_size

        record_path = os.path.join(self.path, 'installed-files.txt')
        result = []
        if os.path.exists(record_path):
            with codecs.open(record_path, 'r', encoding='utf-8') as f:
                for line in f:
                    line = line.strip()
                    p = os.path.normpath(os.path.join(self.path, line))
                    # "./" is present as a marker between installed files
                    # and installation metadata files
                    if not os.path.exists(p):
                        logger.warning('Non-existent file: %s', p)
                        if p.endswith(('.pyc', '.pyo')):
                            continue
                        #otherwise fall through and fail
                    if not os.path.isdir(p):
                        result.append((p, _md5(p), _size(p)))
            result.append((record_path, None, None))
        return result 
Example 49
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: postgresql_user.py    MIT License 5 votes vote down vote up
def user_should_we_change_password(current_role_attrs, user, password, encrypted):
    """Check if we should change the user's password.

    Compare the proposed password with the existing one, comparing
    hashes if encrypted. If we can't access it assume yes.
    """

    if current_role_attrs is None:
        # on some databases, E.g. AWS RDS instances, there is no access to
        # the pg_authid relation to check the pre-existing password, so we
        # just assume password is different
        return True

    # Do we actually need to do anything?
    pwchanging = False
    if password is not None:
        # 32: MD5 hashes are represented as a sequence of 32 hexadecimal digits
        #  3: The size of the 'md5' prefix
        # When the provided password looks like a MD5-hash, value of
        # 'encrypted' is ignored.
        if ((password.startswith('md5') and len(password) == 32 + 3) or encrypted == 'UNENCRYPTED'):
            if password != current_role_attrs['rolpassword']:
                pwchanging = True
        elif encrypted == 'ENCRYPTED':
            hashed_password = 'md5{0}'.format(md5(to_bytes(password) + to_bytes(user)).hexdigest())
            if hashed_password != current_role_attrs['rolpassword']:
                pwchanging = True

    return pwchanging 
Example 50
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: digital_ocean_sshkey.py    MIT License 5 votes vote down vote up
def ssh_key_fingerprint(ssh_pub_key):
    key = ssh_pub_key.split(None, 2)[1]
    fingerprint = hashlib.md5(base64.decodestring(key)).hexdigest()
    return ':'.join(a + b for a, b in zip(fingerprint[::2], fingerprint[1::2]))