Python hashlib.md5() Examples

The following are 30 code examples of hashlib.md5(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module hashlib , or try the search function .
Example #1
Source File: io.py    From vergeml with MIT License 8 votes vote down vote up
def hash(self, state: str) -> str:
        """Generate a hash representing the current sample state.

        :param state: string capturing the current system configuration state

        This function must be overridden to generate a meaningful hash for the current
        set of input samples."""

        newstate = io.BytesIO(state.encode('utf-8'))

        for k in ('input_patterns', 'samples_dir', 'val_dir', 'val_num', 'val_perc',
                  'test_dir', 'test_num', 'test_perc', 'random_seed'):
            newstate.write(str(getattr(self, k)).encode('utf-8'))

        md5 = hashlib.md5()
        md5.update(newstate.getvalue())
        return md5.hexdigest() 
Example #2
Source File: 视频特殊处理.py    From tools_python with Apache License 2.0 8 votes vote down vote up
def get_file_md5_2(file_path):
    """
    分段读取,获取文件的md5值
    :param file_path:
    :return:
    """
    with open(file_path, 'rb') as file:
        md5_obj = hashlib.md5()
        while True:
            buffer = file.read(8096)
            if not buffer:
                break
            md5_obj.update(buffer)
        hash_code = md5_obj.hexdigest()
    md5 = str(hash_code).lower()
    return md5 
Example #3
Source File: sqldb.py    From Vxscan with Apache License 2.0 8 votes vote down vote up
def create_webinfo_db(self):
        try:
            cursor = self.conn.cursor()
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS webinfo (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                time varchar(255),
                domain varchar(255),
                waf varchar(255) DEFAULT '',
                title varchar(255) DEFAULT '',
                apps varchar(255) DEFAULT '',
                server varchar(255) DEFAULT '',
                address varchar(255) DEFAULT '',
                ipaddr varchar(255) DEFAULT '',
                os varchar(255) DEFAULT '',
                md5 varchar(40) UNIQUE
                )
                """)
        except sqlite3.OperationalError as e:
            pass 
Example #4
Source File: database.py    From pgrepup with GNU General Public License v3.0 7 votes vote down vote up
def create_user(conn, username, password):
    try:
        cur = conn.cursor()
        cur.execute("SELECT passwd FROM pg_shadow WHERE usename = %s", [username])
        row = cur.fetchone()
        if row:
            m = hashlib.md5()
            m.update(password + username)
            encrypted_password = "md5" + m.hexdigest()
            if encrypted_password != row[0]:
                cur.execute("ALTER USER " + username + " ENCRYPTED PASSWORD %s SUPERUSER REPLICATION", [password])
        else:
            cur.execute("CREATE USER " + username + " WITH ENCRYPTED PASSWORD %s SUPERUSER REPLICATION", [password])
        conn.commit()
        return True
    except psycopg2.Error as e:
        print(e)
        conn.rollback()
        return False 
Example #5
Source File: BrownCompleteness.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, Nplanets=1e8, **specs):
        
        # bring in inherited Completeness prototype __init__ values
        Completeness.__init__(self, **specs)
        
        # Number of planets to sample
        self.Nplanets = int(Nplanets)
       
        # get path to completeness interpolant stored in a pickled .comp file
        self.filename = self.PlanetPopulation.__class__.__name__ + self.PlanetPhysicalModel.__class__.__name__ + self.__class__.__name__

        # get path to dynamic completeness array in a pickled .dcomp file
        self.dfilename = self.PlanetPopulation.__class__.__name__ + \
                         self.PlanetPhysicalModel.__class__.__name__ +\
                         specs['modules']['OpticalSystem'] + \
                         specs['modules']['StarCatalog'] + \
                         specs['modules']['TargetList']
        atts = list(self.PlanetPopulation.__dict__)
        self.extstr = ''
        for att in sorted(atts, key=str.lower):
            if not callable(getattr(self.PlanetPopulation, att)) and att != 'PlanetPhysicalModel':
                self.extstr += '%s: ' % att + str(getattr(self.PlanetPopulation, att)) + ' '
        ext = hashlib.md5(self.extstr.encode("utf-8")).hexdigest()
        self.filename += ext
        self.filename.replace(" ","") #Remove spaces from string (in the case of prototype use) 
Example #6
Source File: update.py    From CAMISIM with Apache License 2.0 6 votes vote down vote up
def verifyChecksumForDir(dirName, settings, type):
    """
        For each file listed in a list, verifies its checksum.
    """

    dirName = os.path.join(settings.getLocalDst(type), dirName)

    urlChecksumFile = settings.getRemoteSrc() + '/' + os.path.basename(dirName) + '.checksum'
    print("Verification of the decompressed directory '%s' started." % dirName)
    try:
        for line in urllib2.urlopen(urlChecksumFile):
            f, checksum = line.split('\t')
            f = os.path.join(dirName, f)
            # if checksum.strip() != hashlib.md5(open(f).read()).hexdigest():
            if checksum.strip() != getChecksumForFile(f):
                raise Exception("File '%s' is corrupted, it has a wrong checksum." % f)
    except Exception as e:
        print("Unable to verify directory: %s" % dirName)
        raise e

    print("Checksum verification completed successfully!") 
Example #7
Source File: mmbot.py    From MaliciousMacroBot with MIT License 6 votes vote down vote up
def get_file_meta_data(self, filepath, filename=None, getHash=False):
        """
        helper function to get meta information about a file to include it's path, date modified, size
        :param filepath: path to a file
        :param filename: filename
        :param getHash: whether or not the hash should be computed
        :return: a tuple of format (filename, filepath, filesize, filemodified, md5)
        """
        if filename is None:
            filename = os.path.split(filepath)[1]

        filemodified = time.ctime(os.path.getmtime(filepath))
        filesize = os.path.getsize(filepath)
        md5 = np.nan
        if getHash:
            md5 = self.get_file_hash(filepath)
        return (filename, filepath, filesize, filemodified, md5) 
Example #8
Source File: sms.py    From Servo with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def send(self):
        pwhash = md5(self.conf['sms_http_password']).hexdigest()

        params = {
            'username'  : self.conf['sms_http_user'],
            'password'  : pwhash,
            'message'   : self.body,
            'from'      : self.sender,
            'to'        : self.recipient,
        }

        if self.msg:
            dlruri = settings.SERVO_URL + '/api/messages/?id={0}'.format(self.msg.code)
            params['notify_url'] = dlruri

        params = urllib.urlencode(params)
        r = urllib.urlopen(self.URL, params, context=_create_unverified_context()).read()

        if 'ERROR:' in r:
            raise ValueError(self.ERRORS.get(r, _('Unknown error (%s)') % r)) 
Example #9
Source File: sqldb.py    From Vxscan with Apache License 2.0 6 votes vote down vote up
def create_ports(self):
        try:
            cursor = self.conn.cursor()
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS ports (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                time varchar(255),
                ipaddr varchar(255),
                service varchar(255) DEFAULT '',
                port varchar(255) DEFAULT '',
                banner varchar(255) DEFAULT '',
                md5 varchar(40) UNIQUE
                )
                """)
        except Exception as e:
            pass 
Example #10
Source File: sqldb.py    From Vxscan with Apache License 2.0 6 votes vote down vote up
def create_urls(self):
        try:
            cursor = self.conn.cursor()
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS urls (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                time varchar(255),
                domain varchar(255) DEFAULT '',
                title varchar(255) DEFAULT '',
                url varchar(255) DEFAULT '',
                contype varchar(255) DEFAULT '',
                rsp_len varchar(255) DEFAULT '',
                rsp_code varchar(255) DEFAULT '',
                md5 varchar(40) UNIQUE
                )
                """)
        except Exception as e:
            logging.exception(e) 
Example #11
Source File: customservice.py    From wechatpy with MIT License 6 votes vote down vote up
def update_account(self, account, nickname, password):
        """
        更新客服账号
        详情请参考
        http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html

        :param account: 完整客服账号,格式为:账号前缀@公众号微信号
        :param nickname: 客服昵称,最长6个汉字或12个英文字符
        :param password: 客服账号登录密码
        :return: 返回的 JSON 数据包
        """
        password = to_binary(password)
        password = hashlib.md5(password).hexdigest()
        return self._post(
            "https://api.weixin.qq.com/customservice/kfaccount/update",
            data={"kf_account": account, "nickname": nickname, "password": password},
        ) 
Example #12
Source File: customservice.py    From wechatpy with MIT License 6 votes vote down vote up
def add_account(self, account, nickname, password):
        """
        添加客服账号
        详情请参考
        http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html

        :param account: 完整客服账号,格式为:账号前缀@公众号微信号
        :param nickname: 客服昵称,最长6个汉字或12个英文字符
        :param password: 客服账号登录密码
        :return: 返回的 JSON 数据包
        """
        password = to_binary(password)
        password = hashlib.md5(password).hexdigest()
        return self._post(
            "https://api.weixin.qq.com/customservice/kfaccount/add",
            data={"kf_account": account, "nickname": nickname, "password": password},
        ) 
Example #13
Source File: api.py    From django-payfast with MIT License 6 votes vote down vote up
def _sign_fields(signable_fields):  # type: (SignableFields) -> str
    """
    Common signing code.
    """
    for (k, v) in signable_fields:
        assert isinstance(k, str), repr(k)
        assert isinstance(v, str), repr(v)

    if sys.version_info < (3,):
        # Python 2 doesn't do IRI encoding.
        text = urlencode([
            (k.encode('utf-8'), v.encode('utf-8'))
            for (k, v) in signable_fields
        ])
    else:
        text = urlencode(signable_fields, encoding='utf-8', errors='strict')

    return md5(text.encode('ascii')).hexdigest()


#: The checkout signature should ignore these leading and trailing whitespace characters.
#:
#: This list is an educated guess based on the PHP trim() function.
#: 
Example #14
Source File: deckhand.py    From drydock with 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 #15
Source File: sqldb.py    From Vxscan with Apache License 2.0 6 votes vote down vote up
def get_ports(self, ipaddr, ports):
        self.create_ports()
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        for i in ports:
            service = i.get('server')
            port = i.get('port')
            banner = i.get('banner')
            banner = re.sub('<', '', banner)
            banner = re.sub('>', '', banner)
            md5sum = hashlib.md5()
            strings = str(ipaddr) + str(service) + str(port)
            md5sum.update(strings.encode('utf-8'))
            md5 = md5sum.hexdigest()
            values = (timestamp, ipaddr, service, port, banner, md5)
            query = "INSERT OR IGNORE INTO ports (time, ipaddr, service, port, banner,md5) VALUES (?,?,?,?,?,?)"
            self.insert_ports(query, values) 
Example #16
Source File: antigravity.py    From jawfish with MIT License 5 votes vote down vote up
def geohash(latitude, longitude, datedow):
    '''Compute geohash() using the Munroe algorithm.

    >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
    37.857713 -122.544543

    '''
    # http://xkcd.com/426/
    h = hashlib.md5(datedow).hexdigest()
    p, q = [('%f' % float.fromhex('0.' + x)) for x in (h[:16], h[16:32])]
    print('%d%s %d%s' % (latitude, p[1:], longitude, q[1:])) 
Example #17
Source File: ssl_.py    From jawfish with MIT License 5 votes vote down vote up
def assert_fingerprint(cert, fingerprint):
    """
    Checks if given fingerprint matches the supplied certificate.

    :param cert:
        Certificate as bytes object.
    :param fingerprint:
        Fingerprint as string of hexdigits, can be interspersed by colons.
    """

    # Maps the length of a digest to a possible hash function producing
    # this digest.
    hashfunc_map = {
        16: md5,
        20: sha1,
        32: sha256,
    }

    fingerprint = fingerprint.replace(':', '').lower()
    digest_length, odd = divmod(len(fingerprint), 2)

    if odd or digest_length not in hashfunc_map:
        raise SSLError('Fingerprint is of invalid length.')

    # We need encode() here for py32; works on py2 and p33.
    fingerprint_bytes = unhexlify(fingerprint.encode())

    hashfunc = hashfunc_map[digest_length]

    cert_digest = hashfunc(cert).digest()

    if not cert_digest == fingerprint_bytes:
        raise SSLError('Fingerprints did not match. Expected "{0}", got "{1}".'
                       .format(hexlify(fingerprint_bytes),
                               hexlify(cert_digest))) 
Example #18
Source File: verify.py    From Vxscan with Apache License 2.0 5 votes vote down vote up
def get_md5():
    plain = ''.join([random.choice('0123456789') for _ in range(8)])
    md5sum = hashlib.md5()
    md5sum.update(plain.encode('utf-8'))
    md5 = md5sum.hexdigest()
    return [plain, md5] 
Example #19
Source File: master.py    From pokemon with MIT License 5 votes vote down vote up
def get_trainer(name):
    """return the unique id for a trainer, determined by the md5 sum
    """
    name = name.lower()
    return int(hashlib.md5(name.encode("utf-8")).hexdigest(), 16) % 10 ** 8 
Example #20
Source File: core.py    From gazetteer with MIT License 5 votes vote down vote up
def _id(uri):
    return hashlib.md5(uri).hexdigest()[:16] 
Example #21
Source File: mmbot.py    From MaliciousMacroBot with MIT License 5 votes vote down vote up
def new_samples(self, existing, possiblenew):
        """
        Returns dataframe containing rows from possiblenew with MD5 hashes that are not in existing, to identify
        new file samples.
        :param existing: dataframe containing an 'md5' field
        :param possiblenew: dataframe containing an 'md5' field
        :return: Returns dataframe containing rows from possiblenew with MD5 hashes that are not in existing.
        """
        existing_items = existing['md5'].tolist()
        possiblenew_items = possiblenew['md5'].tolist()
        actualnew_items = [x for x in possiblenew_items if x not in existing_items]
        if len(actualnew_items) > 0:
            return possiblenew[possiblenew['md5'].isin(actualnew_items)].copy()
        return None 
Example #22
Source File: test_skill_handler.py    From botbuilder-python with MIT License 5 votes vote down vote up
def create_skill_conversation_id(  # pylint: disable=W0221
        self, conversation_reference: ConversationReference
    ) -> str:
        cr_json = json.dumps(conversation_reference.serialize())

        key = hashlib.md5(
            f"{conversation_reference.conversation.id}{conversation_reference.service_url}".encode()
        ).hexdigest()

        if key not in self._conversation_refs:
            self._conversation_refs[key] = cr_json

        return key 
Example #23
Source File: tests.py    From gazetteer with MIT License 5 votes vote down vote up
def test_update_with_no_id(self):
        #creare new place, get the generated ID for it
        new_place = json.loads('{"relationships": [], "admin": [], "updated": "2006-01-15T01:00:00+01:00", \
        "name": "simple place name", "geometry": {"type": "Point", "coordinates": [-114.43359375, 44.033203125]}, \
        "alternate" : [{"lang":"en","type":"preferred","name":"Vokey alt"}], "is_primary": true, \
        "uris": ["example.com/place#newplace123"], "feature_code": "BLDG", "centroid": [-114.43359375, 44.033203125], \
        "timeframe": {"end_range": 0,"start": "1800-01-01","end": "1900-01-01","start_range": 0 }}')
        import hashlib
        new_place_id = hashlib.md5("example.com/place#newplace123").hexdigest()[:16]
        self.conn.index("gaz-test-index", "place", new_place, id=new_place_id, metadata={"user_created": "test program"})
        self.conn.refresh(["gaz-test-index"])
        
        place = Place.objects.get(new_place_id)
        self.assertEqual(place.name,"simple place name")
        self.assertEqual(place.feature_code,"BLDG")
        self.assertEqual(place.alternate[0]["name"], "Vokey alt")
        
        self.batch_import_update_no_id.start_import = True
        self.batch_import_update_no_id.save()

        self.conn.refresh(["gaz-test-index"])
        place = Place.objects.get(new_place_id)
        self.assertEqual(place.name,"changing name and fcode with no id")
        self.assertEqual(place.feature_code,"DAM")
        self.assertEqual(place.alternate, [])  #alternate names are cleared
        self.assertEqual(place.centroid, [-114.43359375, 44.033203125]) # centroid not overwritten 
Example #24
Source File: utils.py    From audio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def validate_file(file_obj: Any, hash_value: str, hash_type: str = "sha256") -> bool:
    """Validate a given file object with its hash.

    Args:
        file_obj: File object to read from.
        hash_value (str): Hash for url.
        hash_type (str, optional): Hash type, among "sha256" and "md5" (Default: ``"sha256"``).

    Returns:
        bool: return True if its a valid file, else False.
    """

    if hash_type == "sha256":
        hash_func = hashlib.sha256()
    elif hash_type == "md5":
        hash_func = hashlib.md5()
    else:
        raise ValueError

    while True:
        # Read by chunk to avoid filling memory
        chunk = file_obj.read(1024 ** 2)
        if not chunk:
            break
        hash_func.update(chunk)

    return hash_func.hexdigest() == hash_value 
Example #25
Source File: addons_xml_generator_new.py    From tdw with GNU General Public License v3.0 5 votes vote down vote up
def _generate_md5_file(self):
        # create a new md5 hash
        with open('addons.xml', 'r', encoding='UTF-8') as fo:
            m = hashlib.md5(fo.read().encode('UTF-8')).hexdigest()

        # save file
        try:
            self._save_file(m.encode('UTF-8'), file='addons.xml.md5')
        except:
            # oops
            print('An error occurred creating addons.xml.md5 file!')
            print_exc() 
Example #26
Source File: addons_xml_generator_new.py    From tdw with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        # generate files
        self._generate_addons_file()
        self._generate_md5_file()
        # notify user
        print('Finished updating addons xml and md5 files') 
Example #27
Source File: request.py    From certidude with MIT License 5 votes vote down vote up
def on_get(self, req, resp, cn):
        """
        Fetch certificate signing request as PEM
        """

        try:
            path, buf, _, submitted = self.authority.get_request(cn)
        except errors.RequestDoesNotExist:
            logger.warning("Failed to serve non-existant request %s to %s",
                cn, req.context.get("remote_addr"))
            raise falcon.HTTPNotFound()

        resp.set_header("Content-Type", "application/pkcs10")
        logger.debug("Signing request %s was downloaded by %s",
            cn, req.context.get("remote_addr"))

        preferred_type = req.client_prefers(("application/json", "application/x-pem-file"))

        if preferred_type == "application/x-pem-file":
            # For certidude client, curl scripts etc
            resp.set_header("Content-Type", "application/x-pem-file")
            resp.set_header("Content-Disposition", ("attachment; filename=%s.pem" % cn))
            resp.body = buf
        elif preferred_type == "application/json":
            # For web interface events
            resp.set_header("Content-Type", "application/json")
            resp.set_header("Content-Disposition", ("attachment; filename=%s.json" % cn))
            resp.body = json.dumps(dict(
                submitted = submitted,
                common_name = cn,
                address = getxattr(path, "user.request.address").decode("ascii"), # TODO: move to authority.py
                md5sum = hashlib.md5(buf).hexdigest(),
                sha1sum = hashlib.sha1(buf).hexdigest(),
                sha256sum = hashlib.sha256(buf).hexdigest(),
                sha512sum = hashlib.sha512(buf).hexdigest()), cls=MyEncoder)
        else:
            raise falcon.HTTPUnsupportedMediaType(
                "Client did not accept application/json or application/x-pem-file") 
Example #28
Source File: filecache.py    From cutout with MIT License 5 votes vote down vote up
def _get_filename(self, key):
        hash = md5(key.encode("utf8")).hexdigest()
        return os.path.join(self._path, hash) 
Example #29
Source File: 视频特殊处理.py    From tools_python with Apache License 2.0 5 votes vote down vote up
def modify_file_md5(file_path):
    """
    修改文件的md5值
    :param file_path:
    :return:
    """
    with open(file_path, 'a') as file:
        file.write("####&&&&") 
Example #30
Source File: 视频特殊处理.py    From tools_python with Apache License 2.0 5 votes vote down vote up
def get_file_md5(file_path):
    """
    获取文件的MD5值
    :param file_path:
    :return:
    """
    with open(file_path, 'rb') as file:
        temp_md5 = hashlib.md5()
        temp_md5.update(file.read())
        hash_code = str(temp_md5.hexdigest()).lower()
    return hash_code