Advertisement
dartwlad

Untitled

Jan 5th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.64 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class Hash {
  5.  
  6.  
  7.     public static void main(String[] args){
  8.         String test = transactionHashGenerator(2, 8, 456.32);
  9.  
  10.         System.out.println(test); //115G8RSldAs7EFSr50lvv@nophml
  11.                                 //11 - ключ
  12.                                 //[7] l - ид первого
  13.                                 //[15] r - ид второго
  14.                                 // @ - divider
  15.                                 // nophml - значение транзакции
  16.         System.out.println(bitNumber(2));
  17.         System.out.println(bitNumber(8));
  18.         System.out.println(bitNumber(456.3));
  19.         System.out.println(makeSmallHash(456.3));
  20.         System.out.println(makeSmallHash(456.30));
  21.         System.out.println(makeSmallHash(456.32));
  22.         System.out.println(String.valueOf(decryptor(test)));
  23.     }
  24.  
  25.     public static String transactionHashGenerator(int idHost, int idDestination, double value){
  26.         // TODO: принимает id отправителя и получателя, расширяет хэш на нужное количество разрядов, переводит каждый разряд id'шников в знак и вставляет на определённую позицию в хэше
  27.         int symbolCount = bitNumber(idHost) + bitNumber(idDestination) + 20;
  28.         char[] hash = new char[symbolCount];
  29.         char[] valueHash = makeSmallHash(value);
  30.         //final Random rand = new Random();
  31.         char[] host = makeSmallHash(idHost); //
  32.         char[] destination = makeSmallHash(idDestination);
  33.         final Random rand = new Random();
  34.         //String str = "";
  35.  
  36.         hash[0] = (char) (bitNumber(idHost) + 48);
  37.         hash[1] = (char) (bitNumber(idDestination) + 48);
  38.         if (hash[0] == '1' && hash[1] == '1'){
  39.  
  40.             for (int i = 2; i < 7; i++){
  41.                 int ranNum = rand.nextInt(3);
  42.                 if(ranNum == 0){
  43.                     hash[i] = (char) randInt(65, 90);
  44.                 }
  45.                 else if(ranNum == 1){
  46.                     hash[i] = (char) randInt(97, 122);
  47.                 }
  48.                 else{
  49.                     hash[i] = (char) randInt(48, 57);
  50.                 }
  51.             }
  52.             hash[7] = host[0];
  53.             for (int i = 8; i < 15; i++){
  54.                 int ranNum = rand.nextInt(3);
  55.                 if(ranNum == 0){
  56.                     hash[i] = (char) randInt(65, 90);
  57.                 }
  58.                 else if(ranNum == 1){
  59.                     hash[i] = (char) randInt(97, 122);
  60.                 }
  61.                 else{
  62.                     hash[i] = (char) randInt(48, 57);
  63.                 }
  64.             }
  65.             hash[15] = destination[0];
  66.             for (int i = 16; i < symbolCount - 1; i++){
  67.                 int ranNum = rand.nextInt(3);
  68.                 if(ranNum == 0){
  69.                     hash[i] = (char) randInt(65, 90);
  70.                 }
  71.                 else if(ranNum == 1){
  72.                     hash[i] = (char) randInt(97, 122);
  73.                 }
  74.                 else{
  75.                     hash[i] = (char) randInt(48, 57);
  76.                 }
  77.             }
  78.             hash[symbolCount - 1] = '@';
  79.  
  80.             return new String(hash) + new String(valueHash);
  81.         }
  82.         else {
  83.             return "lol";
  84.         }
  85.  
  86.  
  87.         /*
  88.         str += hash[0];
  89.         str += hash[1];
  90.         if (hash[0] == '1' && hash[1] == '1'){
  91.             str += setHashChars(2, 7, hash);
  92.             hash[7] = host[0];
  93.             str += hash[7];
  94.             str += setHashChars(8, 15, hash);
  95.             hash[15] = destination[0];
  96.             str += hash[15];
  97.             str += setHashChars(16, symbolCount, hash);
  98.  
  99.             return str;
  100.  
  101.         } else if(hash[0] == '2' && hash[1] == '2'){
  102.             str += setHashChars(2, 4, hash);
  103.             hash[4] = host[0];
  104.             hash[5] = host[1];
  105.             str += hash[4];
  106.             str += hash[5];
  107.             str += setHashChars(6, 18, hash);
  108.             hash[18] = destination[0];
  109.             hash[19] = destination[1];
  110.             str += hash[18];
  111.             str += hash[19];
  112.             str += setHashChars(20, symbolCount, hash);
  113.  
  114.             return str;
  115.         } else {
  116.             return "pistrun";
  117.         }
  118.         */
  119.     }
  120.  
  121.     public static char[] decryptor(String str){
  122.         char[] arr = str.toCharArray();
  123.         char[] arr2 = new char[str.length()];
  124.         int[] preResult = new int[str.length()];
  125.         for(int i = str.indexOf('@') + 1; i<str.length(); i++){
  126.             preResult[i] = arr[i] - 58;
  127.             arr2[i] = (char) preResult[i];
  128.         }
  129.         if(arr[0] == '1' && arr[1] == '1'){
  130.             preResult[0] = arr[7] - 58;
  131.             preResult[1] = arr[15] - 58;
  132.             arr2[0] = (char) preResult[0];
  133.             arr2[1] = (char) preResult[1];
  134.             return arr2;
  135.         }
  136.         else{
  137.             arr2[0] = 25200;
  138.             arr2[1] = 25200;
  139.  
  140.             return arr2;
  141.         }
  142.     }
  143.  
  144.     public static String setHashChars(int start, int end, char[] hash){
  145.         final Random rand = new Random();
  146.         for (int i = start; i < end; i++){
  147.             int ranNum = rand.nextInt(3);
  148.             if(ranNum == 0){
  149.                 hash[i] = (char) randInt(65, 90);
  150.             }
  151.             else if(ranNum == 1){
  152.                 hash[i] = (char) randInt(97, 122);
  153.             }
  154.             else{
  155.                 hash[i] = (char) randInt(48, 57);
  156.             }
  157.         }
  158.         return new String(hash);
  159.     }
  160.  
  161.     public static int randInt(int min, int max) {
  162.         Random r = new Random();
  163.         return r.nextInt(max - min) + min;
  164.     }
  165.  
  166.     public static int bitNumber(int n){
  167.         return (int) Math.log10(n) + 1;
  168.     }
  169.  
  170.     public static int bitNumber(double n){
  171.         return (int) Math.log10(n) + 1;
  172.     }
  173.  
  174.     public static char[] makeSmallHash(double n){
  175.         String temp = Double.toString(n);
  176.  
  177.         int[] newGuess = new int[temp.length()];
  178.         for (int i = 0; i < temp.length(); i++)
  179.         {
  180.             newGuess[i] = temp.charAt(i) + 58;
  181.         }
  182.         char[] hash = new char[newGuess.length];
  183.         for(int i = 0; i<newGuess.length; i++){
  184.             hash[i] = (char) newGuess[i];
  185.         }
  186.  
  187.         return hash;
  188.     }
  189.  
  190.     public static char[] makeSmallHash(int n){
  191.         String temp = Integer.toString(n);
  192.  
  193.         int[] newGuess = new int[temp.length()];
  194.         for (int i = 0; i < temp.length(); i++)
  195.         {
  196.             newGuess[i] = temp.charAt(i) + 58;
  197.         }
  198.         char[] hash = new char[newGuess.length];
  199.         for(int i = 0; i<newGuess.length; i++){
  200.             hash[i] = (char) newGuess[i];
  201.         }
  202.  
  203.         return hash;
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement