encdec.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. $Id$
  6. This file is part of the xsser project, http://xsser.03c8.net
  7. Copyright (c) 2011/2016 psy <epsylon@riseup.net>
  8. xsser is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation version 3 of the License.
  11. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. details.
  15. You should have received a copy of the GNU General Public License along
  16. with xsser; if not, write to the Free Software Foundation, Inc., 51
  17. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. """
  19. import urllib
  20. class EncoderDecoder(object):
  21. """
  22. Class to help encoding and decoding strings with different hashing or
  23. encoding algorigthms..
  24. """
  25. # encdec functions:
  26. def __init__(self):
  27. self.encmap = { "Str" : lambda x : self._fromCharCodeEncode(x),
  28. "Hex" : lambda x : self._hexEncode(x),
  29. "Hes" : lambda x : self._hexSemiEncode(x),
  30. "Une" : lambda x : self._unEscape(x),
  31. "Dec" : lambda x : self._decEncode(x),
  32. "Mix" : lambda x : self._unEscape(self._fromCharCodeEncode(x))
  33. }
  34. def _fromCharCodeEncode(self, string):
  35. """
  36. Encode to string.
  37. """
  38. encoded=''
  39. for char in string:
  40. encoded=encoded+","+str(ord(char))
  41. return encoded[1:]
  42. def _hexEncode(self, string):
  43. """
  44. Encode to hex.
  45. """
  46. encoded=''
  47. for char in string:
  48. encoded=encoded+"%"+hex(ord(char))[2:]
  49. return encoded
  50. def _hexSemiEncode(self, string):
  51. """
  52. Encode to semi hex.
  53. """
  54. encoded=''
  55. for char in string:
  56. encoded=encoded+"&#x"+hex(ord(char))[2:]+";"
  57. return encoded
  58. def _decEncode(self, string):
  59. """
  60. Encode to decimal.
  61. """
  62. encoded=''
  63. for char in string:
  64. encoded=encoded+"&#"+str(ord(char))
  65. return encoded
  66. def _unEscape(self, string):
  67. """
  68. Escape string.
  69. """
  70. encoded=''
  71. for char in string:
  72. encoded=encoded+urllib.quote(char)
  73. return encoded
  74. def _ipDwordEncode(self, string):
  75. """
  76. Encode to dword.
  77. """
  78. encoded=''
  79. tblIP = string.split('.')
  80. # In the case it's not an IP
  81. if len(tblIP)!=4:
  82. return 0
  83. for number in tblIP:
  84. tmp=hex(int(number))[2:]
  85. if len(tmp)==1:
  86. tmp='0' +tmp
  87. encoded=encoded+tmp
  88. return int(encoded,16)
  89. def _ipOctalEncode(self, string):
  90. """
  91. Encode to octal.
  92. """
  93. encoded=''
  94. tblIP = string.split('.')
  95. # In the case it's not an IP
  96. if len(tblIP)!=4:
  97. return 0
  98. octIP = map(lambda s: oct(int(s)).zfill(4), tblIP)
  99. return ".".join(octIP)
  100. if __name__ == "__main__":
  101. encdec = EncoderDecoder()
  102. print encdec._ipOctalEncode("127.0.0.1")