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

第 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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值