소스 검색

Start utils functions
Start crypto functions

painor 5 년 전
부모
커밋
d462c97d75
8개의 변경된 파일132개의 추가작업 그리고 0개의 파일을 삭제
  1. 4 0
      api/settings
  2. 27 0
      crypto/AES.js
  3. 3 0
      crypto/AuthKey.js
  4. 3 0
      crypto/Factorizator.js
  5. 6 0
      crypto/RSA.js
  6. 3 0
      utils/BinaryReader.js
  7. 3 0
      utils/BinaryWriter.js
  8. 83 0
      utils/Helpers.js

+ 4 - 0
api/settings

@@ -0,0 +1,4 @@
+api_id=12345
+api_hash=0123456789abcdef0123456789abcdef
+user_phone=+34600000000
+session_name=anonymous

+ 27 - 0
crypto/AES.js

@@ -0,0 +1,27 @@
+class AES {
+ decryptIge(){/*
+     var aes, blocks_count, cipher_text_block, iv1, iv2, plain_text, plain_text_block;
+     iv1 = iv.slice(0, (Math.floor(iv.length / 2)));
+     iv2 = iv.slice((Math.floor(iv.length / 2)));
+     aes = new pyaes.AES(key);
+     plain_text = ([0] * cipher_text.length);
+     blocks_count = (Math.floor(cipher_text.length / 16));
+     cipher_text_block = ([0] * 16);
+     for (var block_index = 0, _pj_a = blocks_count; (block_index < _pj_a); block_index += 1) {
+         for (var i = 0, _pj_b = 16; (i < _pj_b); i += 1) {
+             cipher_text_block[i] = (cipher_text[((block_index * 16) + i)] ^ iv2[i]);
+         }
+         plain_text_block = aes.decrypt(cipher_text_block);
+         for (var i = 0, _pj_b = 16; (i < _pj_b); i += 1) {
+             plain_text_block[i] ^= iv1[i];
+         }
+         iv1 = cipher_text.slice((block_index * 16), ((block_index * 16) + 16));
+         iv2 = plain_text_block.slice(0, 16);
+         plain_text.slice((block_index * 16), ((block_index * 16) + 16)) = plain_text_block.slice(0, 16);
+     }
+     return bytes(plain_text);
+*/
+
+
+ }
+}

+ 3 - 0
crypto/AuthKey.js

@@ -0,0 +1,3 @@
+class AuthKey {
+    
+}

+ 3 - 0
crypto/Factorizator.js

@@ -0,0 +1,3 @@
+class Factorizator {
+    
+}

+ 6 - 0
crypto/RSA.js

@@ -0,0 +1,6 @@
+class RSA {
+    
+}
+class RSAServerKey {
+    
+}

+ 3 - 0
utils/BinaryReader.js

@@ -0,0 +1,3 @@
+class BinaryReader {
+
+}

+ 3 - 0
utils/BinaryWriter.js

@@ -0,0 +1,3 @@
+class BinaryWriter {
+
+}

+ 83 - 0
utils/Helpers.js

@@ -0,0 +1,83 @@
+/*Generates a random long integer (8 bytes), which is optionally signed*/
+function generateRandomLong() {
+    let buf = Buffer.from(generateRandomBytes(8),); // 0x12345678 = 305419896
+    return (buf.readUInt32BE(0));
+}
+
+/*Generates a random bytes array*/
+function generateRandomBytes(count) {
+
+    const crypto = require('crypto');
+
+    return crypto.randomBytes(count);
+
+}
+
+function loadSettings(path = "../api/settings") {
+    let settings = {};
+    let left, right, value_pair;
+    const fs = require("fs");
+
+    let data = fs.readFileSync(path, 'utf-8');
+
+
+    for (let line of data.toString().split('\n')) {
+        value_pair = line.split("=");
+        if (value_pair.length !== 2) {
+            break;
+        }
+        left = value_pair[0].replace(/ \r?\n|\r/g, '');
+        right = value_pair[1].replace(/ \r?\n|\r/g, '');
+        if (!isNaN(right)) {
+            settings[left] = Number.parseInt(right);
+        } else {
+            settings[left] = right;
+        }
+    }
+
+
+    return settings;
+
+
+
+}
+
+/*Calculate the key based on Telegram guidelines, specifying whether it's the client or not*/
+
+function calcKey(shared_key, msg_key, client) {
+   let x = client !== null ? 0 : 8;
+    let iv, key, sha1a, sha1b, sha1c, sha1d;
+    sha1a = sha1((msg_key + shared_key.slice(x, (x + 32))));
+    sha1b = sha1(((shared_key.slice((x + 32), (x + 48)) + msg_key) + shared_key.slice((x + 48), (x + 64))));
+    sha1c = sha1((shared_key.slice((x + 64), (x + 96)) + msg_key));
+    sha1d = sha1((msg_key + shared_key.slice((x + 96), (x + 128))));
+    key = ((sha1a.slice(0, 8) + sha1b.slice(8, 20)) + sha1c.slice(4, 16));
+    iv = (((sha1a.slice(8, 20) + sha1b.slice(0, 8)) + sha1c.slice(16, 20)) + sha1d.slice(0, 8));
+    return [key, iv];
+
+
+}
+
+function calcMsgKey(data) {
+    return sha1(data).slice(4, 20);
+
+
+}
+
+function generateKeyDataFromNonces() {
+    let hash1, hash2, hash3;
+    /*hash1 = sha1(bytes((new_nonce + server_nonce)));
+    hash2 = sha1(bytes((server_nonce + new_nonce)));
+    hash3 = sha1(bytes((new_nonce + new_nonce)));
+*/
+
+}
+/*Calculates the SHA1 digest for the given data*/
+function sha1(data) {
+    const crypto = require('crypto')
+        , shasum = crypto.createHash('sha1');
+    shasum.update(data);
+    console.log(shasum.digest());
+
+}
+sha1("test");