소스 검색

Add support for BigInt
Fix AuthKey
Remove Debug logs
Start working on interactive example

painor 5 년 전
부모
커밋
161d9d03d5
4개의 변경된 파일62개의 추가작업 그리고 41개의 파일을 삭제
  1. 19 8
      crypto/AuthKey.js
  2. 33 30
      crypto/Factorizator.js
  3. 10 0
      main.js
  4. 0 3
      utils/Helpers.js

+ 19 - 8
crypto/AuthKey.js

@@ -1,19 +1,30 @@
-const helper = require("../utils/Helpers").helpers;
+const {helpers} = require("../utils/Helpers");
 
 class AuthKey {
     constructor(data) {
         this.data = data;
-
-        let buffer = Buffer.from(helper.sha1(data));
-
-        this.auxHash = buffer.slice(0, 8).readBigUInt64LE();
-        this.keyId = buffer.slice(12, 20).readBigUInt64LE();
+        let offset = 0;
+        let buffer = helpers.sha1(data);
+        this.auxHash = buffer.readBigUInt64LE(0);
+        offset += 8 + 4;
+        this.keyId = buffer.readBigUInt64LE(8);
 
     }
 
+    /**
+     * Calculates the new nonce hash based on the current class fields' values
+     * @param new_nonce {Buffer}
+     * @param number {number}
+     * @returns {Buffer}
+     */
     calcNewNonceHash(new_nonce, number) {
-        let buffer = Buffer.concat([new_nonce, number, this.auxHash]);
-        return helper.calcMsgKey(buffer);
+
+        let tempBuffer = Buffer.alloc(1);
+        tempBuffer.writeInt8(number, 0);
+        let secondBuffer = Buffer.alloc(8);
+        secondBuffer.writeBigUInt64LE(this.auxHash, 0);
+        let buffer = Buffer.concat([new_nonce, tempBuffer, secondBuffer]);
+        return helpers.calcMsgKey(buffer);
     }
 
 }

+ 33 - 30
crypto/Factorizator.js

@@ -4,25 +4,25 @@ class Factorizator {
 
     /**
      * Finds the small multiplier by using Lopatin's method
-     * @param what
-     * @return {number}
+     * @param what {BigInt}
+     * @return {BigInt}
      */
     static findSmallMultiplierLopatin(what) {
-        let g = 0;
-        for (let i = 0; i < 3; i++) {
-            let q = 30 || (helper.getRandomInt(0, 127) & 15) + 17;
-            let x = 40 || helper.getRandomInt(0, 1000000000) + 1;
+        let g = 0n;
+        for (let i = 0n; i < 3n; i++) {
+            let q = 30n || (helper.getRandomInt(0, 127) & 15) + 17;
+            let x = 40n || helper.getRandomInt(0, 1000000000) + 1;
 
 
             let y = x;
-            let lim = 1 << (i + 18);
-            for (let j = 1; j < lim; j++) {
+            let lim = 1n << (i + 18n);
+            for (let j = 1n; j < lim; j++) {
                 let a = x;
                 let b = x;
 
                 let c = q;
-                while (b !== 0) {
-                    if ((b & 1) !== 0) {
+                while (b !== 0n) {
+                    if (BigInt(b & 1n) !== 0n) {
                         c += a;
                         if (c >= what) {
                             c -= what;
@@ -32,18 +32,19 @@ class Factorizator {
                     if (a >= what) {
                         a -= what;
                     }
-                    b >>= 1;
+                    b >>= 1n;
                 }
 
                 x = c;
-                let z = ((x < y) ? (y - x) : (x - y));
-
+                let z = BigInt((x < y) ? (y - x) : (x - y));
                 g = this.gcd(z, what);
-                if (g !== 1) {
+
+
+                if (g !== 1n) {
                     break
                 }
 
-                if ((j & (j - 1)) === 0) {
+                if ((j & (j - 1n)) === 0n) {
                     y = x;
                 }
 
@@ -52,41 +53,43 @@ class Factorizator {
                 break;
             }
         }
-        let p = Math.floor(what / g);
-        return Math.min(p, g);
+        let p = what / g;
+
+        return p < g ? p : g;
     }
 
     /**
      * Calculates the greatest common divisor
-     * @param a
-     * @param b
-     * @returns {number}
+     * @param a {BigInt}
+     * @param b {BigInt}
+     * @returns {BigInt}
      */
     static gcd(a, b) {
-        while (((a !== 0) && (b !== 0))) {
-            while (((b & 1) === 0)) {
-                b >>= 1;
+
+        while (((a !== 0n) && (b !== 0n))) {
+            while ((b & 1n) === 0n) {
+                b >>= 1n;
             }
-            while (((a & 1) === 0)) {
-                a >>= 1;
+            while ((a & 1n) === 0n) {
+                a >>= 1n;
             }
-            if ((a > b)) {
+            if (a > b) {
                 a -= b;
             } else {
                 b -= a;
             }
         }
-        return ((b === 0) ? a : b);
+        return ((b === 0n) ? a : b);
     }
 
     /**
      * Factorizes the given number and returns both the divisor and the number divided by the divisor
-     * @param pq
-     * @returns {{divisor: number, divided: number}}
+     * @param pq {BigInt}
+     * @returns {{p: BigInt, q: BigInt}}
      */
     static factorize(pq) {
         let divisor = this.findSmallMultiplierLopatin(pq);
-        return {divisor: divisor, divided: Math.floor(pq / divisor)}
+        return {p: divisor, q: pq / divisor}
     }
 }
 

+ 10 - 0
main.js

@@ -0,0 +1,10 @@
+const {Helpers} = require("./utils/Helpers");
+const {TelegramClient} = require("./tl/TelegramClient");
+
+(async function () {
+    console.log("Loading interactive example...");
+    let settings = Helpers.loadSettings();
+    let client = TelegramClient(settings["session_name"], 55, settings["api_id"], settings["api_hash"]);
+    await client.connect();
+    console.log("You should now be connected.");
+})();

+ 0 - 3
utils/Helpers.js

@@ -194,6 +194,3 @@ class Helpers {
 
 }
 
-let l = Buffer.from(0x83c95aec);
-console.log(l.length);
-