Denis_Hristov

SHA256

Jun 23rd, 2021 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class SHA256 {
  4.     public static char[] CurrentBinaryString;
  5.     public static char[] PreviousBinaryString;
  6.     public static char[] nameToCharArr;
  7.     public static int currentDEC;
  8.     public static char currentASCII;
  9.  
  10.  
  11.     public static void binToDec() {
  12.         String currBIN = String.valueOf(CurrentBinaryString);
  13.  
  14.         currentDEC = Integer.parseInt(currBIN, 2);
  15.         if (currentDEC < 33 || currentDEC > 126) {
  16.             Random random = new Random();
  17.             int min = 33;
  18.             int max = 127;
  19.             currentDEC = random.nextInt(max - min + 1) + min;
  20.         }
  21.         System.out.println(currentDEC);
  22.     }
  23.  
  24.     public static void decToASCII() {
  25.         currentASCII = (char) currentDEC;
  26.     }
  27.  
  28.     public static void ASCIIToNameCharArr(int index) {
  29.         nameToCharArr[index] = currentASCII;
  30.     }
  31.  
  32.     public static void XOR() {
  33.         for (int i = 0; i < CurrentBinaryString.length; i++) {
  34.             if (CurrentBinaryString[i] == PreviousBinaryString[i]) {
  35.                 CurrentBinaryString[i] = '0';
  36.             } else {
  37.                 CurrentBinaryString[i] = '1';
  38.             }
  39.         }
  40.     }
  41.  
  42.  
  43.     public static void RORandSHR(boolean rorOrshr, int rotations) {
  44.         if (!rorOrshr) {
  45.  
  46.             for (int i = rotations; i > 0; i--) {
  47.                 char last = CurrentBinaryString[CurrentBinaryString.length - 1];
  48.                 for (int j = CurrentBinaryString.length - 1; j > 0; j--) {
  49.                     CurrentBinaryString[j] = CurrentBinaryString[j - 1];
  50.                 }
  51.                 CurrentBinaryString[0] = last;
  52.             }
  53.         } else {
  54.             for (int i = rotations; i > 0; i--) {
  55.                 for (int j = CurrentBinaryString.length - 1; j > 0; j--) {
  56.                     CurrentBinaryString[j] = CurrentBinaryString[j - 1];
  57.                 }
  58.                 CurrentBinaryString[0] = '0';
  59.             }
  60.  
  61.         }
  62.     }
  63.  
  64.     public static void addBytes() {
  65.         char ele = '0';
  66.         int index = 0;
  67.         char[] tempArr = new char[CurrentBinaryString.length + 1];
  68.         int j = 0;
  69.         for (int i = 0; i < tempArr.length; i++) {
  70.             if (i == index) {
  71.                 tempArr[i] = ele;
  72.             } else {
  73.                 tempArr[i] = CurrentBinaryString[j];
  74.                 j++;
  75.             }
  76.         }
  77.         CurrentBinaryString = tempArr;
  78.     }
  79.  
  80.  
  81.     public static void main(String[] args) {
  82.         Scanner scan = new Scanner(System.in);
  83.         System.out.println("Input string: ");
  84.         String Name = scan.nextLine();
  85.         nameToCharArr = Name.toCharArray();
  86.         int arrLength = nameToCharArr.length;
  87.         int index = 0;
  88.         for (int i = 0; i < arrLength; i++) {
  89.             int ascII = nameToCharArr[i];
  90.             PreviousBinaryString = new char[arrLength];
  91.             CurrentBinaryString = new char[arrLength];
  92.             CurrentBinaryString = Integer.toBinaryString(ascII).toCharArray();
  93.             PreviousBinaryString = Arrays.copyOf(CurrentBinaryString, arrLength);
  94.             if (CurrentBinaryString.length < 8) {
  95.                 while (CurrentBinaryString.length < 8) {
  96.                     addBytes();
  97.                 }
  98.             }
  99.             int rotations = 13;
  100.             PreviousBinaryString = Arrays.copyOf(CurrentBinaryString, CurrentBinaryString.length);
  101.             RORandSHR(false, rotations);
  102.             XOR();
  103.             PreviousBinaryString = Arrays.copyOf(CurrentBinaryString, CurrentBinaryString.length);
  104.             rotations = 7;
  105.             RORandSHR(false, rotations);
  106.             XOR();
  107.             int shifts = 3;
  108.             RORandSHR(true, shifts);
  109.             binToDec();
  110.             decToASCII();
  111.             ASCIIToNameCharArr(index);
  112.             index++;
  113.             System.out.println(Arrays.toString(CurrentBinaryString));
  114.             System.out.println(currentASCII);
  115.  
  116.  
  117.         }
  118.         System.out.println(Arrays.toString(nameToCharArr));
  119.     }
  120. }
Add Comment
Please, Sign In to add comment