Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. def hexdump(src, length=16, sep='.'):
  2. """
  3. @brief Return {src} in hex dump.
  4. @param[in] length {Int} Nb Bytes by row.
  5. @param[in] sep {Char} For the text part, {sep} will be used for
  6. non ASCII char.
  7. @return {Str} The hexdump
  8. """
  9. result = []
  10.  
  11. # Python3 support
  12. try:
  13. xrange(0, 1)
  14. except NameError:
  15. xrange = range
  16.  
  17. for i in xrange(0, len(src), length):
  18. subSrc = src[i:i+length]
  19. hexa = ''
  20. for h in xrange(0,len(subSrc)):
  21. if h == length/2:
  22. hexa += ' '
  23. h = subSrc[h]
  24. if not isinstance(h, int):
  25. h = ord(h)
  26. h = hex(h).replace('0x', '')
  27. if len(h) == 1:
  28. h = '0' + h
  29. hexa += h + ' '
  30. hexa = hexa.strip(' ')
  31. text = ''
  32. for c in subSrc:
  33. if not isinstance(c, int):
  34. c = ord(c)
  35. if 0x20 <= c < 0x7F:
  36. text += chr(c)
  37. else:
  38. text += sep
  39. result.append(
  40. ('%08X: %-'+str(length*(2+1)+1)+'s |%s|') % (i, hexa, text))
  41.  
  42. return '\n'.join(result)
  43.  
  44.  
  45. ################################################################################
  46. # Basic usage:
  47.  
  48. from binascii import unhexlify, hexlify
  49. def h2bin( x ):
  50. return unhexlify(str.encode(x.replace(' ', '').replace('\n', '')))
  51.  
  52. hello_openvpn = h2bin('''
  53. 16 03 01 00 df 01 00 00 db 03 01 95 a3 8a 7f 46
  54. a9 1c 78 99 21 ae 92 6d 2d 14 5a 8f 2b c8 ee e2
  55. 0b 9e 38 34 ec 3d 66 2b 9c d5 63 00 00 68 c0 14
  56. c0 0a c0 22 c0 21 00 39 00 38 00 88 00 87 c0 0f
  57. c0 05 00 35 00 84 c0 12 c0 08 c0 1c c0 1b 00 16
  58. 00 13 c0 0d c0 03 00 0a c0 13 c0 09 c0 1f c0 1e
  59. 00 33 00 32 00 9a 00 99 00 45 00 44 c0 0e c0 04
  60. 00 2f 00 96 00 41 00 07 c0 11 c0 07 c0 0c c0 02
  61. 00 05 00 04 00 15 00 12 00 09 00 14 00 11 00 08
  62. 00 06 00 03 00 ff 02 01 00 00 49 00 0b 00 04 03
  63. 00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19 00
  64. 0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08 00
  65. 06 00 07 00 14 00 15 00 04 00 05 00 12 00 13 00
  66. 01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00 00
  67. 0f 00 01 01
  68. ''')
  69.  
  70. print(hexdump(hello_openvpn))
  71.  
  72.  
  73. # Will print:
  74. '''
  75. 00000000: 16 03 01 00 df 01 00 00 db 03 01 95 a3 8a 7f 46 |...............F|
  76. 00000010: a9 1c 78 99 21 ae 92 6d 2d 14 5a 8f 2b c8 ee e2 |..x.!..m-.Z.+...|
  77. 00000020: 0b 9e 38 34 ec 3d 66 2b 9c d5 63 00 00 68 c0 14 |..84.=f+..c..h..|
  78. 00000030: c0 0a c0 22 c0 21 00 39 00 38 00 88 00 87 c0 0f |...".!.9.8......|
  79. 00000040: c0 05 00 35 00 84 c0 12 c0 08 c0 1c c0 1b 00 16 |...5............|
  80. 00000050: 00 13 c0 0d c0 03 00 0a c0 13 c0 09 c0 1f c0 1e |................|
  81. 00000060: 00 33 00 32 00 9a 00 99 00 45 00 44 c0 0e c0 04 |.3.2.....E.D....|
  82. 00000070: 00 2f 00 96 00 41 00 07 c0 11 c0 07 c0 0c c0 02 |./...A..........|
  83. 00000080: 00 05 00 04 00 15 00 12 00 09 00 14 00 11 00 08 |................|
  84. 00000090: 00 06 00 03 00 ff 02 01 00 00 49 00 0b 00 04 03 |..........I.....|
  85. 000000A0: 00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19 00 |......4.2.......|
  86. 000000B0: 0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08 00 |................|
  87. 000000C0: 06 00 07 00 14 00 15 00 04 00 05 00 12 00 13 00 |................|
  88. 000000D0: 01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00 00 |............#...|
  89. 000000E0: 0f 00 01 01 |....|
  90. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement