其他分享
首页 > 其他分享> > 自制小功能——cmd中的规则加密输入

自制小功能——cmd中的规则加密输入

作者:互联网

用到的库

基础了解

实现代码

这是几年前曾今自己为兴趣而写的文件,所以里面的注释都是英文的(我英语并不算怎么好,所以有的可能是字面意思~)。

import msvcrt
import argparse


class Password:
    """
    Only use in CMD , the password can only made of num and alpha and special symbols in english.
    ↑↓←→  ==》  [b'H', b'P', b'K', b'M']
    symbols in [b'+', b'-', b'*', b'/', b'_', b'@', b'#', b'%', b'&']
    :type rule: password rules in ['num', 'alpha', 'symbol', 'na', 'ns', 'as', 'all']
    :return: password
    """
    def __init__(self, rule='all'):
        assert rule in ['all', 'num', 'alpha', 'symbol', 'na', 'ns', 'as'], ValueError("rule in ['num', 'alpha', 'symbol', 'na', 'ns', 'as', 'all']")
        self.password = None
        self.rule = rule
        self.symbols = [b'+', b'-', b'*', b'/', b'_', b'@', b'#', b'%', b'&']
        print('Password: ', end='', flush=True)
        self.letters = []
        while 1:
            self.ch = msvcrt.getch()
            # Enter
            if self.ch == b'\r':
                msvcrt.putch(b'\n')
                print(self.password)
                break
            # Backspace
            elif self.ch == b'\x08':
                if self.letters:
                    self.letters.pop()
                    # \b let the cursor move to the left one, msvcrt.putch must one to one .
                    msvcrt.putch(b'\b')
                    msvcrt.putch(b' ')
                    msvcrt.putch(b'\b')
                    self.password = b''.join(self.letters).decode()
            # Esc
            elif self.ch == b'\x1b':
                break
            elif self.check_rule():
                self.letters.append(self.ch)
                msvcrt.putch(b'*')
                self.password = b''.join(self.letters).decode()

    def check_rule(self):
        """
        :return: the password if match rule
        """
        if '\\x' not in str(self.ch):
            if self.rule == 'num':
                return self.ch.isdigit()
            elif self.rule == 'alpha':
                return self.ch.isalpha()
            elif self.rule == 'symbol':
                return self.ch in self.symbols
            elif self.rule == 'na':
                return self.ch.isalnum()
            elif self.rule == 'ns':
                return self.ch.isdigit() or self.ch in self.symbols
            elif self.rule == 'as':
                return self.ch.isalpha() or self.ch in self.symbols
            elif self.rule == 'all':
                return self.ch.isalnum() or self.ch in self.symbols


if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog=f"python {__file__}", description='Only used in CMD.')
    parser.add_argument('-r', '--rule', type=str, default='all', help="Roles in ['all', 'num', 'alpha', 'symbol', 'na', 'ns', 'as']")
    input(Password(parser.parse_args().rule) and '\n Enter to exit.')

结束语

这次准备的可能比较匆忙,比较简陋,下一次不出意外发的应该是正则整理和python中的re模块。

标签:elif,ch,加密,自制,cmd,rule,msvcrt,return,self
来源: https://blog.csdn.net/user_from_future/article/details/121736234