You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 rivejä
2.9 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. """
  24. XTEA Block Encryption Algorithm
  25. Author: Paul Chakravarti (paul_dot_chakravarti_at_gmail_dot_com)
  26. License: Public Domain
  27. """
  28. def get_key():
  29. # Encryption key is datetime of creation of DocType, DocType"
  30. import webnotes
  31. return webnotes.conn.sql("select creation from tabDocType where name='DocType'")[0][0].strftime('%Y%m%d%H%M%s')[:16]
  32. def encrypt(data, encryption_key = None):
  33. if not encryption_key:
  34. encryption_key = get_key()
  35. return crypt(encryption_key, data).encode('hex')
  36. def decrypt(data, encryption_key = None):
  37. if not encryption_key:
  38. encryption_key = get_key()
  39. return crypt(encryption_key, data.decode('hex'))
  40. def crypt(key,data,iv='\00\00\00\00\00\00\00\00',n=32):
  41. def keygen(key,iv,n):
  42. while True:
  43. iv = xtea_encrypt(key,iv,n)
  44. for k in iv:
  45. yield ord(k)
  46. xor = [ chr(x^y) for (x,y) in zip(map(ord,data),keygen(key,iv,n)) ]
  47. return "".join(xor)
  48. def xtea_encrypt(key,block,n=32,endian="!"):
  49. import struct
  50. v0,v1 = struct.unpack(endian+"2L",block)
  51. k = struct.unpack(endian+"4L",key)
  52. sum,delta,mask = 0L,0x9e3779b9L,0xffffffffL
  53. for round in range(n):
  54. v0 = (v0 + (((v1<<4 ^ v1>>5) + v1) ^ (sum + k[sum & 3]))) & mask
  55. sum = (sum + delta) & mask
  56. v1 = (v1 + (((v0<<4 ^ v0>>5) + v0) ^ (sum + k[sum>>11 & 3]))) & mask
  57. return struct.pack(endian+"2L",v0,v1)
  58. def xtea_decrypt(key,block,n=32,endian="!"):
  59. import struct
  60. v0,v1 = struct.unpack(endian+"2L",block)
  61. k = struct.unpack(endian+"4L",key)
  62. delta,mask = 0x9e3779b9L,0xffffffffL
  63. sum = (delta * n) & mask
  64. for round in range(n):
  65. v1 = (v1 - (((v0<<4 ^ v0>>5) + v0) ^ (sum + k[sum>>11 & 3]))) & mask
  66. sum = (sum - delta) & mask
  67. v0 = (v0 - (((v1<<4 ^ v1>>5) + v1) ^ (sum + k[sum & 3]))) & mask
  68. return struct.pack(endian+"2L",v0,v1)