The Python common module (E) and module collections module hashlib

5.8 hashlib module

  1. hashlib Module Description: This module was called digest algorithm, also known as the encryption algorithm or hash algorithm, hash algorithm, etc., mainly for the password was encrypted file consistency check

  2. Encryption methods are: MD5, sha1, sha25, sha512 larger the number, the more complex encryption method, the higher the security, but the efficiency will be slower.

  3. Ordinary encryption

    import hashlib
    
    md5 = hashlib.md5()
    md5.update('123456'.encode('utf-8')) # 必须是bytes类型才能够进行加密
    print(md5.hexdigest())
    
    # 计算结果如下:
    'e10adc3949ba59abbe56e057f20f883e'
  4. Salt Encryption

    • Introduction: What is salt? The word salt comes from abroad, foreigners from the name I think is very casual, the name comes from the barbecue, commonly known as the BBQ. We grilled the time, usually in the instant that they will sprinkle salt to the chicken above, add flavor, then sprinkle salt process, foreigners considered to be more complex, so it will be more complex encryption method called salt.

    • Fixed salt

      ret = hashlib.md5('xx教育'.encode('utf-8'))  # xx教育就是固定的盐
      ret.update('a'.encode('utf-8'))
      print(ret.hexdigest())
    • Dynamic salt

      username = '宝元666'
      ret = hashlib.md5(username[::2].encode('utf-8'))  # 针对于每个账户,每个账户的盐都不一样
      ret.update('a'.encode('utf-8'))
      print(ret.hexdigest())
  5. Check the consistency of the file: hashlib module can be used in addition to password encryption, there are a common feature, that is the consistency checking files.

    #如果文件过大,全部读取出来直接就会撑爆内存的,所以我们要分段读取
    import hashlib
    # 直接 update
    md5obj = hashlib.md5()
    md5obj.update('宝元 is a old driver'.encode('utf-8'))
    print(md5obj.hexdigest())  # da525c66739e6baa8729332f8bae8e0f
    
    # 分段update
    md5obj = hashlib.md5()
    md5obj.update('宝元 '.encode('utf-8'))
    md5obj.update('is '.encode('utf-8'))
    md5obj.update('a '.encode('utf-8'))
    md5obj.update('old '.encode('utf-8'))
    md5obj.update('driver'.encode('utf-8'))
    print(md5obj.hexdigest())  # da525c66739e6baa8729332f8bae8e0f
    # 结果相同
  6. Tall version of the file checksums

    #校验Pyhton解释器的Md5值是否相同
    import hashlib
    
    def file_check(file_path):
        with open(file_path,mode='rb') as f1:
            sha256 = hashlib.md5()
            while 1:
                content = f1.read(1024)
                if content:
                    sha256.update(content)
                else:
                    return sha256.hexdigest()
    print(file_check('python-3.6.6-amd64.exe'))

5.9 collections module

  1. Description: On the basis of built-in data types (dict, list, set, tuple) on, collections module provides several additional data types: Counter, deque, defaultdict, namedtuple and OrderedDict and so on.

  2. namedtuple: generation can use the name to access the content of the element tuple

    #一个点的二维坐标就可以表示成:
    p = (1, 2)
    #但是,看到(1, 2),很难看出这个tuple是用来表示一个坐标的。
    from collections import namedtuple
    Point = namedtuple('Point', ['x', 'y'])
    p = Point(1, 2)
    print(p)
    #结果为:Point(x=1, y=2)
  3. deque: deque, from the other side can be quickly and additional objects Release

    #使用list存储数据时,按索引访问元素很快,但是插入和删除元素就很慢了,因为list是线性存储,数据量大的时候,插入和删除效率很低。
    #deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈:
    from collections import deque
    q = deque(['a', 'b', 'c'])
    q.append('x')
    q.appendleft('y')
    q
    deque(['y', 'a', 'b', 'c', 'x'])
    #deque除了实现list的append()和pop()外,还支持appendleft()和popleft(),这样就可以非常高效地往头部添加或删除元素。
  4. Counter: a counter for counting the main

    #Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)。Counter类和其他语言的bags或multisets很相似。
    c = Counter('abcdeabcdabcaba')
    print c
    输出:Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
  5. OrderedDict: ordered dictionary

    #使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。
    #如果要保持Key的顺序,可以用OrderedDict:
    from collections import OrderedDict
    d = dict([('a', 1), ('b', 2), ('c', 3)]) # 另一种定义字典的方式
    print(d)
    # 结果:
    {'a': 1, 'c': 3, 'b': 2}
    
    od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
    print(od)
    # 结果:
    OrderedDict([('a', 1), ('b', 2), ('c', 3)])
  6. defaultdict: Dictionary with default values

    #有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
    #即: {'k1': 大于66 , 'k2': 小于66}
    li = [11,22,33,44,55,77,88,99,90]
    result = {}
    for row in li:
        if row > 66:
            if 'key1' not in result:
                result['key1'] = []
            result['key1'].append(row)
        else:
            if 'key2' not in result:
                result['key2'] = []
            result['key2'].append(row)
    print(result)
    
    
    from collections import defaultdict
    values = [11, 22, 33,44,55,66,77,88,99,90]
    my_dict = defaultdict(list)
    
    for value in  values:
        if value>66:
            my_dict['k1'].append(value)
        else:
            my_dict['k2'].append(value)
    #使用dict时,如果引用的Key不存在,就会抛出KeyError。如果希望key不存在时,返回一个默认值,就可以用defaultdict:
    from collections import defaultdict
    dd = defaultdict(lambda: 'N/A')
    dd['key1'] = 'abc'
     # key1存在
    print(dd['key1'])
    dd['key2'] # key2不存在,返回默认值
    print(dd['key2'])

Guess you like

Origin www.cnblogs.com/zhangdadayou/p/11415285.html