(了解)configparser,hashlib,hamc模块

configparser模块 #专门解析my.ini这种形式的文件(cnf)    

  import configparser
  config=configparser.ConfigParser()
  config.read('my.ini')
  格式:[section1]
    key=value
    key=value
    key=value
    [section2]
    key=value
    key=value
  section和option   
   #删除整个标题section2
   config.remove_section('section2')
   #删除标题section1下的某个k1和k2
   config.remove_option('section1','k1')
   config.remove_option('section1','k2')
   #判断是否存在某个标题
   print(config.has_section('section1'))
   #判断标题section1下是否有user
   print(config.has_option('section1',''))
   #添加一个标题
   config.add_section('egon')
   #在标题egon下添加name=egon,age=18的配置
    config.set('egon','name','egon')
   config.set('egon','age',18) #报错,必须是字符串
    #最后将修改的内容写入文件,完成最终的修改
    config.write(open('a.cfg','w'))
hashlib模块
  # 1、什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法),该算法接受传入的内容,经过运算得到一串hash值
  # 2、hash值的特点是:
   1 只要传入的 内容一样,得到的 hash值必然一样=====>要用明文传输密码文件完整性校验
   2 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码
   3 只要使用的hash算法不变,无论校验的内容有多大,得到的 hash值长度是固定
  import hashlib
   helloworldegon
工厂 #m=hashlib.md5()
材料1 #m.update('hello'.encode('utf-8'))
   2 #m.update('world'.encode('utf-8'))
   3 #m.update('egon'.encode('utf-8'))   #update接受bytes,可以辅助分批导入
结果 #print(m.hexdigest()) # 3801fab9b8c8d9fcb481017969843ed5
   helloworldegon
  #m=hashlib.md5()
  #m.update('helloworldegon'.encode('utf-8'))
  #print(m.hexdigest()) # 3801fab9b8c8d9fcb481017969843ed5
  其他hash算法
  #m=hashlib. sha512()
  #m.update('helloworld'.encode('utf-8'))
  #print(m.hexdigest()) #1594244d52f2d8c12b142bb61f47bc2eaf503d6d9ca8480cae9fcf112f66e4967dc5e8fa98285e36db8af1b8ffa8b84cb15e0fbcf836c3deb803c13f37659a60
   下载文件校验hash值,和官方的文件hash值相等
  #m=hashlib.md5()
  #with open(r'D:\code\SH_fullstack_s1\day18\上节课复习','rb') as f:
    # for line in f:
      #  m.update(line)
  # hv=m.hexdigest()
  #print(hv) #f2a3a94efd0809e8a9c5ac8794c4bb2d      
     #953cd74a08f4fbb7e69a4bda8dfad056 (update内容增多 hash值长度不变)
   密码加盐
  #import hashlib
  #pwd='alex3714'
  #m=hashlib.md5()
  #m.update('一行白鹭上青天')
  #m.update(pwd.encode('utf-8'))
  #m.update('天'.encode('utf-8'))
  #m.update('小雨一米五'.encode('utf-8'))
  #print(m.hexdigest())
hmac模块
   hmac必须加盐
  import hmac
   m=hmac.new('天王盖地虎,小鸡炖模块'.encode('utf-8'))
  m.update('alex3814'.encode('utf-8'))
 print(m.hexdigest())
 

猜你喜欢

转载自www.cnblogs.com/3sss-ss-s/p/9469968.html