Advertisement
smeech

Password generator

Aug 21st, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.17 KB | None | 0 0
  1. # A simple password generator written while reflecting on an Espanso Hub submission
  2. # Called by `:genpass()` for a twelve-character password, or
  3. # `:genpass(n)` for an n-length password.
  4. # The script will add the result to the clipboard for pasting elsewhere,
  5. # if `pyperclip` is installed
  6.  
  7.   - regex: :genpass\((?P<myvar>\d*)\)
  8.     replace: "{{output}}"
  9.     vars:
  10.       - name: output
  11.         type: script
  12.         params:
  13.           args:
  14.             - python
  15.             - -c
  16.             - |
  17.              import random, string
  18.               characters = string.ascii_letters + string.punctuation + string.digits
  19.               n = max(4, int('{{myvar}}') if '{{myvar}}'.isdigit() else 12)
  20.               while True:
  21.                 password = ''.join(random.choice(characters) for _ in range(n))
  22.                 if (any(c.islower() for c in password) and any(c.isupper() for c in password) and any(c in string.punctuation for c in password) and any(c.isdigit() for c in password)):
  23.                   break
  24.               try:
  25.                 import pyperclip
  26.                 pyperclip.copy(password)
  27.               except:
  28.                 pass
  29.               print(password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement