一个简易的密码生成器,python 写的,遵循 2 句 BSD 许可证,应该很好懂,参数什么的可以自己调调。
PS:其实我写完了才发现有个叫 pwgen 的东西,pwgen -cns 16 似乎也不错。
参考:
- https://blog.delphij.net/2010/12/post-598.html
- phpmyadmin 生成密码的代码:https://github.com/Reen/phpmyadmin/blob/master/js/server_privileges.js#L80
#!/usr/bin/env python2.6 # Copyright 2011 Kou Zuyang. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, are # permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this list of # conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, this list # of conditions and the following disclaimer in the documentation and/or other materials # provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY Kou Zuyang ``AS IS'' AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # The views and conclusions contained in the software and documentation are those of the # authors and should not be interpreted as representing official policies, either expressed # or implied, of Kou Zuyang. import random; def generate_password(): alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPOPQRSTUVWXYZ' number = '1234567890' special = '!@#$%^&*()-=_+[]{};:,./\~`?' charset = alphabet * 3 + number * 7 + special * 2 print ''.join([random.choice(charset) for x in xrange(16)]) if __name__ == "__main__": generate_password()

