<s> __doc__ = '''<STR_LIT>''' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> DES_blocksize = <NUM_LIT:8> <EOL> CAST5_blocksize = <NUM_LIT:8> <EOL> Blowfish_blocksize = <NUM_LIT:8> <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> AES_blocksize = <NUM_LIT:16> <EOL> def paddingLength ( str_len , blocksize = AES_blocksize ) : <EOL> '''<STR_LIT>''' <EOL> assert <NUM_LIT:0> < blocksize < <NUM_LIT:255> , '<STR_LIT>' <EOL> assert str_len > <NUM_LIT:0> , '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> pad_len = blocksize - ( str_len % blocksize ) <EOL> return pad_len <EOL> def appendCMSPadding ( str , blocksize = AES_blocksize ) : <EOL> '''<STR_LIT>''' <EOL> pad_len = paddingLength ( len ( str ) , blocksize ) <EOL> padding = ( chr ( pad_len ) * pad_len ) <EOL> return str + padding <EOL> def removeCMSPadding ( str , blocksize = AES_blocksize ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> pad_len = ord ( str [ - <NUM_LIT:1> ] ) <EOL> except TypeError : <EOL> pad_len = str [ - <NUM_LIT:1> ] <EOL> assert pad_len <= blocksize , '<STR_LIT>' <EOL> assert pad_len <= len ( str ) , '<STR_LIT>' <EOL> return str [ : - pad_len ] <EOL> def appendBitPadding ( str , blocksize = AES_blocksize ) : <EOL> '''<STR_LIT>''' <EOL> pad_len = paddingLength ( len ( str ) , blocksize ) - <NUM_LIT:1> <EOL> padding = chr ( <NUM_LIT> ) + '<STR_LIT>' * pad_len <EOL> return str + padding <EOL> def removeBitPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = <NUM_LIT:0> <EOL> for char in str [ : : - <NUM_LIT:1> ] : <EOL> if char == '<STR_LIT>' : <EOL> pad_len += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> pad_len += <NUM_LIT:1> <EOL> str = str [ : - pad_len ] <EOL> return str <EOL> def appendZeroLenPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = paddingLength ( len ( str ) , blocksize ) - <NUM_LIT:1> <EOL> padding = '<STR_LIT>' * pad_len + chr ( pad_len ) <EOL> return str + padding <EOL> def removeZeroLenPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> try : <EOL> pad_len = ord ( str [ - <NUM_LIT:1> ] ) <EOL> except TypeError : <EOL> pad_len = str [ - <NUM_LIT:1> ] <EOL> assert pad_len < blocksize , '<STR_LIT>' <EOL> assert pad_len < len ( str ) , '<STR_LIT>' <EOL> return str [ : - pad_len ] <EOL> def appendNullPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = paddingLength ( len ( str ) , blocksize ) <EOL> padding = '<STR_LIT>' * pad_len <EOL> return str + padding <EOL> def removeNullPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = <NUM_LIT:0> <EOL> for char in str [ : : - <NUM_LIT:1> ] : <EOL> if char == '<STR_LIT>' : <EOL> pad_len += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> str = str [ : - pad_len ] <EOL> return str <EOL> def appendSpacePadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = paddingLength ( len ( str ) , blocksize ) <EOL> padding = '<STR_LIT>' * pad_len <EOL> return str + padding <EOL> def removeSpacePadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = <NUM_LIT:0> <EOL> for char in str [ : : - <NUM_LIT:1> ] : <EOL> if char == '<STR_LIT:U+0020>' : <EOL> pad_len += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> str = str [ : - pad_len ] <EOL> return str <EOL> def appendRandomLenPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = paddingLength ( len ( str ) , blocksize ) - <NUM_LIT:1> <EOL> from os import urandom <EOL> padding = urandom ( pad_len ) + chr ( pad_len ) <EOL> return str + padding <EOL> def removeRandomLenPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> pad_len = ord ( str [ - <NUM_LIT:1> ] ) <EOL> assert pad_len < blocksize , '<STR_LIT>' <EOL> assert pad_len < len ( str ) , '<STR_LIT>' <EOL> return str [ : - pad_len ] <EOL> def TestPadding ( str , blocksize = AES_blocksize ) : <EOL> '<STR_LIT>' <EOL> from hashlib import sha1 <EOL> str_hash = sha1 ( str ) . hexdigest ( ) <EOL> str = appendPadding ( str ) <EOL> str = removePadding ( str ) <EOL> if sha1 ( str ) . hexdigest ( ) == str_hash : <EOL> return True <EOL> else : <EOL> return False <EOL> '<STR_LIT>' <EOL> MODES = { <EOL> ( <NUM_LIT:0> , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( <NUM_LIT:1> , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( <NUM_LIT:3> , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( <NUM_LIT:4> , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' ) : '<STR_LIT>' <EOL> } <EOL> def appendPadding ( str , blocksize = AES_blocksize , mode = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if mode not in ( <NUM_LIT:0> , '<STR_LIT>' ) : <EOL> for k in MODES . keys ( ) : <EOL> if mode in k : <EOL> return globals ( ) [ '<STR_LIT>' + k [ <NUM_LIT:1> ] + '<STR_LIT>' ] ( str , blocksize ) <EOL> else : <EOL> return appendCMSPadding ( str , blocksize ) <EOL> else : <EOL> return appendCMSPadding ( str , blocksize ) <EOL> def removePadding ( str , blocksize = AES_blocksize , mode = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if mode not in ( <NUM_LIT:0> , '<STR_LIT>' ) : <EOL> for k in MODES . keys ( ) : <EOL> if mode in k : <EOL> return globals ( ) [ '<STR_LIT>' + k [ <NUM_LIT:1> ] + '<STR_LIT>' ] ( str , blocksize ) <EOL> else : <EOL> return removeCMSPadding ( str , blocksize ) <EOL> else : <EOL> return removeCMSPadding ( str , blocksize ) <EOL> '''<STR_LIT>''' </s>
