Guest User

Untitled

a guest
May 25th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. BT_ZERO, BT_NINE, BT_A, BT_F, BT_Z = ord(
  2. '0'), ord('9'), ord('A'), ord('F'), ord('Z')
  3. BT_LIL_A, BT_LIL_Z = ord('a'), ord('z')
  4. BT_CHR62, BT_CHR63 = ord('+'), ord('/')
  5. BT_PAD = ord('=')
  6. B64_BYTES = list(range(BT_A, BT_Z + 1)) +\
  7. list(range(BT_LIL_A, BT_LIL_Z + 1)) +\
  8. list(range(BT_ZERO, BT_NINE + 1)) + \
  9. [BT_CHR62, BT_CHR63]
  10.  
  11. # print(''.join([chr(x) for x in B64_BYTES])) # make string
  12.  
  13.  
  14. def hex_digit_conv(x):
  15. if x > BT_A:
  16. return (x - BT_A) + 10
  17. else:
  18. return x - BT_ZERO
  19.  
  20.  
  21. def nibble_pairs(ingen):
  22. pair = []
  23. watchdog = None
  24. for count, val in enumerate(ingen):
  25. watchdog = count
  26. pair.append(val)
  27. if (count & 1) == 1: # odd check (conceptualy it's even if we weren't counting up from zero)
  28. yield pair
  29. pair = []
  30. if (watchdog & 1) == 0:
  31. raise IndexError("uneven number of digits")
  32.  
  33.  
  34. def hex_bytes_2_bytes_gen(hb):
  35. hb = (x for x in hb.upper() if x > BT_ZERO - 1 and x <
  36. BT_NINE + 1 or x > BT_A - 1 and x < BT_F + 1)
  37.  
  38. btgen = (hex_digit_conv(pair[0]) * 16 +
  39. hex_digit_conv(pair[1]) for pair in nibble_pairs(hb))
  40. # print(''.join([chr(x) for x in btgen])) # make string
  41. return btgen
  42.  
  43.  
  44. def slice_n_gen(ingen, n):
  45. """ How to reimpliment itertools.islice for no reason"""
  46. group = []
  47. count = 0
  48. countmod = 0
  49. for val in ingen:
  50. count += 1
  51. countmod = count % n
  52. if countmod == 0:
  53. group += [val]
  54. yield group
  55. group = []
  56. else:
  57. group += [val]
  58.  
  59. if countmod != 0:
  60. yield group
  61.  
  62.  
  63. def hex2b64(hexbytes):
  64. return bytes2b64(hex_bytes_2_bytes_gen(hexbytes))
  65.  
  66. def bytes2b64(inbytes):
  67. bytegrouping = 3
  68. for bits in slice_n_gen(inbytes, bytegrouping):
  69. chunk = []
  70. extra = bytegrouping - len(bits)
  71. total = (bits[0] << 16)
  72. if extra < 2:
  73. total |= (bits[1] << 8)
  74. if extra < 1:
  75. total |= (bits[2])
  76. nchrs = [(total >> 18) & 63, (total >> 12) &
  77. 63, (total >> 6) & 63, total & 63]
  78. if extra == 0:
  79. chunk += [nchrs[0], nchrs[1], nchrs[2], nchrs[3]]
  80. elif extra == 1:
  81. chunk += [nchrs[0], nchrs[1], nchrs[2], None]
  82. elif extra == 2:
  83. chunk += [nchrs[0], nchrs[1], None, None]
  84.  
  85. for c in map(lambda x: BT_PAD if x is None else B64_BYTES[x], chunk):
  86. yield c
  87.  
  88.  
  89. def main():
  90. import sys
  91. arg = sys.argv[1]
  92. r = ''.join(chr(x) for x in hex2b64(bytes(arg, 'latin-1')))
  93. print(r)
  94.  
  95.  
  96. def test1():
  97. import pprint
  98. import codecs
  99.  
  100. for i in range(0, 9):
  101. pprint.pprint([x for x in slice_n_gen(range(0, i), 3)])
  102. expected = """
  103. []
  104. [[0]]
  105. [[0, 1]]
  106. [[0, 1, 2]]
  107. [[0, 1, 2], [3]]
  108. [[0, 1, 2], [3, 4]]
  109. [[0, 1, 2], [3, 4, 5]]
  110. [[0, 1, 2], [3, 4, 5], [6]]
  111. [[0, 1, 2], [3, 4, 5], [6, 7]]
  112.  
  113. """
  114.  
  115. def sanity_check(h_bt_str):
  116. print("______________")
  117. print("Sanity Check:")
  118. r = codecs.encode(codecs.decode(h_bt_str, 'hex'),
  119. 'base64').decode("latin-1")
  120. print(r)
  121. print("Result:")
  122. r = ''.join(map(chr, hex2b64(bytes(h_bt_str))))
  123. print(r)
  124. print("---------------")
  125.  
  126. hexvals = [
  127. b"49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6573206d757368726f6f6d21",
  128. b"49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"]
  129. testvals = ["hello", "hello!", "hello!!", "hello!!!", "hello!!!?", ]
  130. testvals = [codecs.encode(x.encode('latin-1'), 'hex') for x in testvals]
  131. testvals += hexvals
  132.  
  133. for x in testvals:
  134. # if len(x) % 3 == 2:
  135. # import ipdb; ipdb.set_trace()
  136. sanity_check(x)
  137.  
  138.  
  139. if __name__ == '__main__':
  140. main()
  141. # test1()
Add Comment
Please, Sign In to add comment