How to determine if a string is a hash

I need to implement a Java method that determines whether the input string is a hash (generated by the machine) or plain text (written by a person).

Example:

isThisEncrypted("qwertyuiopasdfghjklzxcvbnm"); // returns true isThisEncrypted("some normal human text"); // returns false 

I thought about using the Kolmogorov-Smirnov test (jsc.goodnessfit.KolmogorovTest), which will check if the characters in the string are a normal distribution, but I found out that checking only one short string may not be convincing.

Do you have an idea to solve this problem in Java (preferably using an existing library)?

+4
source share
3 answers

You stated that you want only an approximate solution (80% accuracy), that the AClassName form classes are probably (capital letter), and this sample of encrypted text does not have capital in it. So

 public class Test{ public static void main(String args[]){ String[] tests=new String[5]; tests[0]="MyClass"; tests[1]="Short"; tests[2]="thsrjtyzfgnmytkzrhjstk"; tests[3]="tatm"; tests[4]="The result is good"; for(int i=0;i<tests.length;i++){ System.out.println(tests[i]+ "- Encrypted:" + isProbablyEncrypted(tests[i])); } } public static boolean isProbablyEncrypted(String in){ int noOfWords= countOccurrences(in, ' ') + countCaps(in); if (noOfWords==0){ return true; }else{ double averageWordLength=(double)(in.length())/(noOfWords+1); if (averageWordLength>15){ return true; }else{ return false; } } } public static int countOccurrences(String haystack, char needle) { int count = 0; for (int i=0; i < haystack.length(); i++) { if (haystack.charAt(i) == needle) { count++; } } return count; } public static int countCaps(String in){ int caps=0; for (int i=0; i<in.length(); i++) { if (Character.isUpperCase(in.charAt(i)))caps++; } return caps; } } 

This is a good decision; no, does accuracy give> 80%; Yes

+1
source

From your comments:

Entering a person may be random

this method should determine if the string comes from this method or form User

Then there is no way to solve your problem with only a string. You need more information.

If you expect Blowfish to return a String in the given format, you are mistaken. Modern encryption algorithms are aimed at high entropy, which means that they must look and feel random.

+3
source

You break your input into words and check them for a dictionary ( checking words in a dictionary ).

From now on, it all depends on your implementation. IMO, if half the words match the dictionary, then your method should return false.

0
source

Source: https://habr.com/ru/post/1479713/


All Articles