用户姓名信息保护python_密码保护Python

import getpass

import pickle

import hashlib

from os import path

def Encryption(data):

return hashlib.sha224(data).hexdigest()

## First we check if the database exists.

if path.isfile('database.db'):

fh = open('database.db', 'rb')

db = pickle.load(fh)

fh.close()

## If it doesn't, we will create one.

else:

## First we create the desired variable.

db = {'torxed' : Encryption('wham'), 'someoneelse' : Encryption('pass')}

## Then we open a filehandle to it.

fh = open('database.db', 'wb')

## And then we dump the variable into the filehandle.

## This will keep the variable intact between sessions,

## meaning the next time you start your script, the variable will look the same.

pickle.dump(db, fh)

fh.close()

## Then we ask the user for his/hers credentials.

user = raw_input('Username: ')

_pass = getpass.getpass('Password: ')

## If the user exists in the "db" and the decoded password

## Matches the logged in user, it's a-ok :)

if user in db and db[user] == Encryption(_pass):

print 'You logged in'

添加更多用户

^{pr2}$

另一种方法是在添加用户时使用sys.argv从命令行获取用户名和密码,在这种情况下:import pickle, hashlib, sys

if len(sys.argv) < 3:

raise ValueError('Need two parameters, username and password')

def Encryption(data):

return hashlib.sha224(data).hexdigest()

with open('database.db', 'rb') as fh:

db = pickle.load(fh)

db[sys.argv[1]] = Encryption(sys.argv[2])

with open('database.db', 'wb') as fh:

pickle.dump(db, fh)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值