Package Peach :: Package Transformers :: Module fcrypt
[hide private]

Module fcrypt

source code


Unix crypt(3) password hash algorithm.

This is a port to Python of the standard Unix password crypt function.
It's a single self-contained source file that works with any version
of Python from version 1.5 or higher.  The code is based on Eric
Young's optimised crypt in C.

Python fcrypt is intended for users whose Python installation has not
had the crypt module enabled, or whose C library doesn't include the
crypt function.  See the documentation for the Python crypt module for
more information:

  http://www.python.org/doc/current/lib/module-crypt.html

An alternative Python crypt module that uses the MD5 algorithm and is
more secure than fcrypt is available from michal j wallace at:

  http://www.sabren.net/code/python/crypt/index.php3

The crypt() function is a one-way hash function, intended to hide a
password such that the only way to find out the original password is
to guess values until you get a match.  If you need to encrypt and
decrypt data, this is not the module for you.

There are at least two packages providing Python cryptography support:
M2Crypto at <http://www.pobox.org.sg/home/ngps/m2/>, and amkCrypto at
<http://www.amk.ca/python/code/crypto.html>.

Functions:

  crypt() -- return hashed password


Version: 1.3.1

Date: 21 February 2004

Author: Carey Evans <careye@spamcop.net>

Functions [hide private]
 
_HPERM_OP(a)
Clever bit manipulation.
source code
 
_PERM_OP(a, b, n, m)
Cleverer bit manipulation.
source code
 
_set_key(password)
Generate DES key schedule from ASCII password.
source code
 
_body(ks, E0, E1)
Use the key schedule ks and salt E0, E1 to create the password hash.
source code
 
crypt(password, salt)
Generate an encrypted hash from the passed password.
source code
 
_test()
Run doctest on fcrypt module.
source code
Variables [hide private]
  __credits__ = '''michal j wallace for inspiring me to write th...
  _ITERATIONS = 16
  _SPtrans = [0x00820200, 0x00020000, 0x80800000, 0x80820200, 0x...
  _skb = [0x00000000, 0x00000010, 0x20000000, 0x20000010, 0x0001...
  _shifts2 = 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0
  _con_salt = [0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0...
  _cov_2char = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij...
Function Details [hide private]

crypt(password, salt)

source code 

Generate an encrypted hash from the passed password. If the password is longer than eight characters, only the first eight will be used.

The first two characters of the salt are used to modify the encryption algorithm used to generate in the hash in one of 4096 different ways. The characters for the salt should be upper- and lower-case letters A to Z, digits 0 to 9, '.' and '/'.

The returned hash begins with the two characters of the salt, and should be passed as the salt to verify the password.

Example:

>>> from fcrypt import crypt
>>> password = 'AlOtBsOl'
>>> salt = 'cE'
>>> hash = crypt(password, salt)
>>> hash
'cEpWz5IUCShqM'
>>> crypt(password, hash) == hash
1
>>> crypt('IaLaIoK', hash) == hash
0

In practice, you would read the password using something like the getpass module, and generate the salt randomly:

>>> import random, string
>>> saltchars = string.letters + string.digits + './'
>>> salt = random.choice(saltchars) + random.choice(saltchars)

Note that other ASCII characters are accepted in the salt, but the results may not be the same as other versions of crypt. In particular, '_', '$1' and '$2' do not select alternative hash algorithms such as the extended passwords, MD5 crypt and Blowfish crypt supported by the OpenBSD C library.


Variables Details [hide private]

__credits__

Value:
'''michal j wallace for inspiring me to write this.
Eric Young for the C code this module was copied from.'''

_SPtrans

Value:
[0x00820200, 0x00020000, 0x80800000, 0x80820200, 0x00800000, 0x8002020\
0, 0x80020000, 0x80800000, 0x80020200, 0x00820200, 0x00820000, 0x80000\
200, 0x80800200, 0x00800000, 0x00000000, 0x80020000, 0x00020000, 0x800\
00000, 0x00800200, 0x00020200, 0x80820200, 0x00820000, 0x80000200, 0x0\
0800200, 0x80000000, 0x00000200, 0x00020200, 0x80820000, 0x00000200, 0\
x80800200, 0x80820000, 0x00000000, 0x00000000, 0x80820200, 0x00800200,\
 0x80020000, 0x00820200, 0x00020000, 0x80000200, 0x00800200, 0x8082000\
0, 0x00000200, 0x00020200, 0x80800000, 0x80020200, 0x80000000, 0x80800\
...

_skb

Value:
[0x00000000, 0x00000010, 0x20000000, 0x20000010, 0x00010000, 0x0001001\
0, 0x20010000, 0x20010010, 0x00000800, 0x00000810, 0x20000800, 0x20000\
810, 0x00010800, 0x00010810, 0x20010800, 0x20010810, 0x00000020, 0x000\
00030, 0x20000020, 0x20000030, 0x00010020, 0x00010030, 0x20010020, 0x2\
0010030, 0x00000820, 0x00000830, 0x20000820, 0x20000830, 0x00010820, 0\
x00010830, 0x20010820, 0x20010830, 0x00080000, 0x00080010, 0x20080000,\
 0x20080010, 0x00090000, 0x00090010, 0x20090000, 0x20090010, 0x0008080\
0, 0x00080810, 0x20080800, 0x20080810, 0x00090800, 0x00090810, 0x20090\
...

_con_salt

Value:
[0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xD\
D, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0\
xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4,\
 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x0\
0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0\
x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,\
 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1\
C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x20, 0x21, 0\
...

_cov_2char

Value:
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'