zoukankan      html  css  js  c++  java
  • hashlib 模块

    hash:一种算法 ,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

    三个特点:
     1.内容相同则hash运算结果相同,内容稍微改变则hash值则变
     2.不可逆推
     3.相同算法:无论校验多长的数据,得到的哈希值长度固定。

    import hashlib
    m=hashlib.md5()# m=hashlib.sha256()
    
    m.update('hello'.encode('utf8'))
    print(m.hexdigest())  #5d41402abc4b2a76b9719d911017c592
    
    m.update('alvin'.encode('utf8'))
    print(m.hexdigest())  #92a7e713c30abbb0319fa07da2a5c4af
    
    m2=hashlib.md5()
    m2.update('helloalvin'.encode('utf8'))
    print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af
    '''
    注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样
    但是update多次为校验大文件提供了可能。
    '''

    以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密

    # 解决方式一(建议使用)
    import hashlib
    m=hashlib.sha512()
    m=hashlib.md5('一行白鹭上青天'.encode('utf-8')) #内容前加内容
    m.update('alex3714'.encode('utf-8'))
    m.update('两个黄鹂鸣翠柳'.encode('utf-8')) ) #内容后加内容
    print(m.hexdigest())
    
    # 解决方式二,加长度(不建议使用)
    import hashlib
    # ######## 256 ########
    hash = hashlib.sha256('898oaFs09f'.encode('utf8'))
    hash.update('alvin'.encode('utf8'))
    print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7
    #解决方式三:python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密
    import hmac
    h = hmac.new('alvin'.encode('utf8'))
    h.update('hello'.encode('utf8'))
    print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940
    
    #要想保证hmac最终结果一致,必须保证:
    #1:hmac.new括号内指定的初始key一样
    #2:无论update多少次,校验的内容累加到一起是一样的内容
    import hmac
    
    h1=hmac.new(b'egon')
    h1.update(b'hello')
    h1.update(b'world')
    print(h1.hexdigest()) # f1bf38d054691688f89dcd34ac3c27f2
    
    h2=hmac.new(b'egon') 
    h2.update(b'helloworld') 
    print(h2.hexdigest()) # f1bf38d054691688f89dcd34ac3c27f2
    
    h3=hmac.new(b'egonhelloworld')
    print(h3.hexdigest()) # bcca84edd9eeb86f30539922b28f3981
    import hashlib
    passwds=[
        'alex3714',
        'alex1313',
        'alex94139413',
        'alex123456',
        '123456alex',
        'a123lex',
        ]
    def make_passwd_dic(passwds):
        dic={}
        for passwd in passwds:
            m=hashlib.md5()
            m.update(passwd.encode('utf-8'))
            dic[passwd]=m.hexdigest()
        return dic
    
    def break_code(cryptograph,passwd_dic):
        for k,v in passwd_dic.items():
            if v == cryptograph:
                print('密码是===>33[46m%s33[0m' %k)
    
    cryptograph='aee949757a2e698417463d47acac93df'
    break_code(cryptograph,make_passwd_dic(passwds))
    复制代码
    12 subprocess 模块
    复制代码
    import  subprocess 
    '''
    sh-3.2# ls /Users/egon/Desktop |grep txt$
    mysql.txt
    tt.txt
    事物.txt
    '''
    #1 Linux下,通过python运行终端代码:
    res1=subprocess.Popen('ls /Users/jieli/Desktop',
                          shell=True,
                          stdout=subprocess.PIPE)
    res=subprocess.Popen('grep txt$',
                          shell=True,
                          stdin=res1.stdout,  # res1.stdout是res.stdin
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)  
    print(res.stdout.read().decode('utf-8'))
    print(res.stderr.read().decode('utf-8'))
    #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
    res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',
                          shell=True,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    print(res1.stdout.read().decode('utf-8'))
    print(res.stderr.read().decode('utf-8'))
    
    #2 windows下,通过python运行终端代码:
    # dir | findstr 'test*'
    # dir | findstr 'txt$'
    import subprocess
    res1=subprocess.Popen(r'dir C:UsersAdministratorPycharmProjects	est函数备课',shell=True,stdout=subprocess.PIPE)
    res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
                     stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    
    print(res.stdout.read().decode('gbk')) 
    print(res.stderr.read().decode('gbk')) #subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码
    模拟撞库破解密码
  • 相关阅读:
    centos7下安装erlang
    centos7下升级git版本
    pytest失败重跑插件: pytest-rerunfailures使用与坑(全网独家精华)
    pytest-assume插件(全网最详细解释):多重断言执行
    pytest踩坑记:NameError: name 'pytest' is not defined
    pytest-ordering:指定pytest的case运行顺序的插件
    pytest中print的坑
    pytest测试入门篇(ExitCode退出码)
    httprunner3.x遇到的问题(hrun make报错)
    httprunner3.x(入门介绍篇)
  • 原文地址:https://www.cnblogs.com/snailgirl/p/9417359.html
Copyright © 2011-2022 走看看