Ejemplo n.º 1
0
    def test_Compat(self):
        assert(isinstance(a2b_hex("00"), bytearray))
        assert(a2b_hex("0102cdef") == bytearray([0x01,0x02,0xcd,0xef]))
        assert(a2b_hex("0102CDEF") == bytearray([0x01,0x02,0xcd,0xef]))
        try:
            assert(a2b_hex("c0102cdef") == bytearray([0x01,0x02,0xcd,0xef]))
            assert False
        except SyntaxError:
            pass
        try:
            assert(a2b_hex("xx0102cdef") == bytearray([0x01,0x02,0xcd,0xef]))
            assert False
        except SyntaxError:
            pass

        assert(isinstance(a2b_base64("0000"), bytearray))
        assert(a2b_base64("Zm9vYg==") == bytearray(b"foob"))

        assert(b2a_hex(bytearray([0x01,0x02,0xcd,0xef])) == "0102cdef")
        assert(b2a_hex(bytearray([0x00])) == "00")
        assert(b2a_hex(bytearray([0xFF])) == "ff")

        assert(b2a_base64(bytearray(b"foob")) == "Zm9vYg==\n")
        assert(b2a_base64(bytearray(b"fooba")) == "Zm9vYmE=\n")
        assert(b2a_base64(bytearray(b"foobar")) == "Zm9vYmFy\n")

        assert(bytesToStrAscii(bytearray(b"abcd123")) == "abcd123")
Ejemplo n.º 2
0
    def test_Compat(self):
        assert (isinstance(a2b_hex("00"), bytearray))
        assert (a2b_hex("0102cdef") == bytearray([0x01, 0x02, 0xcd, 0xef]))
        assert (a2b_hex("0102CDEF") == bytearray([0x01, 0x02, 0xcd, 0xef]))
        try:
            assert (a2b_hex("c0102cdef") == bytearray([0x01, 0x02, 0xcd,
                                                       0xef]))
            assert False
        except SyntaxError:
            pass
        try:
            assert (a2b_hex("xx0102cdef") == bytearray(
                [0x01, 0x02, 0xcd, 0xef]))
            assert False
        except SyntaxError:
            pass

        assert (isinstance(a2b_base64("0000"), bytearray))
        assert (a2b_base64("Zm9vYg==") == bytearray(b"foob"))

        assert (b2a_hex(bytearray([0x01, 0x02, 0xcd, 0xef])) == "0102cdef")
        assert (b2a_hex(bytearray([0x00])) == "00")
        assert (b2a_hex(bytearray([0xFF])) == "ff")

        assert (b2a_base64(bytearray(b"foob")) == "Zm9vYg==\n")
        assert (b2a_base64(bytearray(b"fooba")) == "Zm9vYmE=\n")
        assert (b2a_base64(bytearray(b"foobar")) == "Zm9vYmFy\n")

        assert (bytesToStrAscii(bytearray(b"abcd123")) == "abcd123")
Ejemplo n.º 3
0
    def encode(self, name):
        """Encode a payload bytearray into a PEM string.

        The input will be base64 encoded, then wrapped in a PEM prefix/postfix
        based on the name string, e.g. for name="CERTIFICATE":

        -----BEGIN CERTIFICATE-----
        MIIBXDCCAUSgAwIBAgIBADANBgkqhkiG9w0BAQUFADAPMQ0wCwYDVQQDEwRUQUNL
    ...
        KoZIhvcNAQEFBQADAwA5kw==
        -----END CERTIFICATE-----
        """
        s1 = b2a_base64(self.data)[:-1] # remove terminating \n
        s2 = ""
        while s1:
            s2 += s1[:64] + "\n"
            s1 = s1[64:]

        s = ("-----BEGIN %s-----\n" % name) + s2 +\
            ("-----END %s-----\n" % name)

        return s