コード例 #1
0
 def __init__(
     self, key, iv: Arg('-i', '--iv', help=(
         'Specifies the initialization vector. If none is specified, then a block of zero bytes is used.')) = B'',
     padding: Arg.Choice('-p', type=str.lower, choices=['pkcs7', 'iso7816', 'x923', 'raw'],
         nargs=1, metavar='P', help=(
         'Choose a padding algorithm ({choices}). The raw algorithm does nothing. By default, all other algorithms '
         'are attempted. In most cases, the data was not correctly decrypted if none of these work.')
     ) = None,
     raw: Arg.Switch('-r', '--raw', help='Set the padding to raw; ignored when a padding is specified.') = False,
     **keywords
 ):
     if not padding:
         padding = ['raw'] if raw else ['pkcs7', 'iso7816', 'x923']
     elif not isinstance(padding, list):
         padding = [padding]
     iv = iv or bytes(self.blocksize)
     super().__init__(key=key, iv=iv, padding=padding, **keywords)
コード例 #2
0
ファイル: vigenere.py プロジェクト: binref/refinery
 def __init__(
     self,
     key: Arg(type=str, help='The encryption key'),
     alphabet: Arg(
         help='The alphabet, by default the Latin one is used: "{default}"'
     ) = 'abcdefghijklmnopqrstuvwxyz',
     operator: Arg.Choice(
         '-:',
         choices=['add', 'sub', 'xor'],
         metavar='OP',
         help=
         ('Choose the vigenere block operation. The default is {default}, and the available options are: {choices}'
          )) = 'add',
     case_sensitive: Arg.Switch(
         '-c',
         help=
         ('Unless this option is set, the key will be case insensitive. Uppercase letters from the input are transformed '
          'using the same shift as would be the lowercase variant, but case is retained.'
          )) = False,
     ignore_unknown: Arg.Switch(
         '-i',
         help=
         ('Unless this option is set, the key stream will be iterated even '
          'for letters that are not contained in the alphabet.')) = False):
     if not callable(operator):
         operator = {
             'add': __add__,
             'sub': __sub__,
             'xor': __xor__,
         }.get(operator.lower(), None)
         if operator is None:
             raise ValueError(
                 F'The value {operator!r} is not valid as an operator.')
     if not case_sensitive:
         key = key.lower()
         alphabet = alphabet.lower()
         if len(set(alphabet)) != len(alphabet):
             raise ValueError('Duplicate entries detected in alphabet.')
     if not set(key) <= set(alphabet):
         raise ValueError(
             'key contains letters which are not from the given alphabet')
     self.superinit(super(), **vars())