Beispiel #1
0
 def save(self, filename):
    """ Implements the module-level save() method by writing the 
        debug log information to the given file. """
    
    # protect access to the logLines with a mutex (for multiple threads)
    self._mutex.WaitOne(-1)
    try:
       if self._loglines == None:
          raise Exception("you must install the __Logger before using it")
       loglines_copy = list(self._loglines)
    finally:
       self._mutex.ReleaseMutex()
       
    try:
       writer = StreamWriter(filename, False, UTF8Encoding())
       for line in loglines_copy:
          writer.Write(line)
    finally:
       if writer: writer.Dispose()
Beispiel #2
0
                def onSave():
                    try:
                        fs = None
                        sr = None
                        sw = None

                        try:
                            fs = FileStream(__file__, FileMode.Open,
                                            FileAccess.ReadWrite,
                                            FileShare.Read)
                            encoding = UTF8Encoding(False)
                            sr = StreamReader(fs, encoding, True)
                            lines = Regex.Replace(
                                Regex.Replace(
                                    sr.ReadToEnd(), "username\\s*=\\s*\"\"",
                                    String.Format("username = \"{0}\"",
                                                  username),
                                    RegexOptions.CultureInvariant),
                                "password\\s*=\\s*\"\"",
                                String.Format("password = \"{0}\"", password),
                                RegexOptions.CultureInvariant)
                            fs.SetLength(0)
                            sw = StreamWriter(fs, encoding)
                            sw.Write(lines)

                        finally:
                            if sw is not None:
                                sw.Close()

                            if sr is not None:
                                sr.Close()

                            if fs is not None:
                                fs.Close()

                    except Exception, e:
                        Trace.WriteLine(e.clsException.Message)
                        Trace.WriteLine(e.clsException.StackTrace)
Beispiel #3
0
    def _dotnet_decrypt(data, key):
        """Performs decrypting of provided encrypted data.
        If 'digest' is True data must be hex digest, otherwise data should be
        encrypted bytes.

        This function is symmetrical with encrypt function.
        """
        data = Array[Byte](map(Byte, map(ord, data)))
        key = Array[Byte](map(Byte, key))
        rm = RijndaelManaged()
        dec_transform = rm.CreateDecryptor(key, Array[Byte](_vector))

        mem = MemoryStream()
        cs = CryptoStream(mem, dec_transform, CryptoStreamMode.Write)
        cs.Write(data, 0, data.Length)
        cs.FlushFinalBlock()

        mem.Position = 0
        decrypted = Array.CreateInstance(Byte, mem.Length)
        mem.Read(decrypted, 0, decrypted.Length)

        cs.Close()
        utfEncoder = UTF8Encoding()
        return utfEncoder.GetString(decrypted)
Beispiel #4
0
    def _dotnet_encrypt(text, key):
        """Performs crypting of provided text using AES algorithm.

        If 'digest' is True hex_digest will be returned, otherwise bytes of
        encrypted data will be returned.

        This function is symmetrical with decrypt function.
        """
        utfEncoder = UTF8Encoding()
        bytes_text = utfEncoder.GetBytes(text)
        rm = RijndaelManaged()
        key = Array[Byte](key)
        enc_transform = rm.CreateEncryptor(key, Array[Byte](_vector))
        mem = MemoryStream()

        cs = CryptoStream(mem, enc_transform, CryptoStreamMode.Write)
        cs.Write(bytes_text, 0, len(bytes_text))
        cs.FlushFinalBlock()
        mem.Position = 0
        encrypted = Array.CreateInstance(Byte, mem.Length)
        mem.Read(encrypted, 0, mem.Length)
        cs.Close()

        return ''.join(map(chr, encrypted))
Beispiel #5
0
def Parse(packet):
    encoder = UTF8Encoding()
    return encoder.GetString(packet) + "."
Beispiel #6
0
        print("Parsed file with {} warnings.".format(errorHandler.warnings))

    errorHandler = ErrorHandler()
    tree.Validate(errorHandler)

    if (errorHandler.warnings == 0):
        print("Validated parsed data with no warnings.")
    else:
        print("Validated parsed data with {} warnings.".format(
            errorHandler.warnings))

    input("Press enter to serialise data.")

    tree.elements.Insert(0, comment)
    ms = MemoryStream()
    stream = StreamWriter(ms, UTF8Encoding(True))
    parser.Serialise(tree, stream)
    ms.Position = 0
    sr = StreamReader(ms)
    myStr = sr.ReadToEnd()
    print(myStr)

    # output to the file
    #f = codecs.open("testFileNew.a2l", 'w+', 'utf-8')
    #f.write(myStr)
    #f.close()

    input("Press enter to close...")
else:
    print("Parsing failed!")