Skip to content

Instantly share code, notes, and snippets.

@lemenkov
Created November 25, 2019 13:51
Show Gist options
  • Save lemenkov/f0e3a9fa30c34973407a48050a04738d to your computer and use it in GitHub Desktop.
Save lemenkov/f0e3a9fa30c34973407a48050a04738d to your computer and use it in GitHub Desktop.
Test for topoh module
import unittest
word64digits = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+."
topo_hiding_prefix = b"DLGCH_"
def word64encode(Str):
Out = b""
i = len(Str)
j = 0
while i >= 3:
Out += bytes([word64digits[Str[j+0] >> 2]]);
Out += bytes([word64digits[((Str[j+0] << 4) & 0x30) | (Str[j+1] >> 4)]])
Out += bytes([word64digits[((Str[j+1] << 2) & 0x3c) | (Str[j+2] >> 6)]])
Out += bytes([word64digits[Str[j+2] & 0x3f]])
i -= 3
j += 3
if i > 0:
Out += bytes([word64digits[Str[j+0] >> 2]])
Fragment = (Str[j+0] << 4) & 0x30
if i > 1:
Fragment |= Str[j+1] >> 4;
Out += bytes([word64digits[Fragment]])
if i < 2:
Out += b'-'
else:
Out += bytes([word64digits[(Str[1] << 2) & 0x3c]])
Out += b'-'
return Out
def str_xor(Str0, Str1):
Out = b""
for i in range(len(Str0)):
Out += bytes([Str0[i] ^ Str1[i % len(Str1)]])
return Out
class TestEncryptMethod(unittest.TestCase):
def test_encrypt(self):
Msg = b"my message I want to encrypt"
Seed = b"OpenSIPS"
Wanted = b'DLGCH_IglFAzY6IzIoFUUncz4xPTtQEQFzLD4wPQkVGg--'
self.assertEqual(Wanted, topo_hiding_prefix + word64encode(str_xor(Msg, Seed)))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment