Loading

hashlib和hmac模块

hashlib模块

  • 这个模块针对不同的安全哈希和消息摘要算法实现了一个通用的接口。该算法接收传入的内容,经过运算得到一串hash值
  • 此模块中总是可用的哈希算法构造器有md5, sha1(), sha224(), sha256(), sha384(), sha512(), blake2b()blake2s()

hash值的特点

  • 1、只要传入的内容一样,得到的hash值必然一样=========>>文件传输完整性校验。
  • 2、不能由hash值反解成内容==========>> 把密码做成hash值,不应该在网络传输明文密码。
  • 3、只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的。

hashlib的使用

  • 注:向 update() 输入字符串对象是不被支持的,因为哈希基于字节而非字符。
  • update()可多次传值,与一次update这段长数据,得到的结果一样,但是update多次为校验大文件提供了可能。
import hashlib
m = hashlib.md5()
m.update(b'abc')
m.update('李白'.encode('utf-8'))
print(m.hexdigest())
# ddfe8b4099dee5a307bd8d72b5408332

# 简要写法
print(hashlib.md5('abc李白'.encode()).hexdigest())
# ddfe8b4099dee5a307bd8d72b5408332

撞库

撞库是通过已有的大量密码进行hash运算得到hash值,将截获到的密码秘文与已有的hash值进行比对。

import hashlib

# 已有密码
password_list = [
    "RuGxM62EgW",
    "rx3Nmu59oV",
    "eIgdXq6B4N",
    "75dY890jAA",
    "5yy4GJvTu0",
    "1BB775rZDU",
    "MTlmcz2kmL",
    "vN7MZ71FbL",
]


class Hit_The_Library:
    # 将已有密码文件生成密码字典,key是明文密码,value是秘文
    def get_password_dict(self, password_list):
        self.password_dict = {}
        m = hashlib.md5()
        for i in password_list:
            m.update(i.encode('utf-8'))
            self.password_dict[i] = m.hexdigest()
        return self.password_dict

    # 破解
    def brute_force(self, private_password):
        for k, v in self.password_dict.items():
            if v == private_password:
                return f'明文密码为:{k}\n密文密码为:{v}'

hit = Hit_The_Library()
hit.get_password_dict(password_list)
print(hit.brute_force("a191f8c8408c5a90fd970a02d0a3b52a"))
明文密码为:MTlmcz2kmL
密文密码为:a191f8c8408c5a90fd970a02d0a3b52a

密码加盐

对于私密信息不能直接以明文传输,比如密码,应该将之加密后传输。为防止数据包被截获后通过撞库而破解,可在密码内掺入其他内容。

import hashlib
m = hashlib.sha512()
m.update('首部添加的内容'.encode('utf-8'))
m.update('password'.encode('utf-8'))
m.update('尾部添加的内容'.encode('utf-8'))
print(m.hexdigset())

文件校验

1、校验全部内容,如果文件过大耗时会比较长。

import hashlib

class FileVerification:
    def __init__(self,file):
        self.file = file

    def verification(self,hash):
        h = getattr(hashlib,hash)()
        with open(self.file, 'rb') as f:
            for line in f:
                h.update(line)
            return h.hexdigest()

file = FileVerification('password.txt')
print(file.verification('md5'))
3b673cf066e166e5c8f58e096cf02f85

2、截取文件的部分位置校验,速度较快,但有误差。例如平均在文件每1/100位置上取10个字节进行校验。

import os
import hashlib

class FileVerification:
    def __init__(self,file):
        self.file = file

    def verification(self,hash):
        h = getattr(hashlib,hash)()
        step = os.path.getsize(self.file) // 100
        with open(self.file, 'rb') as f:
            while 1:
                content = f.read(10)
                if len(content):
                    h.update(content)
                    f.seek(step,1)
                else:
                    return h.hexdigest()

file = FileVerification('password.txt')
print(file.verification('md5'))
c7ab5c06d3e9d70f1a40dd022609397b

hmac模块

new方法第一个参数指定加的盐,必须为bytes类型,第二个参数指定hash算法。hmac并不是简单的将盐插入数据首部或尾部,而是经过其他算法将盐混入数据。

import hmac
h1 = hmac.new(b'hash',digestmod='md5')
h1.update(b'hello')
h1.update(b'world')
print(h1.hexdigest())
905f549c5722b5850d602862c34a763e
posted @ 2021-01-12 21:22  吃了好多肉  阅读(292)  评论(1编辑  收藏  举报