Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Random;
- public class Hash {
- public static void main(String[] args){
- String test = transactionHashGenerator(2, 8, 456.32);
- System.out.println(test); //115G8RSldAs7EFSr50lvv@nophml
- //11 - ключ
- //[7] l - ид первого
- //[15] r - ид второго
- // @ - divider
- // nophml - значение транзакции
- System.out.println(bitNumber(2));
- System.out.println(bitNumber(8));
- System.out.println(bitNumber(456.3));
- System.out.println(makeSmallHash(456.3));
- System.out.println(makeSmallHash(456.30));
- System.out.println(makeSmallHash(456.32));
- System.out.println(String.valueOf(decryptor(test)));
- }
- public static String transactionHashGenerator(int idHost, int idDestination, double value){
- // TODO: принимает id отправителя и получателя, расширяет хэш на нужное количество разрядов, переводит каждый разряд id'шников в знак и вставляет на определённую позицию в хэше
- int symbolCount = bitNumber(idHost) + bitNumber(idDestination) + 20;
- char[] hash = new char[symbolCount];
- char[] valueHash = makeSmallHash(value);
- //final Random rand = new Random();
- char[] host = makeSmallHash(idHost); //
- char[] destination = makeSmallHash(idDestination);
- final Random rand = new Random();
- //String str = "";
- hash[0] = (char) (bitNumber(idHost) + 48);
- hash[1] = (char) (bitNumber(idDestination) + 48);
- if (hash[0] == '1' && hash[1] == '1'){
- for (int i = 2; i < 7; i++){
- int ranNum = rand.nextInt(3);
- if(ranNum == 0){
- hash[i] = (char) randInt(65, 90);
- }
- else if(ranNum == 1){
- hash[i] = (char) randInt(97, 122);
- }
- else{
- hash[i] = (char) randInt(48, 57);
- }
- }
- hash[7] = host[0];
- for (int i = 8; i < 15; i++){
- int ranNum = rand.nextInt(3);
- if(ranNum == 0){
- hash[i] = (char) randInt(65, 90);
- }
- else if(ranNum == 1){
- hash[i] = (char) randInt(97, 122);
- }
- else{
- hash[i] = (char) randInt(48, 57);
- }
- }
- hash[15] = destination[0];
- for (int i = 16; i < symbolCount - 1; i++){
- int ranNum = rand.nextInt(3);
- if(ranNum == 0){
- hash[i] = (char) randInt(65, 90);
- }
- else if(ranNum == 1){
- hash[i] = (char) randInt(97, 122);
- }
- else{
- hash[i] = (char) randInt(48, 57);
- }
- }
- hash[symbolCount - 1] = '@';
- return new String(hash) + new String(valueHash);
- }
- else {
- return "lol";
- }
- /*
- str += hash[0];
- str += hash[1];
- if (hash[0] == '1' && hash[1] == '1'){
- str += setHashChars(2, 7, hash);
- hash[7] = host[0];
- str += hash[7];
- str += setHashChars(8, 15, hash);
- hash[15] = destination[0];
- str += hash[15];
- str += setHashChars(16, symbolCount, hash);
- return str;
- } else if(hash[0] == '2' && hash[1] == '2'){
- str += setHashChars(2, 4, hash);
- hash[4] = host[0];
- hash[5] = host[1];
- str += hash[4];
- str += hash[5];
- str += setHashChars(6, 18, hash);
- hash[18] = destination[0];
- hash[19] = destination[1];
- str += hash[18];
- str += hash[19];
- str += setHashChars(20, symbolCount, hash);
- return str;
- } else {
- return "pistrun";
- }
- */
- }
- public static char[] decryptor(String str){
- char[] arr = str.toCharArray();
- char[] arr2 = new char[str.length()];
- int[] preResult = new int[str.length()];
- for(int i = str.indexOf('@') + 1; i<str.length(); i++){
- preResult[i] = arr[i] - 58;
- arr2[i] = (char) preResult[i];
- }
- if(arr[0] == '1' && arr[1] == '1'){
- preResult[0] = arr[7] - 58;
- preResult[1] = arr[15] - 58;
- arr2[0] = (char) preResult[0];
- arr2[1] = (char) preResult[1];
- return arr2;
- }
- else{
- arr2[0] = 25200;
- arr2[1] = 25200;
- return arr2;
- }
- }
- public static String setHashChars(int start, int end, char[] hash){
- final Random rand = new Random();
- for (int i = start; i < end; i++){
- int ranNum = rand.nextInt(3);
- if(ranNum == 0){
- hash[i] = (char) randInt(65, 90);
- }
- else if(ranNum == 1){
- hash[i] = (char) randInt(97, 122);
- }
- else{
- hash[i] = (char) randInt(48, 57);
- }
- }
- return new String(hash);
- }
- public static int randInt(int min, int max) {
- Random r = new Random();
- return r.nextInt(max - min) + min;
- }
- public static int bitNumber(int n){
- return (int) Math.log10(n) + 1;
- }
- public static int bitNumber(double n){
- return (int) Math.log10(n) + 1;
- }
- public static char[] makeSmallHash(double n){
- String temp = Double.toString(n);
- int[] newGuess = new int[temp.length()];
- for (int i = 0; i < temp.length(); i++)
- {
- newGuess[i] = temp.charAt(i) + 58;
- }
- char[] hash = new char[newGuess.length];
- for(int i = 0; i<newGuess.length; i++){
- hash[i] = (char) newGuess[i];
- }
- return hash;
- }
- public static char[] makeSmallHash(int n){
- String temp = Integer.toString(n);
- int[] newGuess = new int[temp.length()];
- for (int i = 0; i < temp.length(); i++)
- {
- newGuess[i] = temp.charAt(i) + 58;
- }
- char[] hash = new char[newGuess.length];
- for(int i = 0; i<newGuess.length; i++){
- hash[i] = (char) newGuess[i];
- }
- return hash;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement