原文 http://www.iteye.com/topic/1127277
python实现
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"