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 (bytesToStr(bytearray(b"abcd123")) == "abcd123")
def createFromBytes(cls, fileBytes): # May raise SyntaxError try: fileStr = bytesToStr(fileBytes, "ascii") # UnicodeDecodeError return cls.createFromPem(fileStr) # SyntaxError except (UnicodeDecodeError, SyntaxError) as e: # File had non-ASCII chars in it, *OR* # File did not PEM-decode return cls(bytearray(fileBytes)) # SyntaxError
def _readFileTextAndBinary(self, fname): try: # Read both binary (bytearray) and text (str) versions of the input try: if fname == "-": # Read as binary binary = readStdinBinary() else: binary = bytearray(open(fname, "rb").read()) text = bytesToStr(binary, "ascii") except UnicodeDecodeError: # So it must be a binary file, not text text = None return text, binary except IOError: self.printError("Error opening file: %s" % fname)
def getCryptoVersion(): if o.enabled: cryptoVersion = "%s" % bytesToStr(o.SSLeay_version(0)) else: cryptoVersion = "python crypto - %s" % o.initErrorString return cryptoVersion