示例#1
0
        public async Task <IActionResult> GenerarFirmaElectronica(GenerarFirmaElectronicaViewModel viewModel)
        {
            if (viewModel.Archivo.Length > 0)
            {
                using (var fileStream = viewModel.Archivo.OpenReadStream())
                    using (var ms = new MemoryStream())
                    {
                        fileStream.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        //string s = Convert.ToBase64String(fileBytes);

                        if (viewModel.ClaveId == null)
                        {
                            return(Ok(new Response(false, "No se encontró la clave seleccionada.")));
                        }


                        var currentUser = await _userManager.GetUserAsync(User);

                        if (!await _userManager.CheckPasswordAsync(currentUser, viewModel.Password))
                        {
                            return(Ok(new Response(false, "La contraseña es incorrecta.")));
                        }

                        var clave        = _context.Clave.SingleOrDefault(m => m.ClaveId == viewModel.ClaveId);
                        var securityData = new SecurityData(currentUser.DatosSeguridad);

                        var encPrivKey = Convert.FromBase64String(clave.EncPrivKey.Split('|')[0]);
                        var iv         = Convert.FromBase64String(clave.EncPrivKey.Split('|')[1]);

                        var decodedPrivateKey = securityData.Decrypt(encPrivKey, viewModel.Password, iv);

                        byte[] firma = LibreriaCriptografica.FirmaElectronica.GenerarFirmaElectronica(fileBytes, new System.Numerics.BigInteger(decodedPrivateKey));

                        Dictionary <string, byte[]> archivos = new Dictionary <string, byte[]>();
                        archivos.Add("firma.bin", firma.Reverse <byte>().ToArray());
                        archivos.Add("certificado.crt", clave.Certificado);

                        var firmaByteArray = CompressionHelper.CompressToZip("test.test", archivos);

                        var fileName = viewModel.Archivo.FileName + ".frm";



                        _context.RegistroFirmaElectronica.Add(new Models.FirmaElectronica()
                        {
                            ApplicationUser = currentUser,
                            Clave           = clave,
                            FechaGeneracion = DateTime.Now,
                            Hash            = LibreriaCriptografica.FirmaElectronica.ObtenerHash(fileBytes).ToHexString(),
                            NombreArchivo   = viewModel.Archivo.FileName,
                            TamanoArchivo   = viewModel.Archivo.Length
                        });

                        _context.SaveChanges();
                        Response.Headers.Add("Access-Control-Expose-Headers", new Microsoft.Extensions.Primitives.StringValues("Content-Disposition"));
                        return(File(firmaByteArray, "application/x-msdownload", fileName));
                    }
            }
            return(null);
        }