hashilib acquaintance

hashlib module

Some of the classes used to encrypt the package

hashilib encryption algorithm three steps

  1. Gets an encrypted objects
  2. Use encryption to encrypt object update method can be called multiple times
  3. Typically obtained results hexdigest encryption method, or digest ()

The purpose of encryption: for determining and verifying, rather than decryption

Features:

  • Data to a large, cut into different blocks, each of the different blocks is encrypted, and then the results are summarized, and the encrypted data directly to the overall consistent results.
  • One-way encryption, irreversible theory
  • Little change in the original data, will lead to very large differences in the results, "avalanche effect"

When you create an encrypted object, you can specify the parameters referred salt.

To a data encryption:

Verify: Results with another encrypted data and the first encryption of the result and the comparison result of the encryption

If the result of the same description of the same description, if different results indicate the different source

# -*- coding: utf-8 -*-
'''
hashilib 加密算法
加密三大步骤
1. 获取一个加密对象
2. 使用加密对象的update方法进行加密,可以调用多次
3. 通常通过hexdigest 方法 获取加密结果,或digest()
'''

import hashlib
import pickle
#获取一个加密对象
m = hashlib.md5()
#使用加密对象的update ,进行加密
s1 = '我爱你中国123'
m.update(s1.encode('utf-8'))
#通过hexdigest 获取加密结果
res = m.hexdigest()
# res = m.digest()
print(res)

Hashilib achieved by simple login authentication:

import hashlib
def getMd5(username,password):
    m = hashlib.md5()
    m.update(username.encode('utf-8'))
    m.update(password.encode('utf-8'))
    return m.hexdigest()


def register(username,password):
    res = getMd5(username,password)
    with open('User_info.txt','at',encoding='utf-8')as f:
        f.write(res+'\n')

def login(username,password):
    res = getMd5(username,password)
    with open('User_info.txt','rt',encoding='utf-8') as f:
        for line in f:
            if res == line.strip():
                return True
        else:
            return False
while True:
    print(''.center(50,'-'))
    try:
        op = int(input('1.注册\t2.登1录\t3.退出\t\n请输入:'))
    except ValueError:
        print('输入有误')
        continue
    if op == 1:
        username = input('请输入用户名:')
        password = input('请输入密码:')
        register(username,password)
    elif op == 2:
        username = input('请输入用户名:')
        password = input('请输入密码:')
        res = login(username,password)
        if res:
            print('登录成功')
        else:
            print('登录失败')
    elif op == 3:
        break
    else:
        print('输入有误')
        continue

Guess you like

Origin www.cnblogs.com/pandaa/p/12070133.html
Recommended