您的位置:首页 > 移动开发 > 微信开发

每天一个py小程序 001生成激活码

2015-03-12 19:33 806 查看
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

#coding=utf-8
"""第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),
使用 Python 如何生成 200 个激活码(或者优惠券)?"""

import random
import hashlib
#set(['SHA1', 'SHA224', 'SHA', 'SHA384', 'ecdsa-with-SHA1', 'SHA256', 'SHA512', 'md4', 'md5', 'sha1', 'dsaWithSHA', 'DSA-SHA', 'sha224', 'dsaEncryption', 'DSA', 'ripemd160', 'sha', 'MD5', 'MD4', 'sha384', 'sha256', 'sha512', 'RIPEMD160', 'whirlpool'])
import os
import time

def salt():
return "%s"*5  % tuple([random.randint(10000000,99999999) for i in range(5)])

def md5(str):
m=hashlib.md5()
m.update(str)
return m.hexdigest()

def main():
#print hashlib.algorithms_available
res=[md5(salt() + str(time.time())) for i in range(200) ]
path=os.path.split(os.path.realpath(__file__))[0]+"\\"
#print path
newpath=path+"res.txt"

f=open(newpath,"w")
for i in res:
f.write(i+"\n")
f.close()
print "done"

if __name__=='__main__':
main()


程序主要分为两个部分,产生激活码,以及存入文件。

我们产生5组介于10000000~99999999之间的数字。并采用hashlib库中的md5加密 5组数字+当前的时间序列。

>>> help(hashlib.md5())
Help on HASH object:

class HASH(__builtin__.object)
|  A hash represents the object used to calculate a checksum of a
|  string of information.
|
|  Methods:
|
|  update() -- updates the current digest with an additional string
|  digest() -- return the current digest value
|  hexdigest() -- return the current digest as a string of hexadecimal digits
|  copy() -- return a copy of the current hash object
|
|  Attributes:
|
|  name -- the hash algorithm being used by this object
|  digest_size -- number of bytes in this hashes output
|
|  Methods defined here:
|
|  __repr__(...)
|      x.__repr__() <==> repr(x)
|
|  copy(...)
|      Return a copy of the hash object.
|
|  digest(...)
|      Return the digest value as a string of binary data.
|
|  hexdigest(...)
|      Return the digest value as a string of hexadecimal digits.
|
|  update(...)
|      Update this hash object's state with the provided string.
|
|  ----------------------------------------------------------------------
|  Data descriptors defined here:
|
|  block_size
|
|  digest_size
|
|  digestsize
|
|  name
|      algorithm name.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐