ソースを参照

rc4 optimization

sanex3339 8 年 前
コミット
1334b31d89
2 ファイル変更11 行追加5 行削除
  1. 1 1
      dist/index.js
  2. 10 4
      src/templates/custom-nodes/Rc4Template.ts

+ 1 - 1
dist/index.js

@@ -3897,7 +3897,7 @@ exports.AtobTemplate = AtobTemplate;
 "use strict";
 "use strict";
 
 
 function Rc4Template() {
 function Rc4Template() {
-    return "\n        function rc4 (str, key) {\n\t        var s = [], j = 0, x, res = '';\n\t        \n            for (var i = 0; i < 256; i++) {\n                s[i] = i;\n            }\n            \n            for (i = 0; i < 256; i++) {\n                j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;\n                x = s[i];\n                s[i] = s[j];\n                s[j] = x;\n            }\n            \n            i = 0;\n            j = 0;\n            \n            for (var y = 0; y < str.length; y++) {\n                i = (i + 1) % 256;\n                j = (j + s[i]) % 256;\n                x = s[i];\n                s[i] = s[j];\n                s[j] = x;\n                res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);\n            }\n            \n            return res;\n        }\n    ";
+    return "\n        var rc4 = function (str, key) {\n\t        var s = [], j = 0, x, res = '';\n\t        \n\t        if (!rc4.s) {\n                for (var i = 0; i < 256; i++) {\n                    s[i] = i;\n                }\n                \n                rc4.s = s;\n\t        } else {\n\t            s = rc4.s.slice(0);\n\t        }\n            \n            for (i = 0; i < 256; i++) {\n                j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;\n                x = s[i];\n                s[i] = s[j];\n                s[j] = x;\n            }\n            \n            i = 0;\n            j = 0;\n            \n            for (var y = 0; y < str.length; y++) {\n                i = (i + 1) % 256;\n                j = (j + s[i]) % 256;\n                x = s[i];\n                s[i] = s[j];\n                s[j] = x;\n                res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);\n            }\n            \n            return res;\n        }\n    ";
 }
 }
 exports.Rc4Template = Rc4Template;
 exports.Rc4Template = Rc4Template;
 
 

+ 10 - 4
src/templates/custom-nodes/Rc4Template.ts

@@ -3,12 +3,18 @@
  */
  */
 export function Rc4Template (): string {
 export function Rc4Template (): string {
     return `
     return `
-        function rc4 (str, key) {
+        var rc4 = function (str, key) {
 	        var s = [], j = 0, x, res = '';
 	        var s = [], j = 0, x, res = '';
 	        
 	        
-            for (var i = 0; i < 256; i++) {
-                s[i] = i;
-            }
+	        if (!rc4.s) {
+                for (var i = 0; i < 256; i++) {
+                    s[i] = i;
+                }
+                
+                rc4.s = s;
+	        } else {
+	            s = rc4.s.slice(0);
+	        }
             
             
             for (i = 0; i < 256; i++) {
             for (i = 0; i < 256; i++) {
                 j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
                 j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;