PyFoam.Infrastructure.Authentication module

Simple public key authentication with RSA based on the implementation http://code.activestate.com/recipes/578797-public-key-encryption-rsa/

class PyFoam.Infrastructure.Authentication.Key(exponent, modulus)

Bases: tuple

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

__module__ = 'PyFoam.Infrastructure.Authentication'
static __new__(_cls, exponent, modulus)

Create new instance of Key(exponent, modulus)

__repr__()

Return a nicely formatted representation string

__slots__ = ()
_asdict()

Return a new OrderedDict which maps field names to their values.

_fields = ('exponent', 'modulus')
classmethod _make(iterable, new=<built-in method __new__ of type object at 0x9d43a0>, len=<built-in function len>)

Make a new Key object from a sequence or iterable

_replace(**kwds)

Return a new Key object replacing specified fields with new values

_source = "from builtins import property as _property, tuple as _tuple\nfrom operator import itemgetter as _itemgetter\nfrom collections import OrderedDict\n\nclass Key(tuple):\n 'Key(exponent, modulus)'\n\n __slots__ = ()\n\n _fields = ('exponent', 'modulus')\n\n def __new__(_cls, exponent, modulus):\n 'Create new instance of Key(exponent, modulus)'\n return _tuple.__new__(_cls, (exponent, modulus))\n\n @classmethod\n def _make(cls, iterable, new=tuple.__new__, len=len):\n 'Make a new Key object from a sequence or iterable'\n result = new(cls, iterable)\n if len(result) != 2:\n raise TypeError('Expected 2 arguments, got %d' % len(result))\n return result\n\n def _replace(_self, **kwds):\n 'Return a new Key object replacing specified fields with new values'\n result = _self._make(map(kwds.pop, ('exponent', 'modulus'), _self))\n if kwds:\n raise ValueError('Got unexpected field names: %r' % list(kwds))\n return result\n\n def __repr__(self):\n 'Return a nicely formatted representation string'\n return self.__class__.__name__ + '(exponent=%r, modulus=%r)' % self\n\n def _asdict(self):\n 'Return a new OrderedDict which maps field names to their values.'\n return OrderedDict(zip(self._fields, self))\n\n def __getnewargs__(self):\n 'Return self as a plain tuple. Used by copy and pickle.'\n return tuple(self)\n\n exponent = _property(_itemgetter(0), doc='Alias for field number 0')\n\n modulus = _property(_itemgetter(1), doc='Alias for field number 1')\n\n"
property exponent

Alias for field number 0

property modulus

Alias for field number 1

class PyFoam.Infrastructure.Authentication.KeyPair(public, private)

Bases: tuple

__getnewargs__()

Return self as a plain tuple. Used by copy and pickle.

__module__ = 'PyFoam.Infrastructure.Authentication'
static __new__(_cls, public, private)

Create new instance of KeyPair(public, private)

__repr__()

Return a nicely formatted representation string

__slots__ = ()
_asdict()

Return a new OrderedDict which maps field names to their values.

_fields = ('public', 'private')
classmethod _make(iterable, new=<built-in method __new__ of type object at 0x9d43a0>, len=<built-in function len>)

Make a new KeyPair object from a sequence or iterable

_replace(**kwds)

Return a new KeyPair object replacing specified fields with new values

_source = "from builtins import property as _property, tuple as _tuple\nfrom operator import itemgetter as _itemgetter\nfrom collections import OrderedDict\n\nclass KeyPair(tuple):\n 'KeyPair(public, private)'\n\n __slots__ = ()\n\n _fields = ('public', 'private')\n\n def __new__(_cls, public, private):\n 'Create new instance of KeyPair(public, private)'\n return _tuple.__new__(_cls, (public, private))\n\n @classmethod\n def _make(cls, iterable, new=tuple.__new__, len=len):\n 'Make a new KeyPair object from a sequence or iterable'\n result = new(cls, iterable)\n if len(result) != 2:\n raise TypeError('Expected 2 arguments, got %d' % len(result))\n return result\n\n def _replace(_self, **kwds):\n 'Return a new KeyPair object replacing specified fields with new values'\n result = _self._make(map(kwds.pop, ('public', 'private'), _self))\n if kwds:\n raise ValueError('Got unexpected field names: %r' % list(kwds))\n return result\n\n def __repr__(self):\n 'Return a nicely formatted representation string'\n return self.__class__.__name__ + '(public=%r, private=%r)' % self\n\n def _asdict(self):\n 'Return a new OrderedDict which maps field names to their values.'\n return OrderedDict(zip(self._fields, self))\n\n def __getnewargs__(self):\n 'Return self as a plain tuple. Used by copy and pickle.'\n return tuple(self)\n\n public = _property(_itemgetter(0), doc='Alias for field number 0')\n\n private = _property(_itemgetter(1), doc='Alias for field number 1')\n\n"
property private

Alias for field number 1

property public

Alias for field number 0

PyFoam.Infrastructure.Authentication.authenticatedKeys()[source]
PyFoam.Infrastructure.Authentication.checkAuthentication(userName, challenge)[source]
PyFoam.Infrastructure.Authentication.checkChallenge(challenge, pubKey)[source]
PyFoam.Infrastructure.Authentication.createChallengeString(msg)[source]
PyFoam.Infrastructure.Authentication.decode(bcipher, privkey, verbose=False)[source]
PyFoam.Infrastructure.Authentication.encode(msg, pubkey, verbose=False)[source]
PyFoam.Infrastructure.Authentication.ensureKeyPair()[source]
PyFoam.Infrastructure.Authentication.is_prime(n, k=30)[source]
PyFoam.Infrastructure.Authentication.key_to_str(key)[source]

Convert Key to string representation >>> key_to_str(Key(50476910741469568741791652650587163073, 95419691922573224706255222482923256353)) ‘25f97fd801214cdc163796f8a43289c1:47c92a08bc374e96c7af66eb141d7a21’

PyFoam.Infrastructure.Authentication.keygen(n, public=None)[source]

Generate public and private keys from primes up to N.

Optionally, specify the public key exponent (65537 is popular choice).

>>> pubkey, privkey = keygen(2**64)
>>> msg = 123456789012345
>>> coded = pow(msg, *pubkey)
>>> plain = pow(coded, *privkey)
>>> assert msg == plain
PyFoam.Infrastructure.Authentication.multinv(modulus, value)[source]

Multiplicative inverse in a given modulus

>>> multinv(191, 138)
18
>>> multinv(191, 38)
186
>>> multinv(120, 23)
47
PyFoam.Infrastructure.Authentication.myAuthenticatedKeysFile()[source]
PyFoam.Infrastructure.Authentication.myPrivateKey()[source]
PyFoam.Infrastructure.Authentication.myPrivateKeyFile()[source]
PyFoam.Infrastructure.Authentication.myPublicKey()[source]
PyFoam.Infrastructure.Authentication.myPublicKeyFile()[source]
PyFoam.Infrastructure.Authentication.myPublicKeyText()[source]
PyFoam.Infrastructure.Authentication.randprime(n=100000000)[source]
PyFoam.Infrastructure.Authentication.str_to_key(key_str)[source]

Convert string representation to Key (assuming valid input) >>> (str_to_key(‘25f97fd801214cdc163796f8a43289c1:47c92a08bc374e96c7af66eb141d7a21’) == … Key(exponent=50476910741469568741791652650587163073, modulus=95419691922573224706255222482923256353)) True