Ejemplo n.º 1
0
    private bool CheckHash(string input, bool showDialog = false, bool showDialogWhenFailed = true)
    {
        UdonHashLib inspectorBehaviour = (UdonHashLib)target;

        string md5_udon    = inspectorBehaviour.MD5_UTF8(input);
        string sha1_udon   = inspectorBehaviour.SHA1_UTF8(input);
        string sha224_udon = inspectorBehaviour.SHA224_UTF8(input);
        string sha256_udon = inspectorBehaviour.SHA256_UTF8(input);
        string sha384_udon = inspectorBehaviour.SHA384_UTF8(input);
        string sha512_udon = inspectorBehaviour.SHA512_UTF8(input);

        string md5_csharp    = GenerateNonUdon(input, System.Security.Cryptography.MD5.Create());
        string sha1_csharp   = GenerateNonUdon(input, System.Security.Cryptography.SHA1.Create());
        string sha256_csharp = GenerateNonUdon(input, System.Security.Cryptography.SHA256.Create());
        string sha384_csharp = GenerateNonUdon(input, System.Security.Cryptography.SHA384.Create());
        string sha512_csharp = GenerateNonUdon(input, System.Security.Cryptography.SHA512.Create());

        bool allPass = (
            md5_udon == md5_csharp &&
            sha1_udon == sha1_csharp &&
            sha256_udon == sha256_csharp &&
            sha384_udon == sha384_csharp &&
            sha512_udon == sha512_csharp
            );

        if (showDialog || (!allPass && showDialogWhenFailed))
        {
            EditorUtility.DisplayDialog("Hashes", string.Join("\n", new string[] {
                input,
                "",
                $"md5 [{(md5_udon == md5_csharp ? "PASS" : "FAIL")}]: {md5_udon}",
                $"sha1 [{(sha1_udon == sha1_csharp ? "PASS" : "FAIL")}]: {sha1_udon}",
                $"sha224: {sha224_udon}", // No system impl for this
                $"sha256 [{(sha256_udon == sha256_csharp ? "PASS" : "FAIL")}]: {sha256_udon}",
                $"sha384 [{(sha384_udon == sha384_csharp ? "PASS" : "FAIL")}]: {sha384_udon}",
                $"sha512 [{(sha512_udon == sha512_csharp ? "PASS" : "FAIL")}]: {sha512_udon}",
            }), "OK");
        }

        return(allPass);
    }
Ejemplo n.º 2
0
    public override void OnInspectorGUI()
    {
        // Draws the default convert to UdonBehaviour button, program asset field, sync settings, etc.
        if (UdonSharpGUI.DrawDefaultUdonSharpBehaviourHeader(target))
        {
            return;
        }

        UdonHashLib inspectorBehaviour = (UdonHashLib)target;

        if (!EditorApplication.isPlaying)
        {
            EditorGUILayout.HelpBox("Enter play mode to run tests", MessageType.Info);
        }

        EditorGUI.BeginDisabledGroup(!EditorApplication.isPlaying);

        // A simple string field modification with Undo handling
        EditorGUILayout.LabelField("Input for hash");
        inputString = EditorGUILayout.TextArea(inputString);

        if (GUILayout.Button("Calculate hashes"))
        {
            CheckHash(inputString, true);
        }

        if (GUILayout.Button("Run test suite"))
        {
            int testBatch = 4096;
            int testCount = 0, succeeded = 0;

            System.Random random = new System.Random();
            string        chars  = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\n";
            string        buffer = new string(Enumerable.Repeat(chars, testBatch).Select(s => s[random.Next(s.Length)]).ToArray());

            Debug.Log($"Buffer generated for ASCII testing: {buffer}");

            for (int i = 0; i < testBatch; i++)
            {
                string test = buffer.Substring(random.Next(buffer.Length - i), i);

                testCount++;
                if (CheckHash(test))
                {
                    succeeded++;
                }
            }

            // Japanese character (UTF-8) test
            chars  = "\u3042\u3044\u3046\u3048\u304a\u304b\u304d\u304f\u3051\u3053\u3055\u3057\u3059\u305b\u305d\u305f\u3061\u3064\u3066\u3068\u306a\u306b\u306c\u306d\u306e\u306f\u3072\u3075\u3078\u307b\u307e\u307f\u3080\u3081\u3082\u3084\u3086\u3088\u3089\u308a\u308b\u308c\u308d\u308f\u3090\u3091\u3092\u3093\u30a2\u30a4\u30a6\u30a8\u30aa\u30ab\u30ad\u30af\u30b1\u30b3\u30b5\u30b7\u30b9\u30bb\u30bd\u30bf\u30c1\u30c4\u30c6\u30c8\u30ca\u30cb\u30cc\u30cd\u30ce\u30cf\u30d2\u30d5\u30d8\u30db\u30de\u30df\u30e0\u30e1\u30e2\u30e4\u30e6\u30e8\u30e9\u30ea\u30eb\u30ec\u30ed\u30ef\u30f0\u30f1\u30f2\u30f3";
            buffer = new string(Enumerable.Repeat(chars, testBatch).Select(s => s[random.Next(s.Length)]).ToArray());

            Debug.Log($"Buffer generated for JPN/UTF-8 testing: {buffer}");

            for (int i = 0; i < testBatch; i++)
            {
                string test = buffer.Substring(random.Next(buffer.Length - i), i);

                testCount++;
                if (CheckHash(test))
                {
                    succeeded++;
                }
            }

            EditorUtility.DisplayDialog("Results", $"{testCount} tests ran, {succeeded} succeeded in all hashes", "OK");
        }

        EditorGUI.EndDisabledGroup();
    }