[英]How to determine whether a string is a hash
我必須實現一個Java方法,它將確定輸入字符串是哈希(由機器生成)還是純文本(由人類編寫)。
例:
isThisEncrypted("qwertyuiopasdfghjklzxcvbnm"); // returns true
isThisEncrypted("some normal human text"); // returns false
我想過使用Kolmogorov-Smirnov測試(jsc.goodnessfit.KolmogorovTest),它將檢查字符串中的字符是否來自正態分布,但我已經知道,只檢查一個短字符串可能不是決定性的。
您是否知道如何在Java中解決此問題(最好使用現有的庫)?
從你的評論:
人的輸入可以是隨機的
如果字符串來自此方法或表單用戶,則此方法必須確定
那么只有字符串才能解決你的問題。 您需要額外的信息。
如果你期望Blowfish以給定的格式返回一個String,那你就錯了。 現代加密算法以高熵為目標,這意味着它們必須具有隨機的外觀和感覺。
您將輸入拆分為單詞並根據字典檢查它們( 檢查字典中的單詞 )。
從現在開始,一切都取決於您的實施。 IMO,如果一半的單詞與字典匹配,那么你的方法應該返回false。
您已聲明您只需要一個近似解(80%准確度),AClassName形式的類可能(注意大小寫),並且加密文本的給定樣本中沒有大寫字母。 所以
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;
}
}
這是一個很好的解決方案; 不,它是否提供> 80%的准確度; 是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.