python 生成强密码,关注速度和交替打字。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 生成强密码,关注速度和交替打字。相关的知识,希望对你有一定的参考价值。

# Pleasant Password Generator: Generates strong passwords with a focus on speed
#     Copyright (C) 2016 Garrett Mills
#
#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU Affero General Public License for more details.
#
#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.

import random;

# make a dictionary for dynamic references
dictOfStuff = {};
truefalse = [True, False];

# Keys typed with the left hand
left = ['a', 'c', 'd', 'e', 'f', 'g', 'q', 'r', 's', 't', 'v', 'l', 'w', 'x', 'z'];
dictOfStuff["left"] = left;

# keys typed with the right hand
right = ['b', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'u', 'y'];
dictOfStuff["right"] = right;

vowels = ['a', 'e', 'i', 'o', 'u'];
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];

# list of characters allowed to be used to space the word
right_sep_chars = ['.', ','];
dictOfStuff["right_sep_chars"] = right_sep_chars;

left_sep_chars = ['@', '#', '$'];
dictOfStuff["left_sep_chars"] = left_sep_chars;

# list of numbers by hand
right_nums = ['1', '2', '3', '4', '5', '6'];
dictOfStuff['right_nums'] = right_nums;

left_nums = ['7', '8', '9', '0'];
dictOfStuff['left_nums'] = left_nums;

max_word_len = 6
min_word_len = 4
running_password = "";
last_hand = "";

# generate the random word length
word_len = random.randint(min_word_len, max_word_len);

# will return the hand used to type the given letter
def getHand( letter ):
    if letter in left:
        return "left";
    elif letter in right:
        return "right";
    else:
        return None;

# ===========================
# Generate the Word
# ===========================

# repeat for each character in the random word length
for n in range(word_len):
    # for the first letter, select any consonant
    if n == 0:
        c = random.choice(consonants);
        running_password += c;
        last_hand = getHand(c);

    # for the second letter, select a vowel from the opposite hand
    elif n == 1:
        v = random.choice(vowels);
        while v in dictOfStuff[last_hand]:
            v = random.choice(vowels);

        running_password += v;
        last_hand = getHand(v);

    # for the third letter, select a consonant from the other hand
    elif n == 2:
        c = random.choice(consonants);
        while c in dictOfStuff[last_hand]:
            c = random.choice(consonants);

        running_password += c;
        last_hand = getHand(c);

    # for all following characters, select a letter from the opposite hand
    else:
        l = "";
        if last_hand == "left":
            l = random.choice(right);
        else:
            l = random.choice(left);

        running_password += l;
        last_hand = getHand(l);

# ===========================
# Generate the Spacer Char
# ===========================
if last_hand == "left":
    running_password += random.choice(dictOfStuff['right_sep_chars']);
    last_hand = "right";
else:
    running_password += random.choice(dictOfStuff['left_sep_chars']);
    last_hand = "left";

# ===========================
# Generate Numeric String
# ===========================

# randomly decides whether or nor to make the 3rd and 4th digits repeat the
#    1st and 2nd digits, respectively.
repeat_third = random.choice(truefalse);
repeat_fourth = random.choice(truefalse);

# to hold the individual digits
first = ""; second = ""; third = ""; fourth = "";

# generate the first digit, typed on the opposite hand
if last_hand == "left":
    first = random.choice(dictOfStuff["right_nums"]);
    last_hand = "right";
else:
    first = random.choice(dictOfStuff["left_nums"]);
    last_hand = "left";

# generate the second digit, on the opposite hand
if last_hand == "left":
    second = random.choice(dictOfStuff["right_nums"]);
    last_hand = "right";
else:
    second = random.choice(dictOfStuff["left_nums"]);
    last_hand = "left";

# repeat the third digit if given, otherwise, generate one on the opposite hand
if repeat_third:
    third = first;
else:
    if last_hand == "left":
        third = random.choice(dictOfStuff["right_nums"]);
        last_hand = "right";
    else:
        third = random.choice(dictOfStuff["left_nums"]);
        last_hand = "left";

# repeat the fourth digit if given, otherwise, generate one on the opposite hand
if repeat_fourth:
    fourth = second;
else:
    if last_hand == "left":
        fourth = random.choice(dictOfStuff["right_nums"]);
        last_hand = "right";
    else:
        fourth = random.choice(dictOfStuff["left_nums"]);
        last_hand = "left";

# add the digits to the running password
running_password += first+second+third+fourth;

# ===========================
# Generate Last character
# ===========================
if last_hand == "left":
    running_password += random.choice(dictOfStuff['right_sep_chars']);
    last_hand = "right";
else:
    running_password += random.choice(dictOfStuff['left_sep_chars']);
    last_hand = "left";

# return the password to the user
print (running_password);

以上是关于python 生成强密码,关注速度和交替打字。的主要内容,如果未能解决你的问题,请参考以下文章

sh 生成强Diffie-Hellman群|| Dhparams 2048

如何通过输入/答案在 Python 中产生打字效果?

PHP中的动态密码生成[重复]

Python:游戏:测试打字速度

要做最快的男人,教你用Python写一个打字测试器测试你的打字速度~

这款 Chrome 扩展可以把你的打字节奏变成「密码」