Skip to content

Commit

Permalink
Agregar entrada HEX. Arreglar error en rotacionCircularIzquierda
Browse files Browse the repository at this point in the history
  • Loading branch information
Araozu committed Jun 27, 2021
1 parent de9da19 commit 04960c7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
File renamed without changes.
7 changes: 3 additions & 4 deletions src/araoz/hash/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public class Controller {
public RadioButton codificacion_hex;

public void calculateSHA1(MouseEvent mouseEvent) {
System.out.println("Radio HEX: " + codificacion_hex.isSelected());
System.out.println("Radio UTF-8" + codificacion_utf8.isSelected());
System.out.println("Entrada: " + texto_entrada.getText());
String resultado = SHA1.run(texto_entrada.getText());
boolean esHex = codificacion_hex.isSelected();
String entrada = texto_entrada.getText();
String resultado = esHex? SHA1.runHEX(entrada) : SHA1.runUTF8(entrada);
texto_salida.setText(resultado);
}
}
6 changes: 5 additions & 1 deletion src/araoz/hash/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("Controller.fxml"));
primaryStage.setTitle("Funciones Hash");
Scene s = new Scene(root, 800, 700);
primaryStage.setScene(s);
primaryStage.show();
}

public static void main(String[] args) {
byte b = (byte) Integer.parseInt("FF", 16);

System.out.println(b);
System.out.println(Integer.toBinaryString((b & 0xFF) + 0x100).substring(1));
launch(args);
}

Expand Down
22 changes: 18 additions & 4 deletions src/araoz/hash/SHA1.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static int constanteRonda(int numRonda) {
* @return X rotado n bits a la izquierda
*/
private static int rotacionCircularIzq(int X, int n) {
return (X << n) | (X >> (32 - n));
return (X << n) | (X >>> (32 - n));
}

/**
Expand Down Expand Up @@ -85,6 +85,14 @@ private static void procesarBloque(byte[] bloque, int[] estado) {
C = rotacionCircularIzq(B, 30);
B = A;
A = TEMP;

// FIXME
System.out.println(Integer.toHexString(A) + "\t"
+ Integer.toHexString(B) + "\t"
+ Integer.toHexString(C) + "\t"
+ Integer.toHexString(D) + "\t"
+ Integer.toHexString(E) + "\t"
);
}

estado[0] += A;
Expand All @@ -94,9 +102,7 @@ private static void procesarBloque(byte[] bloque, int[] estado) {
estado[4] += E;
}

public static String run(String input) {
byte[][] bloques = Utils.dividirString(input, 64);

private static String run(byte[][] bloques) {
// Estado inicial, a modificarse en cada bloque
int[] estadoActual = {
0x67452301,
Expand All @@ -120,4 +126,12 @@ public static String run(String input) {
return s.toString();
}

public static String runHEX(String hex) {
return run(Utils.dividirHex(hex, 64));
}

public static String runUTF8(String input) {
return run(Utils.dividirString(input, 64));
}

}
46 changes: 41 additions & 5 deletions src/araoz/hash/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

public class Utils {

public static byte[][] dividirString(String input, int tamanoBloque) {
byte[] rawInput = input.getBytes(StandardCharsets.UTF_8);
// TODO: Puede que altere los valores binarios?
private static byte hexStr2AByte(String s) {
return (byte) Integer.parseInt(s, 16);
}

/**
* Divide un array de bytes en bloques
* @param rawInput los bytes
* @param tamanoBloque el tamaño de un bloque en bytes
* @param paddingAplicado si el ultimo byte de rawInput ya tiene 1000
* @return Un array de array de bytes. El primer nivel divide bloques, el segundo nivel contiene los bytes.
*/
private static byte[][] dividir(byte[] rawInput, int tamanoBloque, boolean paddingAplicado) {
int cantidadBloques = (int) Math.ceil(rawInput.length / (tamanoBloque + 1.0));

byte[][] bloques = new byte[cantidadBloques][tamanoBloque];
Expand All @@ -16,17 +26,22 @@ public static byte[][] dividirString(String input, int tamanoBloque) {
System.arraycopy(rawInput, i * tamanoBloque, bloques[i], 0, tamanoBloque);
}

int cantidadDeBits = rawInput.length * 8 - (paddingAplicado? 4 : 0);

// Aplicar padding al ultimo bloque y almacenarlo en ultimoBloque
// TODO: AL final del padding 64 bits se usan para almacenar la cantidad de bits del bloque. Arreglar
byte[] ultimoBloque = new byte[tamanoBloque];
if (rawInput.length % tamanoBloque != 0) {
int modulo = rawInput.length % tamanoBloque;
int bytesFaltantes = tamanoBloque - modulo;

System.arraycopy(rawInput, (cantidadBloques - 1) * tamanoBloque, ultimoBloque, 0, modulo);

// Ya que java no tiene unsigned byte, -128 representa 10000000
ultimoBloque[modulo] = -128;
// if (!paddingAplicado) {
// Ya que java no tiene unsigned byte, -128 representa 10000000
ultimoBloque[modulo] = -128;
// }

// TODO: Si al final quedan solo unos bytes, ¿Se crea un nuevo bloque lleno de ceros?

// El resto de bloques de padding con valor 00000000
for (int i = 0; i < bytesFaltantes - 2; i++) {
Expand All @@ -42,6 +57,27 @@ public static byte[][] dividirString(String input, int tamanoBloque) {
return bloques;
}

public static byte[][] dividirString(String input, int tamanoBloque) {
byte[] rawInput = input.getBytes(StandardCharsets.UTF_8);
return dividir(rawInput, tamanoBloque, false);
}

public static byte[][] dividirHex(String hex, int tamanoBloque) {
byte[] rawInput = new byte[(int) Math.ceil(hex.length() / 2.0)];

for (int i = 0; i < hex.length() / 2; i++) {
rawInput[i] = hexStr2AByte(hex.substring(i * 2, i * 2 + 2));
}

// Si quedó un caracter agregar '1000'
boolean tamanoEntradaImpar = hex.length() % 2 != 0;
if (tamanoEntradaImpar) {
rawInput[rawInput.length - 1] = hexStr2AByte(hex.charAt(hex.length() - 1) + "8");
}

return dividir(rawInput, tamanoBloque, tamanoEntradaImpar);
}

public static int[] dividirBloqueAPalabras32(byte[] bloque) {
int[] palabras = new int[bloque.length / 4];
for (int i = 0; i < bloque.length / 4; i++) {
Expand Down

0 comments on commit 04960c7

Please sign in to comment.