Skip to content

Instantly share code, notes, and snippets.

@013231
Created June 16, 2012 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 013231/2940665 to your computer and use it in GitHub Desktop.
Save 013231/2940665 to your computer and use it in GitHub Desktop.
Generate password
# Copyright 2012 new013231@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python
import argparse
import hashlib
import base64
import re
import getpass
import pyperclip
def genPw(pw, usage):
sha = hashlib.sha256(pw + usage).digest()
pw = base64.b64encode(sha)[:16]
if re.search('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[^+/]*$', pw):
return pw
else:
return genPw(pw, usage)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description = 'Use base password and usage to generate password'
)
parser.add_argument('-p', '--basePassword', dest='basePw'
, metavar = 'basePassword', help = 'Your base password.')
parser.add_argument('usage', nargs = '?', metavar = 'usage'
, help = 'Usage.')
parser.add_argument('-n', action = 'store_false', dest = 'c2cb'
, help = "Do Not copy password to clipboard.")
parser.add_argument('-v', '--version', action = 'version'
, version = '%(prog)s 0.42')
args = parser.parse_args()
if not args.basePw:
args.rPwd = getpass.getpass('Base possward: ')
if not args.usage:
args.usage = raw_input('Usage: ')
pwd = genPw(args.rPwd, args.usage)
print pwd
if args.c2cb:
pyperclip.copy(pwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment