Exemplo n.º 1
0
 private static bool CreateSafeFile(FileInfo fi, CoinStack stack)
 {
     try
     {
         Directory.CreateDirectory(fi.DirectoryName);
         var json        = JsonConvert.SerializeObject(stack);
         var cryptedjson = Utils.Encrypt(json, userEnteredPassword, salt);
         using (var fs = fi.Create())
         {
             fs.Write(encryptedUserEnteredPassword, 0, 60);
             fs.Write(cryptedjson, 0, cryptedjson.Length);
         }
         return(true);
     }
     catch (JsonException ex)
     {
         //MessageBox.Show("Safe.CreateSafeFile() JSON exception: " + ex.Message);
         return(false);
     }
     catch (CryptographicException ex)
     {
         //MessageBox.Show("Safe.CreateSafeFile() encryption exception: " + ex.Message);
         return(false);
     }
     catch (IOException ex)
     {
         //MessageBox.Show("Safe.CreateSafeFile() IO write exception: " + ex.Message);
         return(false);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Exemplo n.º 2
0
 public void Add(CoinStack stack)
 {
     Contents.Add(stack);
     RemoveCounterfeitCoins();
     onSafeContentChanged(new EventArgs());
     Save();
 }
Exemplo n.º 3
0
 public void Remove(CoinStack stack2)
 {
     foreach (CloudCoin coin in stack2)
     {
         cloudcoin.Remove(coin);
     }
 }
Exemplo n.º 4
0
 private Safe(FileInfo fi, CoinStack coins)
 {
     safeFilePath = fi.FullName;
     bkpFilePath  = fi.FullName + ".bkp";
     safeFileInfo = fi;
     bkpFileInfo  = new FileInfo(bkpFilePath);
     Contents     = coins;
 }
Exemplo n.º 5
0
        public void Add(CoinStack stack2)
        {
            foreach (CloudCoin coin in stack2)
            {
                cloudcoin.Add(coin);
            }
            var tmp = cloudcoin.Distinct();

            cloudcoin = new HashSet <CloudCoin>(tmp);
        }
Exemplo n.º 6
0
 public CoinStackOut(CoinStack stack)
 {
     cloudcoin = new List <CloudCoinOut>();
     foreach (CloudCoin coin in stack.cloudcoin)
     {
         cloudcoin.Add(new CloudCoinOut()
         {
             sn = coin.sn.ToString(), nn = coin.nn.ToString(), an = coin.an, aoid = coin.aoid, ed = coin.ed
         });
     }
 }
Exemplo n.º 7
0
        private void RemoveCounterfeitCoins()
        {
            var coinStack = new CoinStack(Contents);

            foreach (CloudCoin coin in coinStack)
            {
                if (coin.Verdict == CloudCoin.Status.Counterfeit)
                {
                    Contents.cloudcoin.Remove(coin);
                }
            }
        }
Exemplo n.º 8
0
        private CoinStack ReadJson(Stream jsonFS)
        {
            jsonFS.Position = 0;
            StreamReader sr    = new StreamReader(jsonFS);
            CoinStack    stack = null;

            try
            {
                stack = JsonConvert.DeserializeObject <CoinStack>(sr.ReadToEnd());
            }
            catch (JsonException ex)
            {
                throw;
            }
            return(stack);
        }
Exemplo n.º 9
0
        public bool SaveOutStack(CoinStack stack, int desiredSum, bool isJSON, string note)
        {
            if (stack != null && stack.Count <CloudCoin>() != 0)
            {
                onSafeContentChanged(new EventArgs());
                CoinStackOut st            = new CoinStackOut(stack);
                string       serialNumbers = "";
                var          appDelegate   = (AppDelegate)UIApplication.SharedApplication.Delegate;

                foreach (CloudCoin coin in stack)
                {
                    serialNumbers += coin.sn.ToString() + "\n";
                }

                if (isJSON)
                {
                    string fn = appDelegate.ExportDir + desiredSum + "."
                                + note + "." + DateTime.Now.ToString("dd-MM-yy.HH-mm") + ".stack";
                    st.SaveInFile(fn);

                    ExportedPaths = new List <string>()
                    {
                        fn
                    };
                }
                else
                {
                    var cloudCoinFile = new CloudCoinFile();
                    ExportedPaths = new List <string>();
                    string path = "";

                    foreach (var coin in stack)
                    {
                        var success = cloudCoinFile.WriteJpeg(coin, note, ref path);
                        if (success)
                        {
                            ExportedPaths.Add(path);
                        }
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 10
0
        public void Detect(CoinStack stack, bool isCoinToBeImported)
        {
            Stopwatch total = new Stopwatch();

            total.Start();
            Task[]      checkStackTasks = new Task[stack.cloudcoin.Count()];
            Stopwatch[] tw = new Stopwatch[stack.cloudcoin.Count()];
            int         k  = 0;

            foreach (CloudCoin coin in stack)    // int k = 0; k < stack.cloudcoin.Count(); k++)
            {
                //var coin = stack.cloudcoin[k];
                if (!isCoinToBeImported)
                {
                    coin.pans = coin.an;
                }
                Task <DetectResponse>[] checkCoinTasks = new Task <DetectResponse> [NODEQNTY];
                var t = tw[k] = new Stopwatch();
                t.Start();
                foreach (Node node in Instance.NodesArray)
                {
                    checkCoinTasks[node.Number] = node.Detect(coin);
                    checkCoinTasks[node.Number].ContinueWith((anc) =>
                    {
                        var tmp = anc.Result;
                        coin.detectStatus[node.Number] = (tmp.status == "pass") ? CloudCoin.raidaNodeResponse.pass : (tmp.status == "fail") ? CloudCoin.raidaNodeResponse.fail : CloudCoin.raidaNodeResponse.error;
                    });
                }
                checkStackTasks[k] = Task.Factory.ContinueWhenAll(checkCoinTasks, (ancs) =>
                {
                    t.Stop();
                    onDetectCoinCompleted(new DetectCoinCompletedEventArgs(coin, t));
                });
                k++;
            }
            Task.Factory.ContinueWhenAll(checkStackTasks, (ancs) =>
            {
                onStackScanCompleted(new StackScanCompletedEventArgs(stack, total, null));
                Logger.Write(string.Format("Import {0} Coins", total), Logger.Level.Normal);
            });
        }
Exemplo n.º 11
0
        private static Safe GetInstance()
        {
            string filePath    = Environment.ExpandEnvironmentVariables(GetSafeFilePath());
            string bkpFilePath = filePath + ".bkp";

            var fileInfo    = new FileInfo(filePath);
            var bkpFileInfo = new FileInfo(bkpFilePath);

            if (!bkpFileInfo.Exists)
            {
                using (var tmp = bkpFileInfo.Create())
                {
                    tmp.Close();
                }
            }

            //userEnteredPassword = UserInteract.Password; //get user password for Safe
            //get user password and check against stored in file

            userEnteredPassword = "";

            if (!fileInfo.Exists)
            { //Safe does not exist, create one
                if (userEnteredPassword != "error")
                {
                    encryptedUserEnteredPassword = Encoding.UTF8.GetBytes(Crypter.Blowfish.Crypt(Encoding.UTF8.GetBytes(userEnteredPassword)));
                    var coins = new CoinStack();
                    if (CreateSafeFile(fileInfo, coins))
                    {
                        theOnlySafeInstance = new Safe(fileInfo, coins);
                        return(theOnlySafeInstance);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else //Safe already exists
            {
                if (userEnteredPassword != "error")
                {
                    encryptedUserEnteredPassword = Encoding.UTF8.GetBytes(Crypter.Blowfish.Crypt(Encoding.UTF8.GetBytes(userEnteredPassword)));
                    CoinStack safeContents = ReadSafeFile(fileInfo);
                    if (safeContents != null)
                    {
                        theOnlySafeInstance = new Safe(fileInfo, safeContents);
                        return(theOnlySafeInstance);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
        }
Exemplo n.º 12
0
        public List <Func <Task> > GetMultiDetectTasks(CoinStack coinStack, int milliSecondsToTimeOut, bool changeANs = true)
        {
            CloudCoin[] coins = coinStack.ToArray <CloudCoin>();
            //this.coins = cloudCoins;

            responseArrayMulti = new Response[Config.NodeCount, coins.Length];

            int[] nns = new int[coins.Length];
            int[] sns = new int[coins.Length];


            String[][] ans  = new String[Config.NodeCount][];
            String[][] pans = new String[Config.NodeCount][];

            int[] dens = new int[coins.Length]; //Denominations
                                                //Stripe the coins
            var detectTasks = new List <Func <Task> >
            {
            };

            List <Func <Task> > multiTaskList = new List <Func <Task> >();

            //List<Task<Response[]>> multiTaskList = new List<Task<Response[]>>();
            for (int i = 0; i < coins.Length; i++)//For every coin
            {
                //if (changeANs)
                coins[i].generatePans();
                //else
                //coins[i].SetAnsToPans();
                //coins[i].setAnsToPans();
                nns[i]  = coins[i].nn;
                sns[i]  = coins[i].sn;
                dens[i] = coins[i].getDenomination();
            }
            multiRequest         = new MultiDetectRequest();
            multiRequest.timeout = Config.milliSecondsToTimeOut;
            for (int nodeNumber = 0; nodeNumber < Config.NodeCount; nodeNumber++)
            {
                ans[nodeNumber]  = new String[coins.Length];
                pans[nodeNumber] = new String[coins.Length];

                for (int i = 0; i < coins.Length; i++)//For every coin
                {
                    ans[nodeNumber][i]  = coins[i].an[nodeNumber];
                    pans[nodeNumber][i] = coins[i].pans[nodeNumber];
                }
                multiRequest.an[nodeNumber]  = ans[nodeNumber];
                multiRequest.pan[nodeNumber] = pans[nodeNumber];
                multiRequest.nn = nns;
                multiRequest.sn = sns;
                multiRequest.d  = dens;
            }


            for (int nodeNumber = 0; nodeNumber < Config.NodeCount; nodeNumber++)
            {
                detectTasks.Add(NodesArray[nodeNumber].MultiDetect);
            }

            return(detectTasks);
        }
Exemplo n.º 13
0
 public StackScanCompletedEventArgs(CoinStack st, Stopwatch stwtch, RAIDA raida)
 {
     stack      = st;
     sw         = stwtch;
     this.raida = raida;
 }