Skip to content

Commit

Permalink
Arreglar error cuando la entrada esta vacia y se intenta aplicar el hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Araozu committed Jun 27, 2021
1 parent bab3769 commit 335b0b6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
27 changes: 15 additions & 12 deletions src/araoz/hash/Controller.fxml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?><?import javafx.scene.layout.*?><?import javafx.scene.text.*?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="araoz.hash.Controller">
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.2"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="araoz.hash.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
Expand All @@ -18,34 +17,37 @@
<RowConstraints />
</rowConstraints>
<children>
<Label text="Ingrese el texto plano:">
<Label text="Texto plano:">
<font>
<Font name="Inter" size="13.0" />
</font>
</Label>
<TextArea fx:id="texto_entrada" prefHeight="200.0" prefWidth="500.0" promptText="Texto plano" GridPane.columnIndex="0" GridPane.rowIndex="1">
<TextArea fx:id="texto_entrada" prefHeight="200.0" prefWidth="500.0" promptText="Texto plano"
GridPane.columnIndex="0" GridPane.rowIndex="1">
<font>
<Font name="Inter" size="13.0" />
</font>
</TextArea>
<GridPane alignment="center" prefHeight="50.0" prefWidth="500.0" GridPane.columnIndex="0" GridPane.rowIndex="2">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="20.0" />
<ColumnConstraints />
</columnConstraints>
<children>
<Label text="Codificación:">
<Label prefHeight="16.0" prefWidth="176.0" text="Codificación del texto plano:">
<font>
<Font name="Inter" size="13.0" />
</font>
</Label>
<RadioButton fx:id="codificacion_utf8" mnemonicParsing="false" prefHeight="18.0" prefWidth="112.0" selected="true" text="UTF-8" GridPane.columnIndex="1">
<RadioButton fx:id="codificacion_utf8" mnemonicParsing="false" prefHeight="18.0" prefWidth="77.0"
selected="true" text="UTF-8" GridPane.columnIndex="1">
<toggleGroup>
<ToggleGroup fx:id="tipo_entrada" />
</toggleGroup>
</RadioButton>
<RadioButton fx:id="codificacion_hex" mnemonicParsing="false" prefHeight="18.0" prefWidth="209.0" text="HEX" toggleGroup="$tipo_entrada" GridPane.columnIndex="2" />
<RadioButton fx:id="codificacion_hex" mnemonicParsing="false" prefHeight="18.0" prefWidth="110.0"
text="HEX" toggleGroup="$tipo_entrada" GridPane.columnIndex="2" />
</children>
<rowConstraints>
<RowConstraints />
Expand All @@ -61,7 +63,8 @@
</rowConstraints>
</GridPane>
<Label fx:id="label_hash_resultado" text="Hash resultado (hex):" GridPane.rowIndex="4" />
<TextArea fx:id="texto_salida" editable="false" prefHeight="200.0" prefWidth="200.0" promptText="Hash" GridPane.rowIndex="5">
<TextArea fx:id="texto_salida" editable="false" prefHeight="200.0" prefWidth="200.0" promptText="Hash"
GridPane.rowIndex="5">
<font>
<Font name="Inter" size="13.0" />
</font>
Expand Down
6 changes: 5 additions & 1 deletion src/araoz/hash/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public class Controller {
public void calculateSHA1(MouseEvent mouseEvent) {
boolean esHex = codificacion_hex.isSelected();
String entrada = texto_entrada.getText();
String resultado = esHex? SHA1.runHEX(entrada) : SHA1.runUTF8(entrada);
if (entrada.isEmpty()) {
texto_salida.setText("");
return;
}
String resultado = esHex ? SHA1.runHEX(entrada) : SHA1.runUTF8(entrada);
texto_salida.setText(resultado);
}
}
3 changes: 0 additions & 3 deletions src/araoz/hash/SHA1.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ private static String run(byte[][] bloques) {

StringBuilder s = new StringBuilder();
for (int palabra : estadoActual) {
// Codificar en UTF-8
// byte[] bytes = ByteBuffer.allocate(4).putInt(palabra).array();
// s.append(new String(bytes, StandardCharsets.UTF_8));
s.append(Integer.toHexString(palabra));
}

Expand Down
20 changes: 12 additions & 8 deletions src/araoz/hash/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,22 @@ public static int[] dividirBloqueAPalabras32(byte[] bloque) {
return palabras;
}

public static long lShiftToLong(byte b, int n) {
return ((long) b << 56L) >>> (56L - n);
}

public static long[] dividirBloqueAPalabras64(byte[] bloque) {
long[] palabras = new long[bloque.length / 8];
for (int i = 0; i < bloque.length / 8; i++) {
int posicionBase = i * 8;
palabras[i] = bloque[posicionBase]
| bloque[posicionBase << 8]
| bloque[posicionBase << 16]
| bloque[posicionBase << 24]
| bloque[posicionBase << 32]
| bloque[posicionBase << 40]
| bloque[posicionBase << 48]
| bloque[posicionBase << 56];
palabras[i] = lShiftToLong(bloque[posicionBase], 56) |
lShiftToLong(bloque[posicionBase + 1], 48) |
lShiftToLong(bloque[posicionBase + 2], 40) |
lShiftToLong(bloque[posicionBase + 3], 32) |
lShiftToLong(bloque[posicionBase + 4], 24) |
lShiftToLong(bloque[posicionBase + 5], 16) |
lShiftToLong(bloque[posicionBase + 6], 8) |
lShiftToLong(bloque[posicionBase + 7], 0);
}
return palabras;
}
Expand Down

0 comments on commit 335b0b6

Please sign in to comment.