k9crypt 1.1.5 → 1.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,9 +6,10 @@ This is a special encryption algorithm created for K9Crypt.
6
6
 
7
7
  ## Updates
8
8
 
9
- **v1.1.5**
9
+ **v1.1.7**
10
10
 
11
- - HOT FIX: Fixed an issue where installing the module caused a "@types/k9crypt" error.
11
+ - The Argon2 hashing system has now been integrated, offering support for both SHA512 and Argon2.
12
+ - Encryption performance has been optimized, significantly increasing speed.
12
13
 
13
14
  ## Installation
14
15
 
@@ -21,7 +22,6 @@ bun add k9crypt
21
22
 
22
23
  ## Usage
23
24
 
24
- Usage in CommonJS:
25
25
  ```javascript
26
26
  const k9crypt = require('k9crypt');
27
27
 
@@ -46,31 +46,6 @@ async function test() {
46
46
  test();
47
47
  ```
48
48
 
49
- Usage in ES Modules:
50
- ```javascript
51
- import k9crypt from 'k9crypt';
52
-
53
- async function test() {
54
- const secretKey = 'VeryLongSecretKey!@#1234567890';
55
- const encryptor = new k9crypt(secretKey);
56
- // Or you can use it without providing a secretKey value. A key will be generated by the system.
57
- // const encryptor = new k9crypt();
58
- const plaintext = 'Hello, World!';
59
-
60
- try {
61
- const encrypted = await encryptor.encrypt(plaintext);
62
- console.log('Encrypted data:', encrypted);
63
-
64
- const decrypted = await encryptor.decrypt(encrypted);
65
- console.log('Decrypted data:', decrypted);
66
- } catch (error) {
67
- console.error('Encryption error:', error);
68
- }
69
- }
70
-
71
- test();
72
- ```
73
-
74
49
  ## License
75
50
 
76
51
  This project is licensed under the MIT license.
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const k9crypt = require('./src/index');
2
+
3
+ module.exports = k9crypt;
package/package.json CHANGED
@@ -1,33 +1,10 @@
1
1
  {
2
2
  "name": "k9crypt",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "A special encryption algorithm created for K9Crypt.",
5
- "type": "module",
6
- "main": "./dist/cjs/index.js",
7
- "module": "./dist/esm/index.js",
8
- "types": "./dist/types/index.d.ts",
9
- "exports": {
10
- ".": {
11
- "import": {
12
- "types": "./dist/types/index.d.ts",
13
- "default": "./dist/esm/index.js"
14
- },
15
- "require": {
16
- "types": "./dist/types/index.d.ts",
17
- "default": "./dist/cjs/index.js"
18
- }
19
- }
20
- },
21
- "files": [
22
- "dist"
23
- ],
5
+ "main": "index.js",
24
6
  "scripts": {
25
- "test": "echo \"Error: no test specified\" && exit 1",
26
- "clean": "rm -rf dist",
27
- "build:types": "tsc --project tsconfig.json",
28
- "build:esm": "babel src --out-dir dist/esm --copy-files",
29
- "build:cjs": "babel src --out-dir dist/cjs --copy-files --config-file ./babel.config.cjs",
30
- "build": "bun run clean && bun run build:esm && bun run build:cjs && bun run build:types"
7
+ "test": "echo \"Error: no test specified\" && exit 1"
31
8
  },
32
9
  "keywords": [
33
10
  "secure",
@@ -41,12 +18,9 @@
41
18
  "author": "K9Crypt Team",
42
19
  "license": "MIT",
43
20
  "dependencies": {
44
- "bcrypt": "^6.0.0",
21
+ "argon2": "^0.44.0",
45
22
  "crypto-js": "^4.2.0",
46
- "lz4": "^0.6.5",
47
- "lzma-native": "^8.0.6",
48
- "node-forge": "^1.3.1",
49
- "xxhash": "^0.3.0"
23
+ "lzma-native": "^8.0.6"
50
24
  },
51
25
  "repository": {
52
26
  "type": "git",
@@ -55,11 +29,5 @@
55
29
  "bugs": {
56
30
  "url": "https://github.com/K9Crypt/module/issues"
57
31
  },
58
- "homepage": "https://k9crypt.xyz",
59
- "devDependencies": {
60
- "@babel/cli": "^7.28.0",
61
- "@babel/core": "^7.28.0",
62
- "@babel/preset-env": "^7.28.0",
63
- "typescript": "^5.8.3"
64
- }
32
+ "homepage": "https://k9crypt.xyz"
65
33
  }
@@ -1,12 +1,12 @@
1
- "use strict";
2
-
3
1
  module.exports = {
4
2
  SALT_SIZE: 32,
5
3
  IV_SIZE: 16,
6
4
  KEY_SIZE: 32,
7
5
  TAG_SIZE: 16,
8
- PBKDF2_ITERATIONS: 310000,
9
- HASH_SEED: 0xCAFEBABE,
6
+ PBKDF2_ITERATIONS: 50000,
7
+ HASH_SEED: 0xcafebabe,
10
8
  PEPPER: 'veryLongAndComplexPepperValue123!@#$%^&*()_+[]{}|;:,.<>?',
11
- HMAC_KEY: 'veryLongAndComplexHMACKeyValue456!@#$%^&*()_+[]{}|;:,.<>?'
12
- };
9
+ HMAC_KEY: 'veryLongAndComplexHMACKeyValue456!@#$%^&*()_+[]{}|;:,.<>?',
10
+ ARGON2_SALT_SIZE: 16,
11
+ ARGON2_HASH_LENGTH: 64
12
+ };
@@ -1,26 +1,10 @@
1
- "use strict";
2
-
3
1
  const crypto = require('crypto');
4
- const {
5
- compress,
6
- decompress
7
- } = require('./utils/compression');
8
- const {
9
- deriveKey
10
- } = require('./utils/keyDerivation');
11
- const {
12
- encrypt,
13
- decrypt
14
- } = require('./utils/encryption');
15
- const {
16
- hash,
17
- verifyHash
18
- } = require('./utils/hashing');
19
- const {
20
- SALT_SIZE,
21
- IV_SIZE,
22
- TAG_SIZE
23
- } = require('./constants');
2
+ const { compress, decompress } = require('./utils/compression');
3
+ const { deriveKey } = require('./utils/keyDerivation');
4
+ const { encrypt, decrypt } = require('./utils/encryption');
5
+ const { hash, verifyHash } = require('./utils/hashing');
6
+ const { SALT_SIZE, IV_SIZE, TAG_SIZE, ARGON2_SALT_SIZE, ARGON2_HASH_LENGTH } = require('./constants');
7
+
24
8
  class K9crypt {
25
9
  constructor(secretKey) {
26
10
  if (!secretKey) {
@@ -31,31 +15,27 @@ class K9crypt {
31
15
  this.secretKey = secretKey;
32
16
  }
33
17
  }
18
+
34
19
  getGenerated() {
35
20
  return this._autoGenerated ? this.secretKey : null;
36
21
  }
22
+
37
23
  async encrypt(plaintext) {
38
24
  try {
39
25
  const compressed = await compress(plaintext);
40
26
  const salt = crypto.randomBytes(SALT_SIZE);
41
27
  const key = await deriveKey(this.secretKey, salt);
42
- const {
43
- iv1,
44
- iv2,
45
- iv3,
46
- iv4,
47
- iv5,
48
- encrypted,
49
- tag1
50
- } = encrypt(compressed, key);
51
- const dataToHash = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1]);
52
- const dataHash = hash(dataToHash);
53
- const result = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1, dataHash]);
28
+ const { iv1, iv2, iv3, iv4, iv5, encrypted, tag1 } = await encrypt(compressed, key);
29
+ const dataToHash = Buffer.concat([ salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1 ]);
30
+ const argon2Salt = crypto.randomBytes(ARGON2_SALT_SIZE);
31
+ const dataHash = await hash(dataToHash, argon2Salt);
32
+ const result = Buffer.concat([ salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1, argon2Salt, dataHash ]);
54
33
  return result.toString('base64');
55
34
  } catch (error) {
56
35
  console.log('Encryption failed');
57
36
  }
58
37
  }
38
+
59
39
  async decrypt(ciphertext) {
60
40
  try {
61
41
  const data = Buffer.from(ciphertext, 'base64');
@@ -65,15 +45,19 @@ class K9crypt {
65
45
  const iv3 = data.slice(SALT_SIZE + 2 * IV_SIZE, SALT_SIZE + 3 * IV_SIZE);
66
46
  const iv4 = data.slice(SALT_SIZE + 3 * IV_SIZE, SALT_SIZE + 4 * IV_SIZE);
67
47
  const iv5 = data.slice(SALT_SIZE + 4 * IV_SIZE, SALT_SIZE + 5 * IV_SIZE);
68
- const encrypted = data.slice(SALT_SIZE + 5 * IV_SIZE, -TAG_SIZE - 64);
69
- const tag1 = data.slice(-TAG_SIZE - 64, -64);
70
- const dataHash = data.slice(-64);
71
- const dataToVerify = data.slice(0, -64);
72
- if (!verifyHash(dataToVerify, dataHash)) {
48
+
49
+ const dataHash = data.slice(-ARGON2_HASH_LENGTH);
50
+ const argon2Salt = data.slice(-ARGON2_HASH_LENGTH - ARGON2_SALT_SIZE, -ARGON2_HASH_LENGTH);
51
+ const tag1 = data.slice(-ARGON2_HASH_LENGTH - ARGON2_SALT_SIZE - TAG_SIZE, -ARGON2_HASH_LENGTH - ARGON2_SALT_SIZE);
52
+ const encrypted = data.slice(SALT_SIZE + 5 * IV_SIZE, -ARGON2_HASH_LENGTH - ARGON2_SALT_SIZE - TAG_SIZE);
53
+
54
+ const dataToVerify = data.slice(0, -ARGON2_HASH_LENGTH - ARGON2_SALT_SIZE);
55
+ if (!(await verifyHash(dataToVerify, dataHash, argon2Salt))) {
73
56
  console.log('Data integrity check failed');
74
57
  }
58
+
75
59
  const key = await deriveKey(this.secretKey, salt);
76
- const decrypted = decrypt(encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1);
60
+ const decrypted = await decrypt(encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1);
77
61
  const decompressed = await decompress(decrypted);
78
62
  return decompressed.toString('utf8');
79
63
  } catch (error) {
@@ -81,4 +65,5 @@ class K9crypt {
81
65
  }
82
66
  }
83
67
  }
84
- module.exports = K9crypt;
68
+
69
+ module.exports = K9crypt;
@@ -1,38 +1,48 @@
1
- "use strict";
2
-
3
1
  const zlib = require('zlib');
4
2
  const lzma = require('lzma-native');
5
- exports.compress = async data => {
3
+
4
+ exports.compress = async (data) => {
6
5
  try {
7
6
  const brotliParams = {
8
7
  params: {
9
8
  [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
10
- [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
9
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 4,
11
10
  [zlib.constants.BROTLI_PARAM_SIZE_HINT]: Buffer.byteLength(data, 'utf8'),
12
11
  [zlib.constants.BROTLI_PARAM_LGWIN]: 24
13
12
  }
14
13
  };
14
+
15
15
  const brotliCompressed = await new Promise((resolve, reject) => {
16
- zlib.brotliCompress(Buffer.from(data, 'utf8'), brotliParams, (err, compressed) => {
17
- if (err) reject(err);else resolve(compressed);
18
- });
16
+ zlib.brotliCompress(
17
+ Buffer.from(data, 'utf8'),
18
+ brotliParams,
19
+ (err, compressed) => {
20
+ if (err) reject(err);
21
+ else resolve(compressed);
22
+ }
23
+ );
19
24
  });
20
- const lzmaCompressed = await lzma.compress(brotliCompressed, 9);
25
+
26
+ const lzmaCompressed = await lzma.compress(brotliCompressed, 3);
21
27
  return lzmaCompressed;
22
28
  } catch (error) {
23
29
  throw new Error(`Compression error: ${error.message}`);
24
30
  }
25
31
  };
26
- exports.decompress = async data => {
32
+
33
+ exports.decompress = async (data) => {
27
34
  try {
28
35
  const lzmaDecompressed = await lzma.decompress(data);
36
+
29
37
  const brotliDecompressed = await new Promise((resolve, reject) => {
30
38
  zlib.brotliDecompress(lzmaDecompressed, (err, decompressed) => {
31
- if (err) reject(err);else resolve(decompressed);
39
+ if (err) reject(err);
40
+ else resolve(decompressed);
32
41
  });
33
42
  });
43
+
34
44
  return brotliDecompressed;
35
45
  } catch (error) {
36
46
  throw new Error(`Decompression error: ${error.message}`);
37
47
  }
38
- };
48
+ };
@@ -0,0 +1,61 @@
1
+ const crypto = require('crypto');
2
+ const { Readable } = require('stream');
3
+ const { IV_SIZE } = require('../constants');
4
+ const { reverseBuffer } = require('./math');
5
+
6
+ exports.encrypt = (data, key) => {
7
+ return new Promise((resolve, reject) => {
8
+ const iv1 = crypto.randomBytes(IV_SIZE);
9
+ const cipher1 = crypto.createCipheriv('aes-256-gcm', key, iv1);
10
+
11
+ const iv2 = crypto.randomBytes(IV_SIZE);
12
+ const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
13
+
14
+ const iv3 = crypto.randomBytes(IV_SIZE);
15
+ const cipher3 = crypto.createCipheriv('aes-256-cfb', key, iv3);
16
+
17
+ const iv4 = crypto.randomBytes(IV_SIZE);
18
+ const cipher4 = crypto.createCipheriv('aes-256-ofb', key, iv4);
19
+
20
+ const iv5 = crypto.randomBytes(IV_SIZE);
21
+ const cipher5 = crypto.createCipheriv('aes-256-ctr', key, iv5);
22
+
23
+ const readable = Readable.from(data);
24
+ const chunks = [];
25
+
26
+ const stream = readable.pipe(cipher1).pipe(cipher2).pipe(cipher3).pipe(cipher4).pipe(cipher5);
27
+
28
+ stream.on('data', (chunk) => chunks.push(chunk));
29
+ stream.on('error', (err) => reject(err));
30
+ stream.on('end', () => {
31
+ const encrypted = Buffer.concat(chunks);
32
+ const tag1 = cipher1.getAuthTag();
33
+ const permutedEncrypted = reverseBuffer(encrypted);
34
+ resolve({ iv1, iv2, iv3, iv4, iv5, encrypted: permutedEncrypted, tag1 });
35
+ });
36
+ });
37
+ };
38
+
39
+ exports.decrypt = (encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1) => {
40
+ return new Promise((resolve, reject) => {
41
+ const originalEncrypted = reverseBuffer(encrypted);
42
+
43
+ const decipher5 = crypto.createDecipheriv('aes-256-ctr', key, iv5);
44
+ const decipher4 = crypto.createDecipheriv('aes-256-ofb', key, iv4);
45
+ const decipher3 = crypto.createDecipheriv('aes-256-cfb', key, iv3);
46
+ const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
47
+ const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
48
+ decipher1.setAuthTag(tag1);
49
+
50
+ const readable = Readable.from(originalEncrypted);
51
+ const chunks = [];
52
+
53
+ const stream = readable.pipe(decipher5).pipe(decipher4).pipe(decipher3).pipe(decipher2).pipe(decipher1);
54
+
55
+ stream.on('data', (chunk) => chunks.push(chunk));
56
+ stream.on('error', (err) => reject(err));
57
+ stream.on('end', () => {
58
+ resolve(Buffer.concat(chunks));
59
+ });
60
+ });
61
+ };
@@ -0,0 +1,42 @@
1
+ const crypto = require('crypto');
2
+ const argon2 = require('argon2');
3
+ const { HMAC_KEY, ARGON2_HASH_LENGTH } = require('../constants');
4
+ const { reverseHash } = require('./math');
5
+
6
+ const ARGON2_OPTIONS = {
7
+ timeCost: 1,
8
+ memoryCost: 12288,
9
+ parallelism: 4,
10
+ type: argon2.argon2id,
11
+ hashLength: ARGON2_HASH_LENGTH
12
+ };
13
+
14
+ exports.hash = async (data, salt) => {
15
+ const hmac = crypto.createHmac('sha512', HMAC_KEY);
16
+ hmac.update(data);
17
+ const digest = hmac.digest();
18
+ const reversedSha512Hash = reverseHash(digest);
19
+
20
+ const argon2Hash = await argon2.hash(reversedSha512Hash, {
21
+ ...ARGON2_OPTIONS,
22
+ salt,
23
+ raw: true
24
+ });
25
+
26
+ return argon2Hash;
27
+ };
28
+
29
+ exports.verifyHash = async (data, hash, salt) => {
30
+ const hmac = crypto.createHmac('sha512', HMAC_KEY);
31
+ hmac.update(data);
32
+ const digest = hmac.digest();
33
+ const reversedSha512Hash = reverseHash(digest);
34
+
35
+ const expectedHash = await argon2.hash(reversedSha512Hash, {
36
+ ...ARGON2_OPTIONS,
37
+ salt,
38
+ raw: true
39
+ });
40
+
41
+ return crypto.timingSafeEqual(hash, expectedHash);
42
+ };
@@ -1,20 +1,15 @@
1
- "use strict";
2
-
3
1
  const crypto = require('crypto');
4
- const {
5
- PBKDF2_ITERATIONS,
6
- KEY_SIZE,
7
- PEPPER
8
- } = require('../constants');
9
- const {
10
- enhanceKey
11
- } = require('./math');
2
+ const { PBKDF2_ITERATIONS, KEY_SIZE, PEPPER } = require('../constants');
3
+ const { enhanceKey } = require('./math');
4
+
12
5
  exports.deriveKey = (password, salt) => {
13
6
  return new Promise((resolve, reject) => {
14
7
  const pepperedPassword = password + PEPPER;
15
8
  crypto.pbkdf2(pepperedPassword, salt, PBKDF2_ITERATIONS, KEY_SIZE, 'sha512', (err, key) => {
16
9
  if (err) reject(err);
10
+
17
11
  const enhancedKey = enhanceKey(key);
12
+
18
13
  resolve(enhancedKey);
19
14
  });
20
15
  });
@@ -0,0 +1,11 @@
1
+ exports.reverseBuffer = (data) => {
2
+ return Buffer.from(data).reverse();
3
+ };
4
+
5
+ exports.reverseHash = (hash) => {
6
+ return Buffer.from(hash).reverse();
7
+ };
8
+
9
+ exports.enhanceKey = (key) => {
10
+ return Buffer.from(key).reverse();
11
+ };
package/dist/cjs/index.js DELETED
@@ -1,84 +0,0 @@
1
- "use strict";
2
-
3
- const crypto = require('crypto');
4
- const {
5
- compress,
6
- decompress
7
- } = require('./utils/compression');
8
- const {
9
- deriveKey
10
- } = require('./utils/keyDerivation');
11
- const {
12
- encrypt,
13
- decrypt
14
- } = require('./utils/encryption');
15
- const {
16
- hash,
17
- verifyHash
18
- } = require('./utils/hashing');
19
- const {
20
- SALT_SIZE,
21
- IV_SIZE,
22
- TAG_SIZE
23
- } = require('./constants');
24
- class K9crypt {
25
- constructor(secretKey) {
26
- if (!secretKey) {
27
- this.secretKey = crypto.randomBytes(50);
28
- this._autoGenerated = true;
29
- } else {
30
- this._autoGenerated = false;
31
- this.secretKey = secretKey;
32
- }
33
- }
34
- getGenerated() {
35
- return this._autoGenerated ? this.secretKey : null;
36
- }
37
- async encrypt(plaintext) {
38
- try {
39
- const compressed = await compress(plaintext);
40
- const salt = crypto.randomBytes(SALT_SIZE);
41
- const key = await deriveKey(this.secretKey, salt);
42
- const {
43
- iv1,
44
- iv2,
45
- iv3,
46
- iv4,
47
- iv5,
48
- encrypted,
49
- tag1
50
- } = encrypt(compressed, key);
51
- const dataToHash = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1]);
52
- const dataHash = hash(dataToHash);
53
- const result = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1, dataHash]);
54
- return result.toString('base64');
55
- } catch (error) {
56
- console.log('Encryption failed');
57
- }
58
- }
59
- async decrypt(ciphertext) {
60
- try {
61
- const data = Buffer.from(ciphertext, 'base64');
62
- const salt = data.slice(0, SALT_SIZE);
63
- const iv1 = data.slice(SALT_SIZE, SALT_SIZE + IV_SIZE);
64
- const iv2 = data.slice(SALT_SIZE + IV_SIZE, SALT_SIZE + 2 * IV_SIZE);
65
- const iv3 = data.slice(SALT_SIZE + 2 * IV_SIZE, SALT_SIZE + 3 * IV_SIZE);
66
- const iv4 = data.slice(SALT_SIZE + 3 * IV_SIZE, SALT_SIZE + 4 * IV_SIZE);
67
- const iv5 = data.slice(SALT_SIZE + 4 * IV_SIZE, SALT_SIZE + 5 * IV_SIZE);
68
- const encrypted = data.slice(SALT_SIZE + 5 * IV_SIZE, -TAG_SIZE - 64);
69
- const tag1 = data.slice(-TAG_SIZE - 64, -64);
70
- const dataHash = data.slice(-64);
71
- const dataToVerify = data.slice(0, -64);
72
- if (!verifyHash(dataToVerify, dataHash)) {
73
- console.log('Data integrity check failed');
74
- }
75
- const key = await deriveKey(this.secretKey, salt);
76
- const decrypted = decrypt(encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1);
77
- const decompressed = await decompress(decrypted);
78
- return decompressed.toString('utf8');
79
- } catch (error) {
80
- console.log('Decryption failed');
81
- }
82
- }
83
- }
84
- module.exports = K9crypt;
@@ -1,62 +0,0 @@
1
- "use strict";
2
-
3
- const crypto = require('crypto');
4
- const {
5
- IV_SIZE
6
- } = require('../constants');
7
- const {
8
- reverseBuffer
9
- } = require('./math');
10
- exports.encrypt = (data, key) => {
11
- const iv1 = crypto.randomBytes(IV_SIZE);
12
- const cipher1 = crypto.createCipheriv('aes-256-gcm', key, iv1);
13
- let encrypted1 = cipher1.update(data);
14
- encrypted1 = Buffer.concat([encrypted1, cipher1.final()]);
15
- const tag1 = cipher1.getAuthTag();
16
- const iv2 = crypto.randomBytes(IV_SIZE);
17
- const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
18
- let encrypted2 = cipher2.update(encrypted1);
19
- encrypted2 = Buffer.concat([encrypted2, cipher2.final()]);
20
- const iv3 = crypto.randomBytes(IV_SIZE);
21
- const cipher3 = crypto.createCipheriv('aes-256-cfb', key, iv3);
22
- let encrypted3 = cipher3.update(encrypted2);
23
- encrypted3 = Buffer.concat([encrypted3, cipher3.final()]);
24
- const iv4 = crypto.randomBytes(IV_SIZE);
25
- const cipher4 = crypto.createCipheriv('aes-256-ofb', key, iv4);
26
- let encrypted4 = cipher4.update(encrypted3);
27
- encrypted4 = Buffer.concat([encrypted4, cipher4.final()]);
28
- const iv5 = crypto.randomBytes(IV_SIZE);
29
- const cipher5 = crypto.createCipheriv('aes-256-ctr', key, iv5);
30
- let encrypted5 = cipher5.update(encrypted4);
31
- encrypted5 = Buffer.concat([encrypted5, cipher5.final()]);
32
- const permutedEncrypted = reverseBuffer(encrypted5);
33
- return {
34
- iv1,
35
- iv2,
36
- iv3,
37
- iv4,
38
- iv5,
39
- encrypted: permutedEncrypted,
40
- tag1
41
- };
42
- };
43
- exports.decrypt = (encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1) => {
44
- const originalEncrypted = reverseBuffer(encrypted, true);
45
- const decipher5 = crypto.createDecipheriv('aes-256-ctr', key, iv5);
46
- let decrypted5 = decipher5.update(originalEncrypted);
47
- decrypted5 = Buffer.concat([decrypted5, decipher5.final()]);
48
- const decipher4 = crypto.createDecipheriv('aes-256-ofb', key, iv4);
49
- let decrypted4 = decipher4.update(decrypted5);
50
- decrypted4 = Buffer.concat([decrypted4, decipher4.final()]);
51
- const decipher3 = crypto.createDecipheriv('aes-256-cfb', key, iv3);
52
- let decrypted3 = decipher3.update(decrypted4);
53
- decrypted3 = Buffer.concat([decrypted3, decipher3.final()]);
54
- const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
55
- let decrypted2 = decipher2.update(decrypted3);
56
- decrypted2 = Buffer.concat([decrypted2, decipher2.final()]);
57
- const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
58
- decipher1.setAuthTag(tag1);
59
- let decrypted1 = decipher1.update(decrypted2);
60
- decrypted1 = Buffer.concat([decrypted1, decipher1.final()]);
61
- return decrypted1;
62
- };
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- const crypto = require('crypto');
4
- const {
5
- HMAC_KEY
6
- } = require('../constants');
7
- const {
8
- reverseHash
9
- } = require('./math');
10
- exports.hash = data => {
11
- const hmac = crypto.createHmac('sha512', HMAC_KEY);
12
- hmac.update(data);
13
- const digest = hmac.digest();
14
- return reverseHash(digest);
15
- };
16
- exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
@@ -1,14 +0,0 @@
1
- "use strict";
2
-
3
- exports.reverseBuffer = (data, reverse = false) => {
4
- if (reverse) {
5
- return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
6
- }
7
- return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
8
- };
9
- exports.reverseHash = hash => {
10
- return Buffer.from(hash.toString('hex').split('').reverse().join(''), 'hex');
11
- };
12
- exports.enhanceKey = key => {
13
- return Buffer.from(key.toString('hex').split('').reverse().join(''), 'hex');
14
- };
@@ -1,8 +0,0 @@
1
- export let SALT_SIZE: number;
2
- export let IV_SIZE: number;
3
- export let KEY_SIZE: number;
4
- export let TAG_SIZE: number;
5
- export let PBKDF2_ITERATIONS: number;
6
- export let HASH_SEED: number;
7
- export let PEPPER: string;
8
- export let HMAC_KEY: string;
@@ -1,12 +0,0 @@
1
- "use strict";
2
-
3
- module.exports = {
4
- SALT_SIZE: 32,
5
- IV_SIZE: 16,
6
- KEY_SIZE: 32,
7
- TAG_SIZE: 16,
8
- PBKDF2_ITERATIONS: 310000,
9
- HASH_SEED: 0xCAFEBABE,
10
- PEPPER: 'veryLongAndComplexPepperValue123!@#$%^&*()_+[]{}|;:,.<>?',
11
- HMAC_KEY: 'veryLongAndComplexHMACKeyValue456!@#$%^&*()_+[]{}|;:,.<>?'
12
- };
@@ -1,38 +0,0 @@
1
- "use strict";
2
-
3
- const zlib = require('zlib');
4
- const lzma = require('lzma-native');
5
- exports.compress = async data => {
6
- try {
7
- const brotliParams = {
8
- params: {
9
- [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
10
- [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
11
- [zlib.constants.BROTLI_PARAM_SIZE_HINT]: Buffer.byteLength(data, 'utf8'),
12
- [zlib.constants.BROTLI_PARAM_LGWIN]: 24
13
- }
14
- };
15
- const brotliCompressed = await new Promise((resolve, reject) => {
16
- zlib.brotliCompress(Buffer.from(data, 'utf8'), brotliParams, (err, compressed) => {
17
- if (err) reject(err);else resolve(compressed);
18
- });
19
- });
20
- const lzmaCompressed = await lzma.compress(brotliCompressed, 9);
21
- return lzmaCompressed;
22
- } catch (error) {
23
- throw new Error(`Compression error: ${error.message}`);
24
- }
25
- };
26
- exports.decompress = async data => {
27
- try {
28
- const lzmaDecompressed = await lzma.decompress(data);
29
- const brotliDecompressed = await new Promise((resolve, reject) => {
30
- zlib.brotliDecompress(lzmaDecompressed, (err, decompressed) => {
31
- if (err) reject(err);else resolve(decompressed);
32
- });
33
- });
34
- return brotliDecompressed;
35
- } catch (error) {
36
- throw new Error(`Decompression error: ${error.message}`);
37
- }
38
- };
@@ -1,62 +0,0 @@
1
- "use strict";
2
-
3
- const crypto = require('crypto');
4
- const {
5
- IV_SIZE
6
- } = require('../constants');
7
- const {
8
- reverseBuffer
9
- } = require('./math');
10
- exports.encrypt = (data, key) => {
11
- const iv1 = crypto.randomBytes(IV_SIZE);
12
- const cipher1 = crypto.createCipheriv('aes-256-gcm', key, iv1);
13
- let encrypted1 = cipher1.update(data);
14
- encrypted1 = Buffer.concat([encrypted1, cipher1.final()]);
15
- const tag1 = cipher1.getAuthTag();
16
- const iv2 = crypto.randomBytes(IV_SIZE);
17
- const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
18
- let encrypted2 = cipher2.update(encrypted1);
19
- encrypted2 = Buffer.concat([encrypted2, cipher2.final()]);
20
- const iv3 = crypto.randomBytes(IV_SIZE);
21
- const cipher3 = crypto.createCipheriv('aes-256-cfb', key, iv3);
22
- let encrypted3 = cipher3.update(encrypted2);
23
- encrypted3 = Buffer.concat([encrypted3, cipher3.final()]);
24
- const iv4 = crypto.randomBytes(IV_SIZE);
25
- const cipher4 = crypto.createCipheriv('aes-256-ofb', key, iv4);
26
- let encrypted4 = cipher4.update(encrypted3);
27
- encrypted4 = Buffer.concat([encrypted4, cipher4.final()]);
28
- const iv5 = crypto.randomBytes(IV_SIZE);
29
- const cipher5 = crypto.createCipheriv('aes-256-ctr', key, iv5);
30
- let encrypted5 = cipher5.update(encrypted4);
31
- encrypted5 = Buffer.concat([encrypted5, cipher5.final()]);
32
- const permutedEncrypted = reverseBuffer(encrypted5);
33
- return {
34
- iv1,
35
- iv2,
36
- iv3,
37
- iv4,
38
- iv5,
39
- encrypted: permutedEncrypted,
40
- tag1
41
- };
42
- };
43
- exports.decrypt = (encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1) => {
44
- const originalEncrypted = reverseBuffer(encrypted, true);
45
- const decipher5 = crypto.createDecipheriv('aes-256-ctr', key, iv5);
46
- let decrypted5 = decipher5.update(originalEncrypted);
47
- decrypted5 = Buffer.concat([decrypted5, decipher5.final()]);
48
- const decipher4 = crypto.createDecipheriv('aes-256-ofb', key, iv4);
49
- let decrypted4 = decipher4.update(decrypted5);
50
- decrypted4 = Buffer.concat([decrypted4, decipher4.final()]);
51
- const decipher3 = crypto.createDecipheriv('aes-256-cfb', key, iv3);
52
- let decrypted3 = decipher3.update(decrypted4);
53
- decrypted3 = Buffer.concat([decrypted3, decipher3.final()]);
54
- const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
55
- let decrypted2 = decipher2.update(decrypted3);
56
- decrypted2 = Buffer.concat([decrypted2, decipher2.final()]);
57
- const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
58
- decipher1.setAuthTag(tag1);
59
- let decrypted1 = decipher1.update(decrypted2);
60
- decrypted1 = Buffer.concat([decrypted1, decipher1.final()]);
61
- return decrypted1;
62
- };
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- const crypto = require('crypto');
4
- const {
5
- HMAC_KEY
6
- } = require('../constants');
7
- const {
8
- reverseHash
9
- } = require('./math');
10
- exports.hash = data => {
11
- const hmac = crypto.createHmac('sha512', HMAC_KEY);
12
- hmac.update(data);
13
- const digest = hmac.digest();
14
- return reverseHash(digest);
15
- };
16
- exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- const crypto = require('crypto');
4
- const {
5
- PBKDF2_ITERATIONS,
6
- KEY_SIZE,
7
- PEPPER
8
- } = require('../constants');
9
- const {
10
- enhanceKey
11
- } = require('./math');
12
- exports.deriveKey = (password, salt) => {
13
- return new Promise((resolve, reject) => {
14
- const pepperedPassword = password + PEPPER;
15
- crypto.pbkdf2(pepperedPassword, salt, PBKDF2_ITERATIONS, KEY_SIZE, 'sha512', (err, key) => {
16
- if (err) reject(err);
17
- const enhancedKey = enhanceKey(key);
18
- resolve(enhancedKey);
19
- });
20
- });
21
- };
@@ -1,14 +0,0 @@
1
- "use strict";
2
-
3
- exports.reverseBuffer = (data, reverse = false) => {
4
- if (reverse) {
5
- return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
6
- }
7
- return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
8
- };
9
- exports.reverseHash = hash => {
10
- return Buffer.from(hash.toString('hex').split('').reverse().join(''), 'hex');
11
- };
12
- exports.enhanceKey = key => {
13
- return Buffer.from(key.toString('hex').split('').reverse().join(''), 'hex');
14
- };
package/dist/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export = K9crypt;
2
- declare class K9crypt {
3
- constructor(secretKey: any);
4
- secretKey: any;
5
- _autoGenerated: boolean;
6
- getGenerated(): any;
7
- encrypt(plaintext: any): Promise<any>;
8
- decrypt(ciphertext: any): Promise<any>;
9
- }
@@ -1,2 +0,0 @@
1
- export function compress(data: any): Promise<any>;
2
- export function decompress(data: any): Promise<any>;
@@ -1,10 +0,0 @@
1
- export function encrypt(data: any, key: any): {
2
- iv1: any;
3
- iv2: any;
4
- iv3: any;
5
- iv4: any;
6
- iv5: any;
7
- encrypted: any;
8
- tag1: any;
9
- };
10
- export function decrypt(encrypted: any, key: any, iv1: any, iv2: any, iv3: any, iv4: any, iv5: any, tag1: any): any;
@@ -1,2 +0,0 @@
1
- export function hash(data: any): any;
2
- export function verifyHash(data: any, hash: any): any;
@@ -1 +0,0 @@
1
- export function deriveKey(password: any, salt: any): any;
@@ -1,3 +0,0 @@
1
- export function reverseBuffer(data: any, reverse?: boolean): any;
2
- export function reverseHash(hash: any): any;
3
- export function enhanceKey(key: any): any;