/src/commonutils/crypt.py

https://github.com/omat/kimlerdensin · Python · 111 lines · 91 code · 14 blank · 6 comment · 16 complexity · 9896c3718b4d3d5b7b9e120523d1cf74 MD5 · raw file

  1. from django.conf import settings
  2. import django.contrib.contenttypes.models
  3. from django.contrib import contenttypes
  4. from Crypto.Cipher import Blowfish
  5. from base64 import *
  6. import random
  7. import sha
  8. # Caches of content types/models
  9. _model_cache = {}
  10. _model_name_cache = {}
  11. _content_type_cache = {}
  12. _content_type_rec_cache = {}
  13. def verify_objref_hash( content_type_id, object_id, hash ):
  14. hash_match = sha.new("%s/%s" % (content_type_id, object_id) + settings.SECRET_KEY).hexdigest()
  15. if hash == hash_match:
  16. return True
  17. else:
  18. return False
  19. def cryptString( plain ):
  20. obj=Blowfish.new( settings.SECRET_KEY, Blowfish.MODE_ECB)
  21. randstring = open("/dev/urandom").read(12)
  22. split = random.randrange(10)+1
  23. s = randstring[:split] + ':valid:' + plain +':valid:'+ randstring[split:]
  24. length = len(s)
  25. l = length + 8 - ( length % 8 )
  26. padded = s + " " * ( 8 - length % 8)
  27. ciph=obj.encrypt(padded[:l])
  28. try:
  29. return b32encode(ciph)
  30. except NameError:
  31. return encodestring(ciph)
  32. def decryptString( cipher ):
  33. obj=Blowfish.new( settings.SECRET_KEY, Blowfish.MODE_ECB)
  34. try:
  35. ciph = b32decode( cipher )
  36. except NameError:
  37. ciph = decodestring(cipher )
  38. plaintext = obj.decrypt(ciph)
  39. try:
  40. (c1, email,c2) = plaintext.split(":valid:")
  41. except ValueError:
  42. return None
  43. return email
  44. def get_object(content_type_id, pk_val):
  45. """Gets an object from the content type id and a primary key"""
  46. return get_model(content_type_id)._default_manager.get(pk=pk_val)
  47. def get_model(content_type_id):
  48. """Gets a model class for a given content_type_id"""
  49. try:
  50. return _model_cache[content_type_id]
  51. except KeyError:
  52. try:
  53. ct = contenttypes.models.ContentType.objects.get(pk=content_type_id)
  54. except contenttypes.models.ContentType.DoesNotExist:
  55. return None
  56. model = ct.model_class()
  57. if model is None:
  58. return None
  59. _model_cache[content_type_id] = model
  60. db = model._meta.db_table
  61. _content_type_cache[db] = content_type_id
  62. _content_type_rec_cache[db] = ct
  63. return model
  64. def get_content_type_id(model):
  65. """Gets a content type id for a given model"""
  66. try:
  67. db = model._meta.db_table
  68. return _content_type_cache[db]
  69. except:
  70. ct = contenttypes.models.ContentType.objects.get_for_model(model)
  71. model = ct.model_class()
  72. _model_cache[ct.id] = model
  73. db = model._meta.db_table
  74. _content_type_cache[db] = ct.id
  75. _content_type_rec_cache[db] = ct
  76. return ct.id
  77. def get_content_type(model):
  78. """Gets a content type record for a given model"""
  79. try:
  80. db = model._meta.db_table
  81. return _content_type_rec_cache[db]
  82. except:
  83. ct = contenttypes.models.ContentType.objects.get_for_model(model)
  84. model = ct.model_class()
  85. _model_cache[ct.id] = model
  86. db = model._meta.db_table
  87. _content_type_cache[db] = ct.id
  88. _content_type_rec_cache[db] = ct
  89. return ct
  90. def get_content_type_id_by_modelname(modelname):
  91. """Gets a content type record for a given modelname"""
  92. try:
  93. return _model_name_cache[modelname]
  94. except:
  95. ct = contenttypes.models.ContentType.objects.get(model=modelname)
  96. _model_name_cache[modelname] = ct.id
  97. return ct.id