如何确定字符串是否为哈希 [英] How to determine whether a string is a hash

查看:177
本文介绍了如何确定字符串是否为哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现Java方法,该方法将确定输入字符串是哈希(由机器生成)还是纯文本(由人类编写)。

I have to implement a Java method, which will determine, if a input string is a hash (generated by a machine) or a plain-text (written by a human).

示例:

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

我考虑过使用Kolmogorov-Smirnov测试(jsc.goodnessfit.KolmogorovTest),这将检查字符串中的字符是否来自正态分布,但据我了解,仅检查一个短字符串可能不是结论性的。

I thought about using the Kolmogorov-Smirnov test (jsc.goodnessfit.KolmogorovTest), which will check, if a characters in a string are from normal distribution, but I have learned, that checking only one, short string might not be conclusive.

知道如何解决Java中的这个问题(最好使用现有的库)吗?

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

推荐答案

您已经声明只想要一个近似解决方案(准确度为80%),可能是AClassName形式的类(注意大小写),并且给定的加密文本样本中没有大写字母。因此

You have stated that you only want an approximate solution (80% accuracy), that classes of the form AClassName are likely (note capitalisation) and the given sample of encrypted text has no capitals 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;
    }
}

这是一个好的解决方案吗?不,它是否提供> 80%的准确性;是

Is this a good solution; no, does it give >80% accuracy; yes

这篇关于如何确定字符串是否为哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆