elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Curso de javascript por TickTack


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Ejemplo de Token Digital
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ejemplo de Token Digital  (Leído 4,702 veces)
sapito169


Desconectado Desconectado

Mensajes: 628



Ver Perfil
Ejemplo de Token Digital
« en: 5 Abril 2022, 07:59 am »

hola a qui una pequeño ejemplo de token digital

todo en memoria esta lejos de estar completo

y un parde fotitos



 
Código
  1. package otpserver.simple;
  2.  
  3.  
  4. import java.awt.event.ActionEvent;
  5. import java.security.MessageDigest;
  6. import java.time.LocalDateTime;
  7. import java.time.ZoneId;
  8. import java.util.Base64;
  9. import java.util.Random;
  10.  
  11. import javax.xml.bind.DatatypeConverter;
  12.  
  13. import javafx.application.Application;
  14. import javafx.application.Platform;
  15. import javafx.scene.Scene;
  16. import javafx.scene.layout.*;
  17. import javafx.scene.text.Text;
  18. import javafx.scene.control.*;
  19.  
  20. import javafx.stage.Stage;
  21.  
  22. public class CounterApp extends Application {
  23.  
  24.  
  25.    private final Text text = new Text();
  26.    private final Text codigoSecreto = new Text();
  27.    private final Button boton = new Button("muestra secreto");
  28.    private String secreto= "";
  29.    private boolean muestra = false;
  30.    private void incrementCount() {
  31.         try {
  32.  
  33.         long a = System.currentTimeMillis()/(1000*60);
  34.         MessageDigest instance = MessageDigest.getInstance("MD5");
  35. instance.update((secreto +a).getBytes());
  36.         byte[] digest = instance.digest();
  37.         String token = DatatypeConverter.printHexBinary(digest).toUpperCase();
  38.     text.setText(  LocalDateTime.now() +"\n"+ token.substring(0, 5) );
  39. } catch (Exception e) {
  40. throw new RuntimeException(e);
  41. }
  42.  
  43.    }
  44.  
  45.    @Override
  46.    public void start(Stage primaryStage) {
  47.  
  48.     Random random = new Random();
  49.  
  50.     for (int c = 0;c<4;c++) {
  51.     secreto+=random.nextInt(9);
  52.     }
  53.  
  54.  
  55.        VBox root = new VBox();
  56.  
  57.        root.getChildren().add(boton);
  58.        root.getChildren().add(codigoSecreto);
  59.  
  60.        root.getChildren().add(text);
  61.  
  62.  
  63.        Scene scene = new Scene(root, 200, 200);
  64.  
  65.        // longrunning operation runs on different thread
  66.        Thread thread = new Thread(new Runnable() {
  67.  
  68.            @Override
  69.            public void run() {
  70.                Runnable updater = new Runnable() {
  71.  
  72.                    @Override
  73.                    public void run() {
  74.                        incrementCount();
  75.                    }
  76.                };
  77.  
  78.                while (true) {
  79.                    try {
  80.                        Thread.sleep(1000);
  81.                    } catch (InterruptedException ex) {
  82.                    }
  83.  
  84.                    // UI update is run on the Application thread
  85.                    Platform.runLater(updater);
  86.                }
  87.            }
  88.  
  89.        });
  90.        // don't let thread prevent JVM shutdown
  91.        thread.setDaemon(true);
  92.        thread.start();
  93.  
  94.        boton.setOnAction(p->{
  95.         muestra=!muestra;
  96.         if(muestra)
  97.         codigoSecreto.setText(secreto);
  98.         else
  99.         codigoSecreto.setText("*****");
  100.        });
  101.        primaryStage.setTitle("Token Digital");
  102.        primaryStage.setScene(scene);
  103.        primaryStage.show();
  104.    }
  105.  
  106.    public static void main(String[] args) {
  107.        launch(args);
  108.    }
  109.  
  110. }
  111.  


Código
  1. package otpserver.simple;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.time.LocalDateTime;
  7. import java.time.ZoneId;
  8. import java.util.Base64;
  9. import java.util.Optional;
  10. import java.util.Random;
  11.  
  12. import javax.swing.JOptionPane;
  13. import javax.xml.bind.DatatypeConverter;
  14.  
  15. import javafx.application.Application;
  16. import javafx.application.Platform;
  17. import javafx.scene.Scene;
  18. import javafx.scene.layout.*;
  19. import javafx.scene.text.Text;
  20. import javafx.scene.control.*;
  21.  
  22. import javafx.stage.Stage;
  23.  
  24.  
  25. public class Login extends Application {
  26.  
  27. private final TextField usuario = new TextField();
  28. private final PasswordField clave = new PasswordField();
  29. private final TextField token = new TextField();
  30.  
  31. private final Button boton = new Button("login");
  32.  
  33. private boolean tieneToken = false;
  34. private String secretotoken = "";
  35.  
  36. @Override
  37. public void start(Stage primaryStage) {
  38.  
  39. usuario.setPromptText("usuario");
  40. token.setPromptText("token");
  41. token.setVisible(false);
  42. VBox root = new VBox();
  43.  
  44. root.getChildren().add(usuario);
  45.  
  46. root.getChildren().add(clave);
  47.  
  48. root.getChildren().add(token);
  49.  
  50. root.getChildren().add(boton);
  51.  
  52. Scene scene = new Scene(root, 200, 200);
  53.  
  54. // longrunning operation runs on different thread
  55.  
  56. boton.setOnAction(p -> {
  57.  
  58. if (usuario.getText().equals("admin") && clave.getText().equals("admin") && !tieneToken) {
  59. System.out.println("xd");
  60. TextInputDialog dialog = new TextInputDialog();
  61. dialog.setTitle("parear");
  62. dialog.setHeaderText("parear");
  63.  
  64. // Traditional way to get the response value.
  65. Optional<String> result = dialog.showAndWait();
  66. if (result.isPresent()) {
  67. tieneToken = true;
  68. token.setVisible(tieneToken);
  69.  
  70. }
  71.  
  72. // The Java 8 way to get the response value (with lambda expression).
  73. result.ifPresent(name -> {
  74. secretotoken=name;
  75. });
  76. }
  77.  
  78. if (usuario.getText().equals("admin") && clave.getText().equals("admin") && tieneToken) {
  79. try {
  80.  
  81.         long a = System.currentTimeMillis()/(1000*60);
  82.         MessageDigest instance = MessageDigest.getInstance("MD5");
  83. instance.update((secretotoken +a).getBytes());
  84.         byte[] digest = instance.digest();
  85.         String tokenProvar = DatatypeConverter.printHexBinary(digest).toUpperCase();
  86.         System.out.println("tokenProvar "+tokenProvar);
  87.         if(token.getText().equals(tokenProvar.substring(0,5))) {
  88.         JOptionPane.showMessageDialog(null, "exito");
  89.         }else {
  90.         JOptionPane.showMessageDialog(null, "eres falla");
  91.         }
  92. } catch (Exception e) {
  93. throw new RuntimeException(e);
  94. }
  95. }
  96.  
  97. });
  98. primaryStage.setTitle("Login");
  99. primaryStage.setScene(scene);
  100. primaryStage.show();
  101. }
  102.  
  103. public static void main(String[] args) {
  104. launch(args);
  105. }
  106.  
  107. }
  108.  













MOD: Enlaces a imágenes corregidos
Mod: Titulos descriptivos.


« Última modificación: 16 Mayo 2022, 14:46 pm por #!drvy » En línea

Maurice_Lupin


Desconectado Desconectado

Mensajes: 356

GPS


Ver Perfil WWW
Re: sapito169
« Respuesta #1 en: 15 Mayo 2022, 02:18 am »

Hola sapito169, supongo que la idea es similar a cuando se crea un token en php  ;-)

Saludos.


En línea

Un error se comete al equivocarse.
sapito169


Desconectado Desconectado

Mensajes: 628



Ver Perfil
Re: sapito169
« Respuesta #2 en: 16 Mayo 2022, 00:39 am »

El lenguaje no tiene nada q ver
Todo lo q puedes hacer en un lenguaje lo puedes hacer en otro
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
comparacion de token
Programación C/C++
bash 6 5,780 Último mensaje 11 Julio 2010, 01:30 am
por bash
Holanda eliminará el canon digital. ¿Tomará ejemplo España?
Noticias
wolfbcn 0 1,311 Último mensaje 19 Abril 2011, 18:58 pm
por wolfbcn
trabajar con un token
Programación C/C++
bash 3 3,194 Último mensaje 11 Enero 2012, 11:41 am
por Eternal Idol
csrf token
Hacking
fokin 2 3,066 Último mensaje 21 Noviembre 2013, 17:27 pm
por fokin
dudas token
Foro Libre
General Dmitry Vergadoski 0 1,066 Último mensaje 11 Octubre 2018, 18:18 pm
por General Dmitry Vergadoski
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines