Example #1
0
        public void test_image_encription()
        {
            var img = (Bitmap)Bitmap.FromFile(FILENAME);
            CertificateSecurity sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
            var data = sec.SignData(img.ToArray());

            Assert.IsNotNull(data);
        }
Example #2
0
        /// <summary>
        /// EXAMPLE! NOT IN USE NOW
        /// </summary>
        /// <returns></returns>
        private static Action <TaskProcessOrganizer <string> .TaskItem> CreateScanActionORIGINAL()
        {
            const int TRIES = 10;

            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string barcode = null;
                string siteCode = null;

                StateManager.Item item = null;

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

#if PROC_TIFF_AS_JPEG
                        if (ext == ".tif")
                        {
                            int c = 0;

                            foreach (var im in info.FullName.TiffGetAllImages())
                            {
                                var name = info.FullName.ChangeFilePath((n) => n.Replace(ext, c++ + ext));
                                im.Save(name);
                                im.DisposeSf();
                            }

                            return;
                        }
#endif
                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();

                        item = StateManager.Default.ProcessItem_Begin(!MainForm.ms_ImportCoversheet);
                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        if (!MainForm.ms_ImportCoversheet)
                        {
                            StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;
                            FileInfo barcFilePath = null;
                            Rectangle rect = Rectangle.Empty;
                            CommonTools.ParseVoucherImage(ref bmp, ref bmpBarcode, out rect, ref barcode, BarcodeTypeEnum.BT_All);

                            vitem.Barcode = barcode;

                            if (vitem.HasBarcode)
                            {
                                string user = string.Concat("Country: ", Program.currentUser.CountryID, " User: "******"{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                        gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                        gr.DrawString(u.Cast <string>(), font, Brushes.Black, new PointF(10, 25));
                                    }
                                }, user);

                                var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                                if (!coverArea.IsEmpty)
                                {
                                    var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                                    bmp.Pixellate(coverArea);
                                }

                                List <BarcodeConfig> barcodeLayouts = StateSaver.Default.Get <List <BarcodeConfig> >(Strings.LIST_OF_BARCODECONFIGS);

                                foreach (var cfg in barcodeLayouts)
                                {
                                    if (cfg.ParseBarcode(barcode, ref data))
                                    {
                                        break;
                                    }
                                }

                                if (data == null)
                                {
                                    throw new ApplicationException("Can not find barcode");
                                }

                                item.CountryID = data.CountryID;
                                vitem.RetailerID = data.RetailerID;
                                vitem.VoucherID = data.VoucherID;
                                vitem.Barcode = barcode;

                                var barcodePath = fullFilePath.ChangeFilePath((name) => name.Replace(".", "_barcode."));
                                Global.IgnoreList.Add(barcodePath);

                                bmpBarcode.DrawOnImage((gr, s) =>
                                {
                                    using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                                    {
                                        var str = Convert.ToString(s);
                                        gr.DrawString(str, font, Brushes.Red, new PointF(10, 10));
                                    }
                                }, barcode);

                                bmpBarcode.Save(barcodePath, bmp.RawFormat);

                                barcFilePath = new FileInfo(barcodePath); // Scanned Barcode
                                vitem.FileInfoList.Add(barcFilePath);     // Scanned Barcode Image
                            }
                        }
                        else
                        {
                            MainForm.ms_ImportCoversheet = false;
                            MainForm.Default.InvokeSf(() => MainForm.Default.cbCoversheet.Checked = false);
                        }

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                        if (sec.Loaded)
                        {
                            var signFilePath = new FileInfo(Path.ChangeExtension(fullFilePath, ".sgn"));
                            item.Signature = sec.SignData(bmp.ToArray());
                            File.WriteAllBytes(signFilePath.FullName, item.Signature);
                            item.FileInfoList.Add(signFilePath);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                    }
                }
                catch (Exception ex)
                {
                    if (item != null)
                    {
                        item.State = StateManager.eState.Err;
                        item.Message = ex.Message;
                    }
                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = siteCode,
                        FilePath = fullFilePath
                    };

                    if (Error != null)
                    {
                        Error(null, new ThreadExceptionEventArgs(ex));
                    }
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();

                    MainForm.Default.m_MainContext.Post(MainForm.Default.ShowItemScannedCallback, item);

                    try
                    {
                        StateManager.Default.ProcessItem_End(item);
                    }
                    catch (Exception ex0)
                    {
                        if (item != null)
                        {
                            item.State = StateManager.eState.Err;
                            item.Message = ex0.Message;
                        }

                        var scex = new ScanException(ex0, data)
                        {
                            SiteCode = siteCode,
                            FilePath = fullFilePath
                        };

                        if (Error != null)
                        {
                            Error(null, new ThreadExceptionEventArgs(scex));
                        }
                    }
                }
            }));
        }
Example #3
0
        public void test_find_certificate()
        {
            CertificateSecurity sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);

            Assert.IsNotNull(sec.Loaded);
        }
Example #4
0
 protected override void BeginProcessing() => enc = new CertificateSecurity();
        public Action <TaskProcessOrganizer <string> .TaskItem> GetAction()
        {
            return(new Action <TaskProcessOrganizer <string> .TaskItem>((o) =>
            {
                var fullFilePath = o.Item;

                Bitmap bmp = null;
                Bitmap bmpBarcode = null;
                BarcodeData data = null;

                string barcode = null;
                string siteCode = null;

                StateManager.VoucherItem item = (StateManager.VoucherItem)StateManager.Default.ProcessItem_Begin(true);

                try
                {
                    var info = new FileInfo(fullFilePath);

                    if (info.Exists && !info.IsReadOnly(TRIES))
                    {
                        // ".tif"
                        var ext = Path.GetExtension(info.FullName);

                        bmp = ((Bitmap)Bitmap.FromFile(info.FullName)).Crop2();
                        item.FullFileName = fullFilePath;
                        item.FileInfoList.Add(new FileInfo(fullFilePath)); // Scanned Image

                        StateManager.VoucherItem vitem = (StateManager.VoucherItem)item;
                        FileInfo barcFilePath = null;
                        Rectangle rect = Rectangle.Empty;
                        CommonTools.ParseVoucherImage(ref bmp, ref bmpBarcode, out rect, ref barcode, BarcodeTypeEnum.BT_All);
                        vitem.Barcode = barcode;

                        string user = string.Concat("Country: ", Program.currentUser.CountryID, " User: "******"{0:dd-MM-yyyy hh:mm}", DateTime.Now);
                                gr.DrawString(str, font, Brushes.Black, new PointF(10, 10));
                                gr.DrawString(u.Cast <string>(), font, Brushes.Black, new PointF(10, 25));
                            }
                        }, user);

                        var coverArea = StateSaver.Default.Get <Rectangle>(Strings.VOUCHERCOVERREGION);
                        if (!coverArea.IsEmpty)
                        {
                            var size = StateSaver.Default.Get <int>(Strings.PIXELSIZE, 5);
                            bmp.Pixellate(coverArea);
                        }

                        if (vitem.HasBarcode)
                        {
                            List <BarcodeConfig> barcodeLayouts = StateSaver.Default.Get <List <BarcodeConfig> >(Strings.LIST_OF_BARCODECONFIGS);

                            foreach (var cfg in barcodeLayouts)
                            {
                                if (cfg.ParseBarcodeSafe(barcode, ref data))
                                {
                                    break;
                                }
                            }

                            if (data == null)
                            {
                                throw new ApplicationException("Barcode invalid");
                            }

                            vitem.CountryID = data.CountryID;
                            vitem.RetailerID = data.RetailerID;
                            vitem.VoucherID = data.VoucherID;
                            vitem.Barcode = barcode;

                            var barcodePath = fullFilePath.ChangeFilePath((name) => name.Replace(".", "_barcode."));
                            Global.IgnoreList.Add(barcodePath);

                            bmpBarcode.DrawOnImage((gr, s) =>
                            {
                                using (var font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular))
                                {
                                    var str = Convert.ToString(s);
                                    gr.DrawString(str, font, Brushes.Red, new PointF(10, 10));
                                }
                            }, barcode);

                            bmpBarcode.Save(barcodePath, bmp.RawFormat);

                            barcFilePath = new FileInfo(barcodePath); // Scanned Barcode
                            vitem.FileInfoList.Add(barcFilePath);     // Scanned Barcode Image
                        }

                        item = StateManager.Default.AddTransferFileItem(item);

                        if (item.CountryID == 0)
                        {
                            item.CountryID = MainForm.ms_DefaultCountryId;
                        }

                        if (!ext.EqualNoCase(".tif"))
                        {
                            bmp.Save(fullFilePath, ImageFormat.Jpeg);
                        }

                        if (item.Thumbnail == null)
                        {
                            item.Thumbnail = bmp.GetThumbnailImage(45, 45, () => false, IntPtr.Zero);
                        }

                        var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                        if (sec.Loaded)
                        {
                            var signFilePath = new FileInfo(Path.ChangeExtension(fullFilePath, ".sgn"));
                            item.Signature = sec.SignData(bmp.ToArray());
                            File.WriteAllBytes(signFilePath.FullName, item.Signature);
                            item.FileInfoList.Add(signFilePath);
                            Global.IgnoreList.Add(signFilePath.FullName);
                        }

                        item.State = StateManager.eState.OK;
                        item.Message = "";
                        StateManager.Default.CompleteItem(item);
                    }
                }
                catch (Exception ex)
                {
                    item.State = StateManager.eState.Err;
                    item.Message = ex.Message;

                    var scex = new ScanException(ex, data)
                    {
                        SiteCode = siteCode,
                        FilePath = fullFilePath
                    };

                    int count = StateManager.Default.SetItemWithErr();
                    DelegateHelper.PostShowItemsWithErrCallback(count);
                    DelegateHelper.FireError(this, ex);
                }
                finally
                {
                    bmp.DisposeSf();
                    bmpBarcode.DisposeSf();
                    DelegateHelper.PostItemScannedCallback(item);
                    StateManager.Default.ShowNextItemExpected();
                }
            }));
        }
Example #6
0
        private void DownloadOrganizer_Completed(object sender, TaskProcessOrganizer <StateManager.Item> .CompletedEventArgs e)
        {
            string fullFileName = m_DownloadFileOrganizer.Data.GetValueAndRemove <StateManager.Item, string>(e.Value);

            if (!fullFileName.IsNullOrEmpty())
            {
                var temp = new DirectoryInfo(Path.GetTempPath());
                var gdir = temp.Combine(e.Value.SessionID.ToString());
                gdir.EnsureDirectory();
                e.Value.FileInfoList.AddRange(ZipFileAccess.Instance.ExtractFileZip(fullFileName, gdir.FullName));

                var cover = ServiceDataAccess.Instance.ReadCoverInfo(e.Value.Id);
                if (!string.IsNullOrWhiteSpace(cover))
                {
                    var covers      = cover.ToObject <List <Rectangle> >();
                    var voucherFile = e.Value.FileInfoList.Max((i1, i2) => i1.Length > i2.Length);
                    using (var bmp = (Bitmap)Bitmap.FromFile(voucherFile.FullName))
                    {
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            foreach (var area in covers)
                            {
                                bmp.Pixellate(area);
                                g.DrawString(string.Format("{0}", area), this.Font, Brushes.BlueViolet, area.Left, area.Top);
                            }
                        }

                        var newvoucherfile = voucherFile.Rename((i) => i.GetFileNameWithoutExtension() + "_new");
                        e.Value.FileInfoList.Remove(voucherFile);
                        e.Value.FileInfoList.Add(newvoucherfile);
                        bmp.Save(newvoucherfile.FullName, ImageFormat.Jpeg);
                    }
                    voucherFile.DeleteSafe();
                }

                var sgn = e.Value.FileInfoList.FirstOrDefault(f => f.Extension == ".sgn");
                if (sgn != null)
                {
                    var sec = new CertificateSecurity(X509FindType.FindBySerialNumber, Strings.CERTNUMBER, StoreLocation.LocalMachine);
                    if (sec.Loaded)
                    {
                        FileInfo fi  = null;
                        long     max = 0;
                        foreach (var f in e.Value.FileInfoList)
                        {
                            if (f.Length > max)
                            {
                                max = f.Length;
                                fi  = f;
                            }
                        }
                        byte[] voucher   = fi.ReadAllBytes();
                        byte[] signature = e.Value.Signature = sgn.ReadAllBytes();
                        e.Value.IsSignatureValid = sec.Verify(voucher, signature);
                    }
                }

                var vi = e.Value as StateManager.VoucherItem;
                if (vi != null)
                {
                    ServiceDataAccess.Instance.SaveHistory(OperationHistory.FileDownloaded, e.Value.CountryID, vi.RetailerID, vi.VoucherID, 0, 1,
                                                           e.Value.ToString());
                }

                if (m_DownloadFileOrganizer.Data.ContainsKey(e.Value.SessionID))
                {
                    ItemControl selectedItemCnt = m_DownloadFileOrganizer.Data.GetValueAndRemove <Guid, ItemControl>(e.Value.SessionID);
                    this.InvokeSf(() => { selectedItemCnt.UnLock(); });
                }
                this.InvokeSf(() => SelectFilesForm.ShowFiles(this, e.Value.FileInfoList));
            }
        }