PageRenderTime 101ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/sphene/contrib/libs/common/utils/misc.py

https://github.com/tsaylor/BARcamp-Chicago-Website
Python | 43 lines | 34 code | 5 blank | 4 comment | 0 complexity | 255be507a847a2ec54bb79e0259073f1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Most stuff here is borrowed from the zyons project (http://zyons.python-hosting.com/)
  3. """
  4. from Crypto.Cipher import Blowfish
  5. from base64 import *
  6. import random
  7. def cryptString( secret, plain ):
  8. obj = Blowfish.new( secret, Blowfish.MODE_ECB )
  9. #randstring = unicode(open("/dev/urandom").read(12), 'ascii', 'ignore')
  10. randstring = str.join( '', random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890',12) )
  11. split = random.randrange(10)+1
  12. s = randstring[:split] + ':valid:' + plain + ':valid:' + randstring[split:]
  13. length = len(s)
  14. l = length + 8 - ( length % 8 )
  15. padded = s + " " * ( 8 - length % 8 )
  16. ciph = obj.encrypt(padded[:l])
  17. try:
  18. return b32encode(ciph)
  19. except NameError:
  20. return encodestring
  21. def decryptString( secret, cipher ):
  22. obj = Blowfish.new( secret, Blowfish.MODE_ECB )
  23. try:
  24. ciph = b32decode( cipher )
  25. except NameError:
  26. ciph = decodestring( cipher )
  27. plaintext = obj.decrypt( ciph )
  28. try:
  29. (c1,email,c2) = plaintext.split(":valid:")
  30. except ValueError:
  31. return None
  32. return email