瀏覽代碼

update RSA
fix docstrings

painor 5 年之前
父節點
當前提交
89e0e2f9de
共有 2 個文件被更改,包括 20 次插入9 次删除
  1. 7 9
      crypto/Factorizator.js
  2. 13 0
      utils/Helpers.js

+ 7 - 9
crypto/Factorizator.js

@@ -1,19 +1,17 @@
+const helper = require("../utils/Helpers").helpers;
+
 class Factorizator {
-    static getRandomInt(min, max) {
-        min = Math.ceil(min);
-        max = Math.floor(max);
-        return Math.floor(Math.random() * (max - min + 1)) + min;
-    }
 
     /**
      * Finds the small multiplier by using Lopatin's method
      * @param what
+     * @return {number}
      */
     static findSmallMultiplierLopatin(what) {
         let g = 0;
         for (let i = 0; i < 3; i++) {
-            let q = 30 || (this.getRandomInt(0, 127) & 15) + 17;
-            let x = 40 || this.getRandomInt(0, 1000000000) + 1;
+            let q = 30 || (helper.getRandomInt(0, 127) & 15) + 17;
+            let x = 40 || helper.getRandomInt(0, 1000000000) + 1;
 
 
             let y = x;
@@ -62,7 +60,7 @@ class Factorizator {
      * Calculates the greatest common divisor
      * @param a
      * @param b
-     * @returns {*}
+     * @returns {number}
      */
     static gcd(a, b) {
         while (((a !== 0) && (b !== 0))) {
@@ -84,7 +82,7 @@ class Factorizator {
     /**
      * Factorizes the given number and returns both the divisor and the number divided by the divisor
      * @param pq
-     * @returns {{divisor: *, divided: *}}
+     * @returns {{divisor: number, divided: number}}
      */
     static factorize(pq) {
         let divisor = this.findSmallMultiplierLopatin(pq);

+ 13 - 0
utils/Helpers.js

@@ -112,6 +112,19 @@ class Helpers {
         return shaSum.digest();
 
     }
+
+    /**
+     * returns a random int from min (inclusive) and max (inclusive)
+     * @param min
+     * @param max
+     * @returns {number}
+     */
+    static getRandomInt(min, max) {
+        min = Math.ceil(min);
+        max = Math.floor(max);
+        return Math.floor(Math.random() * (max - min + 1)) + min;
+    }
+
 }
 
 exports.helpers = Helpers;