Esempio n. 1
0
    def run(self, edit, encode_type='b64encode'):

        fix_base32_padding = sublime.load_settings(SETTINGS_FILE).get("base32_fix_padding", False)
        print("Codec: fix base32 padding? %s" % str(fix_base32_padding))
        fix_base64_padding = sublime.load_settings(SETTINGS_FILE).get("base64_fix_padding", False)
        print("Codec: fix base64 padding? %s" % str(fix_base64_padding))

        for region in selected_regions(self.view):
            if not region.empty():
                original_string = self.view.substr(region)
                # print("string: " + original_string)
                if 'b64encode' == encode_type:
                    encoded_string = base64.b64encode(original_string.encode("UTF-8"))
                elif 'b64decode' == encode_type:
                    encoded_string = codec_base64.b64decode(original_string.encode("UTF-8"), add_padding=fix_base64_padding)                    
                elif 'urlsafe_b64encode' == encode_type:
                    encoded_string = base64.urlsafe_b64encode(original_string.encode("UTF-8"))
                elif 'urlsafe_b64decode' == encode_type:
                    encoded_string = codec_base64.urlsafe_b64decode(original_string.encode("UTF-8"), add_padding=fix_base64_padding)
                elif 'b32encode' == encode_type:
                    encoded_string = base64.b32encode(original_string.encode("UTF-8"))
                elif 'b32decode' == encode_type:
                    encoded_string = codec_base64.b32decode(original_string.encode("UTF-8"), add_padding=fix_base32_padding)
                elif 'b16encode' == encode_type:
                    encoded_string = base64.b16encode(original_string.encode("UTF-8"))
                elif 'b16decode' == encode_type:
                    encoded_string = base64.b16decode(original_string.encode("UTF-8"))
                else:
                    print("unsupported operation %s" % (encode_type,))
                    break

                # print("string encoded: " + str(encoded_string.decode("UTF-8")))
                self.view.replace(edit, region, encoded_string.decode("UTF-8"))
Esempio n. 2
0
    def run(self, edit, encode_type='b64encode'):

        fix_base32_padding = sublime.load_settings(SETTINGS_FILE).get(
            "base32_fix_padding", False)
        print("Codec: fix base32 padding? %s" % str(fix_base32_padding))
        fix_base64_padding = sublime.load_settings(SETTINGS_FILE).get(
            "base64_fix_padding", False)
        print("Codec: fix base64 padding? %s" % str(fix_base64_padding))

        for region in selected_regions(self.view):
            if not region.empty():
                original_string = self.view.substr(region)
                # print("string: " + original_string)
                if 'b64encode' == encode_type:
                    encoded_string = base64.b64encode(
                        original_string.encode("UTF-8"))
                elif 'b64decode' == encode_type:
                    encoded_string = codec_base64.b64decode(
                        original_string.encode("UTF-8"),
                        add_padding=fix_base64_padding)
                elif 'urlsafe_b64encode' == encode_type:
                    encoded_string = base64.urlsafe_b64encode(
                        original_string.encode("UTF-8"))
                elif 'urlsafe_b64decode' == encode_type:
                    encoded_string = codec_base64.urlsafe_b64decode(
                        original_string.encode("UTF-8"),
                        add_padding=fix_base64_padding)
                elif 'b32encode' == encode_type:
                    encoded_string = base64.b32encode(
                        original_string.encode("UTF-8"))
                elif 'b32decode' == encode_type:
                    encoded_string = codec_base64.b32decode(
                        original_string.encode("UTF-8"),
                        add_padding=fix_base32_padding)
                elif 'b16encode' == encode_type:
                    encoded_string = base64.b16encode(
                        original_string.encode("UTF-8"))
                elif 'b16decode' == encode_type:
                    encoded_string = base64.b16decode(
                        original_string.encode("UTF-8"))
                else:
                    print("unsupported operation %s" % (encode_type, ))
                    break

                # print("string encoded: " + str(encoded_string.decode("UTF-8")))
                self.view.replace(edit, region, encoded_string.decode("UTF-8"))
Esempio n. 3
0
    def test_b32decode_padding_is_false(self):
        # Don't fix the padding
        pad_padded_s = 'IFBEGRCFIY'

        with self.assertRaises(TypeError):
            codec_base64.b32decode(pad_padded_s, add_padding=False)
Esempio n. 4
0
    def test_b32decode_six_pads(self):
        expected = 'ABCDEF'
        pad_padded_s = 'IFBEGRCFIY'

        self.assertEqual(expected, codec_base64.b32decode(pad_padded_s, add_padding=True))
Esempio n. 5
0
    def test_b32decode_padding_is_false(self):
        # Don't fix the padding
        pad_padded_s = 'IFBEGRCFIY'.encode('UTF-8')

        with self.assertRaises(Exception):
            codec_base64.b32decode(pad_padded_s, add_padding=False)
Esempio n. 6
0
    def test_b32decode_six_pads(self):
        expected = 'ABCDEF'.encode('UTF-8')
        pad_padded_s = 'IFBEGRCFIY'.encode('UTF-8')

        self.assertEqual(
            expected, codec_base64.b32decode(pad_padded_s, add_padding=True))