def crypt(word, salt=None, iterations=None):
 if (salt is None):
  salt = _makesalt()
 if isinstance(salt, unicode):
  salt = salt.encode('us-ascii')
 if (not isinstance(salt, str)):
  raise TypeError('salt must be a string')
 if isinstance(word, unicode):
  word = word.encode('UTF-8')
 if (not isinstance(word, str)):
  raise TypeError('word must be a string or unicode')
 if salt.startswith('$p5k2$'):
  (iterations, salt, dummy) = salt.split('$')[2:5]
  if (iterations == ''):
   iterations = 400
  else:
   converted = int(iterations, 16)
   if (iterations != ('%x' % converted)):
    raise ValueError('Invalid salt')
   iterations = converted
   if (not (iterations >= 1)):
    raise ValueError('Invalid salt')
 allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./'
 for ch in salt:
  if (ch not in allowed):
   raise ValueError(('Illegal character %r in salt' % (ch,)))
 if ((iterations is None) or (iterations == 400)):
  iterations = 400
  salt = ('$p5k2$$' + salt)
 else:
  salt = ('$p5k2$%x$%s' % (iterations, salt))
 rawhash = PBKDF2(word, salt, iterations).read(24)
 return ((salt + '$') + b64encode(rawhash, './'))

def crypt ( word , salt = None , iterations = None ) : if ( salt is None ) : salt = _makesalt ( ) if isinstance ( salt , unicode ) : salt = salt . encode ( 'us-ascii' ) if ( not isinstance ( salt , str ) ) : raise TypeError ( 'salt must be a string' ) if isinstance ( word , unicode ) : word = word . encode ( 'UTF-8' ) if ( not isinstance ( word , str ) ) : raise TypeError ( 'word must be a string or unicode' ) if salt . startswith ( '$p5k2$' ) : ( iterations , salt , dummy ) = salt . split ( '$' ) [ 2 : 5 ] if ( iterations == '' ) : iterations = 400 else : converted = int ( iterations , 16 ) if ( iterations != ( '%x' % converted ) ) : raise ValueError ( 'Invalid salt' ) iterations = converted if ( not ( iterations >= 1 ) ) : raise ValueError ( 'Invalid salt' ) allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./' for ch in salt : if ( ch not in allowed ) : raise ValueError ( ( 'Illegal character %r in salt' % ( ch , ) ) ) if ( ( iterations is None ) or ( iterations == 400 ) ) : iterations = 400 salt = ( '$p5k2$$' + salt ) else : salt = ( '$p5k2$%x$%s' % ( iterations , salt ) ) rawhash = PBKDF2 ( word , salt , iterations ) . read ( 24 ) return ( ( salt + '$' ) + b64encode ( rawhash , './' ) )
method to encrypt streams using a pdf security handler .