×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dorian Cuentas
Added: Jul 2, 2015 8:56 PM
Views: 1878
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package integracion.tarjeta.de.coordenadas;
  7.  
  8. import com.senhaforte.action.Utils;
  9. import com.senhaforte.classes.ChallengeRetorno;
  10. import com.senhaforte.classes.DBc;
  11. import com.senhaforte.classes.Grupo;
  12. import com.senhaforte.classes.Retorno;
  13. import com.senhaforte.classes.Ticket;
  14. import com.senhaforte.classes.Usuario;
  15. import com.senhaforte.security.Challenge;
  16. import com.sun.media.sound.ReferenceCountingDevice;
  17. import java.awt.Color;
  18. import java.awt.Graphics2D;
  19. import java.awt.GridBagLayout;
  20. import java.awt.Rectangle;
  21. import java.awt.image.BufferedImage;
  22. import java.awt.image.RenderedImage;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import java.util.Arrays;
  27. import java.util.Scanner;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javax.imageio.ImageIO;
  31. import javax.imageio.stream.FileImageOutputStream;
  32. import javax.imageio.stream.ImageOutputStream;
  33. import javax.imageio.stream.MemoryCacheImageOutputStream;
  34. import javax.swing.JEditorPane;
  35. import javax.swing.JLabel;
  36. import javax.swing.JPanel;
  37. import javax.swing.JTextArea;
  38. import javax.swing.SwingUtilities;
  39.  
  40. /**
  41.  *
  42.  * @author user
  43.  */
  44. public class Autenticación {
  45.  
  46.     /**
  47.      *
  48.      * @param gridArray
  49.      * @return styles from http://www.tablesgenerator.com/html_tables
  50.      */
  51.     public static StringBuffer createHtmlGrid(String[] gridCharArray) {
  52.         final StringBuffer htmlStringBuffer = new StringBuffer();
  53.         htmlStringBuffer.append("<html>");
  54.         htmlStringBuffer.append(
  55.                 "<style type=\"text/css\">"
  56.                 + ".table width:600px;height:400px"
  57.                         + ".th height:15px"
  58.                         + ".tg  {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}"
  59.                 + ".tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;}"
  60.                 + ".tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;}"
  61.                 + ".tg .tg-s6z2{text-align:center}"
  62.                 + ".tg .tg-igvl{background-color:#b9c9fe;color:#003399;text-align:center}"
  63.                 + "</style>"
  64.                 + "<table class=\"tg\">"
  65.                 + "  <tr>"
  66.                 + "    <th class=\"tg-s6z2\"></th>"
  67.                 + "    <th class=\"tg-s6z2\">A<br></th>"
  68.                 + "    <th class=\"tg-s6z2\">B</th>"
  69.                 + "    <th class=\"tg-s6z2\">C</th>"
  70.                 + "    <th class=\"tg-s6z2\">D</th>"
  71.                 + "    <th class=\"tg-s6z2\">E</th>"
  72.                 + "    <th class=\"tg-s6z2\">F</th>"
  73.                 + "    <th class=\"tg-s6z2\">G</th>"
  74.                 + "    <th class=\"tg-s6z2\">H</th>"
  75.                 + "    <th class=\"tg-s6z2\">I</th>"
  76.                 + "    <th class=\"tg-s6z2\">J</th>"
  77.                 + "  </tr>"
  78.                 + "  <tr>");
  79.  
  80.         for (int i = 0; i < gridCharArray.length; i++) {
  81.             if (((i) % 10 == 0)) {
  82.                 if (i > 9) {
  83.                     htmlStringBuffer.append("  </tr>");
  84.                 }
  85.                 htmlStringBuffer.append(
  86.                         "  <tr>" + "    <td class=\"tg-igvl\">" + String.valueOf((i / 10) + 1) + "</td>");
  87.             }
  88.             htmlStringBuffer.append("    <td class=\"tg-s6z2\">" + gridCharArray[i] + "</td>");
  89.         }
  90.         htmlStringBuffer.append("</table>");
  91.         htmlStringBuffer.append("</html>");
  92.         return htmlStringBuffer;
  93.     }
  94.  
  95.     /**
  96.      *
  97.      * @param imageOutputStream
  98.      * @param htmlGrid
  99.      */
  100.     public static void createImageGrid(ImageOutputStream imageOutputStream, String htmlGrid, final int with, final int height) {
  101.         final JPanel containerComponent = new JPanel();
  102.         containerComponent.setBackground(Color.WHITE);
  103.         JLabel htmlTextComponent = new JLabel();
  104.         htmlTextComponent.setBackground(Color.WHITE);
  105.         htmlTextComponent.setText(htmlGrid);
  106.         htmlTextComponent.repaint();
  107.         containerComponent.setLayout(new GridBagLayout());
  108.         containerComponent.add(htmlTextComponent);
  109.         containerComponent.setSize(with, height);
  110.         containerComponent.validate();
  111.  
  112.         //create a new image
  113.         final BufferedImage image = new BufferedImage(containerComponent.getWidth(), containerComponent.getHeight(), BufferedImage.TYPE_INT_ARGB);
  114.  
  115.         SwingUtilities.invokeLater(new Runnable() {
  116.  
  117.             @Override
  118.             public void run() {
  119.                 try {
  120. //                    SwingUtilities.paintComponent(image.createGraphics(), jPanel, new JPanel(), new Rectangle(0, 0, jPanel.getWidth(), jPanel.getHeight()));
  121.                     containerComponent.setSize(with, height);
  122.                     containerComponent.validate();
  123.                     containerComponent.doLayout();
  124.                     Graphics2D g = image.createGraphics();
  125.                     containerComponent.print(g);
  126.                     g.dispose();
  127.  
  128.                     ImageIO.write((RenderedImage) image, "png", new File("C:\\Users\\user\\Desktop\\tarjeta.png"));
  129.                 } catch (IOException ex) {
  130.                     Logger.getLogger(Autenticación.class.getName()).log(Level.SEVERE, null, ex);
  131.                 }
  132.             }
  133.         });
  134.     }
  135.  
  136.     public static void main(String[] args) throws IOException {
  137.         Grupo g = Grupo.BuscaGrupo("NotSel", "NotSelADM", "VdJRYHFK");  //Esta linha adiciona suas credenciais
  138.         DBc c = new DBc(); //Cria conexao com nosso banco de dados
  139.         Usuario user = Usuario.buscaUsuario(g, "dcuentas", c);
  140.  
  141.         String cartaoGrid = user.getCartaoGrid();
  142.         final String[] charArray = cartaoGrid.split(",");
  143.         final StringBuffer stringBuffer = new StringBuffer();
  144.  
  145.         stringBuffer.append("\tA\tB\tC\tD\tE\tF\tG\tH\tI\tJ");
  146. //        System.out.print("1\t");
  147.         for (int i = 0; i < charArray.length; i++) {
  148.             if (((i) % 10 == 0)) {
  149.                 stringBuffer.append("");
  150.                 stringBuffer.append(String.valueOf((i / 10) + 1) + "\t");
  151.             }
  152.             stringBuffer.append(charArray[i] + "\t");
  153.         }
  154.         System.out.println(stringBuffer.toString());
  155.        
  156.         StringBuffer htmlGrid = createHtmlGrid(charArray);
  157.         createImageGrid(new FileImageOutputStream(new File("C:\\Users\\user\\Desktop\\tarjeta.png")), htmlGrid.toString(), 500, 500);
  158.  
  159. //        jPanel.paintComponents(image.createGraphics());
  160.         ChallengeRetorno cr = Challenge.GeraChallenge(g, "dcuentas", c);
  161.         String[] desafio = cr.getDesafio();
  162.         System.out.println("");
  163.         System.out.println("Desafio criado a partir de Grid - " + cr.getSerial_grid());
  164.         System.out.println("Responda ao desafio:");
  165.         System.out.println("");
  166.         System.out.println("ingrese las coordenadas : " + desafio[0] + " - " + desafio[1] + " - " + desafio[2]);
  167.  
  168. //        String geraDesafio = Challenge.geraDesafio();
  169. //        System.out.println("desafio : " + geraDesafio);
  170. //        System.out.println("");
  171. //        final int[][] coords = new int[][]{{1, 2}, {1, 2}, {1, 3}};
  172. //        char[] BuscaValorGrid = Challenge.BuscaValorGrid(coords, user);
  173. //        System.out.println(Arrays.toString(BuscaValorGrid));
  174.         System.out.println("");
  175.         Scanner in = new Scanner(System.in);
  176.         char[] segredos = new char[3];
  177.         segredos[0] = in.nextLine().charAt(0);
  178.         segredos[1] = in.nextLine().charAt(0);
  179.         segredos[2] = in.nextLine().charAt(0);
  180.  
  181.         System.out.println("enviando coordenadas : " + segredos[0] + " - " + segredos[1] + " - " + segredos[2]);
  182.         boolean ret = Challenge.RespostaChallenge(c.CriaConn(), segredos[0], segredos[1], segredos[2], user);
  183.  
  184.         System.out.println("autenticación : " + ret);
  185.     }
  186. }
  187.