private void sendFile(IWebSocketConnection webSocket, string filename)
        {
            if (!File.Exists(filename))
            {
                Log(webSocket, filename + " not exists");
                return;
            }
            List <string> param = new List <string>();

            param.Add(Path.GetFileName(filename));
            param.Add(RSA_Unit.Base64EncodeBytes(rsa.Encode(File.ReadAllBytes(filename))));
            param.Add(HashTool.SHA256File(new FileStream(filename, FileMode.Open)));
            Operation op = new Operation()
            {
                op    = "send_file",
                param = param
            };

            Log(string.Format("{0} {1}", param[0], param[2]));
            send(webSocket, JsonConvert.SerializeObject(op));
        }
Example #2
0
        private void ParseResponse(byte[] result)
        {
            string message = Encoding.Default.GetString(result);

            Operation msg = JsonConvert.DeserializeObject <Operation>(message);

            switch (msg.op)
            {
            case "auth":
                rsa = new RSACrypto(File.ReadAllText("pri.key"), true);
                break;

            case "list_file":
                lvFileBrowser.Items.Clear();
                int id = 1;
                foreach (string i in msg.param)
                {
                    ListViewItem tmp = new ListViewItem(new string[] { id.ToString(), i, "Not Download" });
                    lvFileBrowser.Items.Add(tmp);
                    id++;
                }
                break;

            case "send_file":
                string path = "data/" + msg.param[0];
                File.WriteAllBytes(path, rsa.DecodeOrNull(RSA_Unit.Base64DecodeBytes(msg.param[1])));
                if (HashTool.SHA256File(new FileStream(path, FileMode.Open)) == msg.param[2])
                {
                    lvFileBrowser.SelectedItems[0].SubItems[2].Text = "Verified";
                }
                else
                {
                    lvFileBrowser.SelectedItems[0].SubItems[2].Text = "Error";
                }
                break;

            default:
                break;
            }
        }