答复: 一种简单的给MD5加盐算法

原文 http://www.iteye.com/topic/1127277
python实现
#!/usr/bin/env python
#coding:utf-8

import random
import hashlib

DIGEST_LEN = 16

def generate(password):
    salt = '%0*d%0*d' % (8, random.randint(0, 99999999), 8, random.randint(0, 99999999))
    password = hashlib.md5(str(password)+salt).hexdigest()
    cs = [] # size 48
    for i in range(DIGEST_LEN):
        cs.append(password[i*2])
        cs.append(salt[i])
        cs.append(password[i*2+1])

    return "".join(cs)

def verify(password, md5):
    salt = []
    md5_password = []
    for i in range(DIGEST_LEN):
        salt.append(md5[i * 3 + 1])
        md5_password.append(md5[i * 3])
        md5_password.append(md5[i * 3 + 2])

    return hashlib.md5(str(password) + "".join(salt)).hexdigest() == "".join(md5_password)

if __name__ == '__main__':
    password = generate("123456")
    print verify("123456", password) # print "True"
(文:程序员之家)

转载注明地址:http://www.verydemo.com

 

原作者:http://www.verydemo.com/demo_c122_i22264.html