Ejemplo n.º 1
0
        public static async Task <string> EncryptRSATextToTextAsync(string text, string publicKey)
        {
            byte[]       byteArrayText    = Encoding.UTF8.GetBytes(text);
            MemoryStream encMemoryStream1 = new MemoryStream(byteArrayText);

            byte[]       byteArrayPubKey = Encoding.UTF8.GetBytes(publicKey);
            MemoryStream streamPub       = new MemoryStream(byteArrayPubKey);

            var streamEncOut = new MemoryStream();

            using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
            {
                await pgp.EncryptAsync(encMemoryStream1, streamEncOut, streamPub);
            }

            byte[] encryptedBytes = streamEncOut.ToArray();
            var    encryptedText  = Encoding.UTF8.GetString(encryptedBytes);

            return(encryptedText);
        }
Ejemplo n.º 2
0
    public async Task <IActionResult> UploadReport()
    {
        var response = new ContentResult
        {
            StatusCode  = (int)HttpStatusCode.OK,
            ContentType = "text/plain"
        };

        try
        {
            var         newReport = new DeviceReport();
            HttpRequest request   = HttpContext.Request;

            var xs = new XmlSerializer(newReport.GetType());

            newReport =
                (DeviceReport)xs.Deserialize(new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));

            if (newReport == null)
            {
                response.Content = "notstats";

                return(response);
            }

            var reportV2 = new DeviceReportV2(newReport);
            var jsonSw   = new StringWriter();

            await jsonSw.WriteAsync(JsonConvert.SerializeObject(reportV2, Formatting.Indented,
                                                                new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            }));

            string reportV2String = jsonSw.ToString();
            jsonSw.Close();

            var newUploadedReport = new UploadedReport(reportV2);

            // Ensure CHS and CurrentCHS are not duplicates
            if (newUploadedReport.ATA?.ReadCapabilities?.CHS != null &&
                newUploadedReport.ATA?.ReadCapabilities?.CurrentCHS != null)
            {
                if (newUploadedReport.ATA.ReadCapabilities.CHS.Cylinders ==
                    newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Cylinders &&
                    newUploadedReport.ATA.ReadCapabilities.CHS.Heads ==
                    newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Heads &&
                    newUploadedReport.ATA.ReadCapabilities.CHS.Sectors ==
                    newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Sectors)
                {
                    newUploadedReport.ATA.ReadCapabilities.CHS = newUploadedReport.ATA.ReadCapabilities.CurrentCHS;
                }
            }

            // Check if the CHS or CurrentCHS of this report already exist in the database
            if (newUploadedReport.ATA?.ReadCapabilities?.CHS != null)
            {
                Chs existingChs =
                    _ctx.Chs.FirstOrDefault(c => c.Cylinders == newUploadedReport.ATA.ReadCapabilities.CHS.Cylinders &&
                                            c.Heads == newUploadedReport.ATA.ReadCapabilities.CHS.Heads &&
                                            c.Sectors == newUploadedReport.ATA.ReadCapabilities.CHS.Sectors);

                if (existingChs != null)
                {
                    newUploadedReport.ATA.ReadCapabilities.CHS = existingChs;
                }
            }

            if (newUploadedReport.ATA?.ReadCapabilities?.CurrentCHS != null)
            {
                Chs existingChs =
                    _ctx.Chs.FirstOrDefault(c =>
                                            c.Cylinders == newUploadedReport.ATA.ReadCapabilities.CurrentCHS.
                                            Cylinders &&
                                            c.Heads == newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Heads &&
                                            c.Sectors == newUploadedReport.ATA.ReadCapabilities.CurrentCHS.Sectors);

                if (existingChs != null)
                {
                    newUploadedReport.ATA.ReadCapabilities.CurrentCHS = existingChs;
                }
            }

            if (newUploadedReport.ATA?.RemovableMedias != null)
            {
                foreach (TestedMedia media in newUploadedReport.ATA.RemovableMedias)
                {
                    if (media.CHS != null &&
                        media.CurrentCHS != null)
                    {
                        if (media.CHS.Cylinders == media.CurrentCHS.Cylinders &&
                            media.CHS.Heads == media.CurrentCHS.Heads &&
                            media.CHS.Sectors == media.CurrentCHS.Sectors)
                        {
                            media.CHS = media.CurrentCHS;
                        }
                    }

                    if (media.CHS != null)
                    {
                        Chs existingChs =
                            _ctx.Chs.FirstOrDefault(c => c.Cylinders == media.CHS.Cylinders &&
                                                    c.Heads == media.CHS.Heads && c.Sectors == media.CHS.Sectors);

                        if (existingChs != null)
                        {
                            media.CHS = existingChs;
                        }
                    }

                    if (media.CHS != null)
                    {
                        Chs existingChs =
                            _ctx.Chs.FirstOrDefault(c => c.Cylinders == media.CurrentCHS.Cylinders &&
                                                    c.Heads == media.CurrentCHS.Heads &&
                                                    c.Sectors == media.CurrentCHS.Sectors);

                        if (existingChs != null)
                        {
                            media.CurrentCHS = existingChs;
                        }
                    }
                }
            }

            await _ctx.Reports.AddAsync(newUploadedReport);

            await _ctx.SaveChangesAsync();

            var pgpIn  = new MemoryStream(Encoding.UTF8.GetBytes(reportV2String));
            var pgpOut = new MemoryStream();
            var pgp    = new ChoPGPEncryptDecrypt();

            await pgp.EncryptAsync(pgpIn, pgpOut,
                                   Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
                                                "public.asc"));

            pgpOut.Position = 0;
            reportV2String  = Encoding.UTF8.GetString(pgpOut.ToArray());

            var message = new MimeMessage
            {
                Subject = "New device report (old version)",
                Body    = new TextPart("plain")
                {
                    Text = reportV2String
                }
            };

            message.From.Add(new MailboxAddress("Aaru Server", "*****@*****.**"));
            message.To.Add(new MailboxAddress("Natalia Portillo", "*****@*****.**"));

            using (var client = new SmtpClient())
            {
                await client.ConnectAsync("mail.claunia.com", 25, false);

                await client.SendAsync(message);

                await client.DisconnectAsync(true);
            }

            response.Content = "ok";

            return(response);
        }

        // ReSharper disable once RedundantCatchClause
        catch
        {
        #if DEBUG
            if (Debugger.IsAttached)
            {
                throw;
            }
        #endif
            response.Content = "error";

            return(response);
        }
    }