/usr/lib/python2.7/dist-packages/M2Crypto/util.py is in python-m2crypto 0.27.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | from __future__ import absolute_import
"""
M2Crypto utility routines.
Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved.
Portions created by Open Source Applications Foundation (OSAF) are
Copyright (C) 2004 OSAF. All Rights Reserved.
"""
import binascii
import logging
import sys
# This means "Python 2.7 or higher" so it is True for py3k as well
py27plus = sys.version_info[:2] > (2, 6) # type: bool
from M2Crypto import m2, six
if py27plus:
from typing import AnyStr, Tuple, Union # noqa
# see https://github.com/python/typeshed/issues/222
AddrType = Union[Tuple[str, int], str]
log = logging.getLogger('util')
class UtilError(Exception):
pass
m2.util_init(UtilError)
def pkcs5_pad(data, blklen=8):
# type: (str, int) -> str
pad = (8 - (len(data) % 8))
return data + chr(pad) * pad
def pkcs7_pad(data, blklen):
# type: (str, int) -> str
if blklen > 255:
raise ValueError('illegal block size')
pad = (blklen - (len(data) % blklen))
return data + chr(pad) * pad
# before the introduction of py3{bytes,str}, python2 code
# was just using args as-is
if six.PY2:
def py3bytes(x):
# type: (AnyStr) -> Optional[bytes,bytearray]
if isinstance(x, unicode):
return x.encode('utf8')
elif isinstance(x, (bytearray, str)):
return x
else:
raise TypeError('No string argument provided')
def py3str(x):
# type: (Optional[str,bytearray]) -> str
if isinstance(x, bytearray):
return str(x)
elif isinstance(x, (str, unicode)):
return x
else:
raise TypeError('No string argument provided')
else:
def py3bytes(x):
# type: (AnyStr) -> Optional[bytes,bytearray]
if isinstance(x, str):
return bytes(x, encoding='utf8')
elif isinstance(x, (bytes, bytearray)):
return x
else:
raise TypeError('No string argument provided')
def py3str(x):
# type: (Optional[AnyStr,bytearray]) -> str
if isinstance(x, (bytes, bytearray)):
return x.decode('utf8')
elif isinstance(x, str):
return x
else:
raise TypeError('No string argument provided')
def bin_to_hex(b):
# type: (bytes) -> str
return py3str(binascii.b2a_base64(b)[:-1])
def octx_to_num(x):
# type: (bytes) -> int
return int(binascii.hexlify(x), 16)
def genparam_callback(p, n, out=sys.stdout):
# type: (int, Any, file) -> None
ch = ['.', '+', '*', '\n']
out.write(ch[p])
out.flush()
def quiet_genparam_callback(p, n, out):
# type: (Any, Any, Any) -> None
pass
def passphrase_callback(v, prompt1='Enter passphrase:',
prompt2='Verify passphrase:'):
# type: (bool, str, str) -> Optional[str]
from getpass import getpass
while 1:
try:
p1 = getpass(prompt1)
if v:
p2 = getpass(prompt2)
if p1 == p2:
break
else:
break
except KeyboardInterrupt:
return None
return p1
def no_passphrase_callback(*args):
# type: (*Any) -> str
return ''
|