k9crypt 1.1.0 → 1.1.2

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
@@ -1,10 +1,12 @@
1
- # K9Crypt Encryption Algorithm
1
+ ![](https://www.upload.ee/image/18092921/k9crypt-npm-banner.png)
2
+
3
+ # K9Crypt Algorithm
2
4
 
3
5
  This is a special encryption algorithm created for K9Crypt.
4
6
 
5
7
  ## Updates
6
- **v1.1.0**
7
- - Modules have been updated to the latest version.
8
+ **v1.1.2**
9
+ - The issue caused by modules has been resolved.
8
10
 
9
11
  ## Installation
10
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "k9crypt",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "A special encryption algorithm created for K9Crypt.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,12 +18,12 @@
18
18
  "author": "K9Crypt Team",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "bcrypt": "5.1.1",
22
- "crypto-js": "4.2.0",
23
- "lz4": "0.6.5",
24
- "lzma-native": "8.0.6",
25
- "node-forge": "1.3.1",
26
- "xxhash": "0.3.0"
21
+ "bcrypt": "^6.0.0",
22
+ "crypto-js": "^4.2.0",
23
+ "lz4": "^0.6.5",
24
+ "lzma-native": "^8.0.6",
25
+ "node-forge": "^1.3.1",
26
+ "xxhash": "^0.3.0"
27
27
  },
28
28
  "repository": {
29
29
  "type": "git",
package/src/constants.js CHANGED
@@ -1,10 +1,10 @@
1
1
  module.exports = {
2
- SALT_SIZE: 32,
3
- IV_SIZE: 16,
4
- KEY_SIZE: 32,
5
- TAG_SIZE: 16,
6
- PBKDF2_ITERATIONS: 310000,
7
- HASH_SEED: 0xCAFEBABE,
8
- PEPPER: 'veryLongAndComplexPepperValue123!@#$%^&*()_+[]{}|;:,.<>?',
9
- HMAC_KEY: 'veryLongAndComplexHMACKeyValue456!@#$%^&*()_+[]{}|;:,.<>?',
2
+ SALT_SIZE: 32,
3
+ IV_SIZE: 16,
4
+ KEY_SIZE: 32,
5
+ TAG_SIZE: 16,
6
+ PBKDF2_ITERATIONS: 310000,
7
+ HASH_SEED: 0xCAFEBABE,
8
+ PEPPER: 'veryLongAndComplexPepperValue123!@#$%^&*()_+[]{}|;:,.<>?',
9
+ HMAC_KEY: 'veryLongAndComplexHMACKeyValue456!@#$%^&*()_+[]{}|;:,.<>?',
10
10
  };
package/src/index.js CHANGED
@@ -6,51 +6,51 @@ const { hash, verifyHash } = require('./utils/hashing');
6
6
  const { SALT_SIZE, IV_SIZE, TAG_SIZE } = require('./constants');
7
7
 
8
8
  class K9crypt {
9
- constructor(secretKey) {
10
- this.secretKey = secretKey;
11
- }
9
+ constructor(secretKey) {
10
+ this.secretKey = secretKey;
11
+ }
12
12
 
13
- async encrypt(plaintext) {
14
- try {
15
- const compressed = await compress(plaintext);
16
- const salt = crypto.randomBytes(SALT_SIZE);
17
- const key = await deriveKey(this.secretKey, salt);
18
- const { iv1, iv2, iv3, iv4, iv5, encrypted, tag1 } = encrypt(compressed, key);
19
- const dataToHash = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1]);
20
- const dataHash = hash(dataToHash);
21
- const result = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1, dataHash]);
22
- return result.toString('base64');
23
- } catch (error) {
24
- console.log('Encryption failed');
25
- }
13
+ async encrypt(plaintext) {
14
+ try {
15
+ const compressed = await compress(plaintext);
16
+ const salt = crypto.randomBytes(SALT_SIZE);
17
+ const key = await deriveKey(this.secretKey, salt);
18
+ const { iv1, iv2, iv3, iv4, iv5, encrypted, tag1 } = encrypt(compressed, key);
19
+ const dataToHash = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1]);
20
+ const dataHash = hash(dataToHash);
21
+ const result = Buffer.concat([salt, iv1, iv2, iv3, iv4, iv5, encrypted, tag1, dataHash]);
22
+ return result.toString('base64');
23
+ } catch (error) {
24
+ console.log('Encryption failed');
26
25
  }
26
+ }
27
27
 
28
- async decrypt(ciphertext) {
29
- try {
30
- const data = Buffer.from(ciphertext, 'base64');
31
- const salt = data.slice(0, SALT_SIZE);
32
- const iv1 = data.slice(SALT_SIZE, SALT_SIZE + IV_SIZE);
33
- const iv2 = data.slice(SALT_SIZE + IV_SIZE, SALT_SIZE + 2 * IV_SIZE);
34
- const iv3 = data.slice(SALT_SIZE + 2 * IV_SIZE, SALT_SIZE + 3 * IV_SIZE);
35
- const iv4 = data.slice(SALT_SIZE + 3 * IV_SIZE, SALT_SIZE + 4 * IV_SIZE);
36
- const iv5 = data.slice(SALT_SIZE + 4 * IV_SIZE, SALT_SIZE + 5 * IV_SIZE);
37
- const encrypted = data.slice(SALT_SIZE + 5 * IV_SIZE, -TAG_SIZE - 64);
38
- const tag1 = data.slice(-TAG_SIZE - 64, -64);
39
- const dataHash = data.slice(-64);
28
+ async decrypt(ciphertext) {
29
+ try {
30
+ const data = Buffer.from(ciphertext, 'base64');
31
+ const salt = data.slice(0, SALT_SIZE);
32
+ const iv1 = data.slice(SALT_SIZE, SALT_SIZE + IV_SIZE);
33
+ const iv2 = data.slice(SALT_SIZE + IV_SIZE, SALT_SIZE + 2 * IV_SIZE);
34
+ const iv3 = data.slice(SALT_SIZE + 2 * IV_SIZE, SALT_SIZE + 3 * IV_SIZE);
35
+ const iv4 = data.slice(SALT_SIZE + 3 * IV_SIZE, SALT_SIZE + 4 * IV_SIZE);
36
+ const iv5 = data.slice(SALT_SIZE + 4 * IV_SIZE, SALT_SIZE + 5 * IV_SIZE);
37
+ const encrypted = data.slice(SALT_SIZE + 5 * IV_SIZE, -TAG_SIZE - 64);
38
+ const tag1 = data.slice(-TAG_SIZE - 64, -64);
39
+ const dataHash = data.slice(-64);
40
40
 
41
- const dataToVerify = data.slice(0, -64);
42
- if (!verifyHash(dataToVerify, dataHash)) {
43
- console.log('Data integrity check failed');
44
- }
41
+ const dataToVerify = data.slice(0, -64);
42
+ if (!verifyHash(dataToVerify, dataHash)) {
43
+ console.log('Data integrity check failed');
44
+ }
45
45
 
46
- const key = await deriveKey(this.secretKey, salt);
47
- const decrypted = decrypt(encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1);
48
- const decompressed = await decompress(decrypted);
49
- return decompressed.toString('utf8');
50
- } catch (error) {
51
- console.log('Decryption failed');
52
- }
46
+ const key = await deriveKey(this.secretKey, salt);
47
+ const decrypted = decrypt(encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1);
48
+ const decompressed = await decompress(decrypted);
49
+ return decompressed.toString('utf8');
50
+ } catch (error) {
51
+ console.log('Decryption failed');
53
52
  }
53
+ }
54
54
  }
55
55
 
56
56
  module.exports = K9crypt;
@@ -2,43 +2,43 @@ const zlib = require('zlib');
2
2
  const lzma = require('lzma-native');
3
3
 
4
4
  exports.compress = async (data) => {
5
- try {
6
- const brotliParams = {
7
- params: {
8
- [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
9
- [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
10
- [zlib.constants.BROTLI_PARAM_SIZE_HINT]: Buffer.byteLength(data, 'utf8'),
11
- [zlib.constants.BROTLI_PARAM_LGWIN]: 24
12
- }
13
- };
5
+ try {
6
+ const brotliParams = {
7
+ params: {
8
+ [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
9
+ [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
10
+ [zlib.constants.BROTLI_PARAM_SIZE_HINT]: Buffer.byteLength(data, 'utf8'),
11
+ [zlib.constants.BROTLI_PARAM_LGWIN]: 24
12
+ }
13
+ };
14
14
 
15
- const brotliCompressed = await new Promise((resolve, reject) => {
16
- zlib.brotliCompress(Buffer.from(data, 'utf8'), brotliParams, (err, compressed) => {
17
- if (err) reject(err);
18
- else resolve(compressed);
19
- });
20
- });
15
+ const brotliCompressed = await new Promise((resolve, reject) => {
16
+ zlib.brotliCompress(Buffer.from(data, 'utf8'), brotliParams, (err, compressed) => {
17
+ if (err) reject(err);
18
+ else resolve(compressed);
19
+ });
20
+ });
21
21
 
22
- const lzmaCompressed = await lzma.compress(brotliCompressed, 9);
23
- return lzmaCompressed;
24
- } catch (error) {
25
- throw new Error(`Compression error: ${error.message}`);
26
- }
22
+ const lzmaCompressed = await lzma.compress(brotliCompressed, 9);
23
+ return lzmaCompressed;
24
+ } catch (error) {
25
+ throw new Error(`Compression error: ${error.message}`);
26
+ }
27
27
  };
28
28
 
29
29
  exports.decompress = async (data) => {
30
- try {
31
- const lzmaDecompressed = await lzma.decompress(data);
30
+ try {
31
+ const lzmaDecompressed = await lzma.decompress(data);
32
32
 
33
- const brotliDecompressed = await new Promise((resolve, reject) => {
34
- zlib.brotliDecompress(lzmaDecompressed, (err, decompressed) => {
35
- if (err) reject(err);
36
- else resolve(decompressed);
37
- });
38
- });
33
+ const brotliDecompressed = await new Promise((resolve, reject) => {
34
+ zlib.brotliDecompress(lzmaDecompressed, (err, decompressed) => {
35
+ if (err) reject(err);
36
+ else resolve(decompressed);
37
+ });
38
+ });
39
39
 
40
- return brotliDecompressed;
41
- } catch (error) {
42
- throw new Error(`Decompression error: ${error.message}`);
43
- }
44
- };
40
+ return brotliDecompressed;
41
+ } catch (error) {
42
+ throw new Error(`Decompression error: ${error.message}`);
43
+ }
44
+ };
@@ -3,50 +3,50 @@ const { IV_SIZE } = require('../constants');
3
3
  const { reverseBuffer } = require('./math');
4
4
 
5
5
  exports.encrypt = (data, key) => {
6
- const iv1 = crypto.randomBytes(IV_SIZE);
7
- const cipher1 = crypto.createCipheriv('aes-256-gcm', key, iv1);
8
- let encrypted1 = cipher1.update(data);
9
- encrypted1 = Buffer.concat([encrypted1, cipher1.final()]);
10
- const tag1 = cipher1.getAuthTag();
11
- const iv2 = crypto.randomBytes(IV_SIZE);
12
- const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
13
- let encrypted2 = cipher2.update(encrypted1);
14
- encrypted2 = Buffer.concat([encrypted2, cipher2.final()]);
15
- const iv3 = crypto.randomBytes(IV_SIZE);
16
- const cipher3 = crypto.createCipheriv('aes-256-cfb', key, iv3);
17
- let encrypted3 = cipher3.update(encrypted2);
18
- encrypted3 = Buffer.concat([encrypted3, cipher3.final()]);
19
- const iv4 = crypto.randomBytes(IV_SIZE);
20
- const cipher4 = crypto.createCipheriv('aes-256-ofb', key, iv4);
21
- let encrypted4 = cipher4.update(encrypted3);
22
- encrypted4 = Buffer.concat([encrypted4, cipher4.final()]);
23
- const iv5 = crypto.randomBytes(IV_SIZE);
24
- const cipher5 = crypto.createCipheriv('aes-256-ctr', key, iv5);
25
- let encrypted5 = cipher5.update(encrypted4);
26
- encrypted5 = Buffer.concat([encrypted5, cipher5.final()]);
27
- const permutedEncrypted = reverseBuffer(encrypted5);
6
+ const iv1 = crypto.randomBytes(IV_SIZE);
7
+ const cipher1 = crypto.createCipheriv('aes-256-gcm', key, iv1);
8
+ let encrypted1 = cipher1.update(data);
9
+ encrypted1 = Buffer.concat([encrypted1, cipher1.final()]);
10
+ const tag1 = cipher1.getAuthTag();
11
+ const iv2 = crypto.randomBytes(IV_SIZE);
12
+ const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv2);
13
+ let encrypted2 = cipher2.update(encrypted1);
14
+ encrypted2 = Buffer.concat([encrypted2, cipher2.final()]);
15
+ const iv3 = crypto.randomBytes(IV_SIZE);
16
+ const cipher3 = crypto.createCipheriv('aes-256-cfb', key, iv3);
17
+ let encrypted3 = cipher3.update(encrypted2);
18
+ encrypted3 = Buffer.concat([encrypted3, cipher3.final()]);
19
+ const iv4 = crypto.randomBytes(IV_SIZE);
20
+ const cipher4 = crypto.createCipheriv('aes-256-ofb', key, iv4);
21
+ let encrypted4 = cipher4.update(encrypted3);
22
+ encrypted4 = Buffer.concat([encrypted4, cipher4.final()]);
23
+ const iv5 = crypto.randomBytes(IV_SIZE);
24
+ const cipher5 = crypto.createCipheriv('aes-256-ctr', key, iv5);
25
+ let encrypted5 = cipher5.update(encrypted4);
26
+ encrypted5 = Buffer.concat([encrypted5, cipher5.final()]);
27
+ const permutedEncrypted = reverseBuffer(encrypted5);
28
28
 
29
- return { iv1, iv2, iv3, iv4, iv5, encrypted: permutedEncrypted, tag1 };
29
+ return { iv1, iv2, iv3, iv4, iv5, encrypted: permutedEncrypted, tag1 };
30
30
  };
31
31
 
32
32
  exports.decrypt = (encrypted, key, iv1, iv2, iv3, iv4, iv5, tag1) => {
33
- const originalEncrypted = reverseBuffer(encrypted, true);
34
- const decipher5 = crypto.createDecipheriv('aes-256-ctr', key, iv5);
35
- let decrypted5 = decipher5.update(originalEncrypted);
36
- decrypted5 = Buffer.concat([decrypted5, decipher5.final()]);
37
- const decipher4 = crypto.createDecipheriv('aes-256-ofb', key, iv4);
38
- let decrypted4 = decipher4.update(decrypted5);
39
- decrypted4 = Buffer.concat([decrypted4, decipher4.final()]);
40
- const decipher3 = crypto.createDecipheriv('aes-256-cfb', key, iv3);
41
- let decrypted3 = decipher3.update(decrypted4);
42
- decrypted3 = Buffer.concat([decrypted3, decipher3.final()]);
43
- const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
44
- let decrypted2 = decipher2.update(decrypted3);
45
- decrypted2 = Buffer.concat([decrypted2, decipher2.final()]);
46
- const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
47
- decipher1.setAuthTag(tag1);
48
- let decrypted1 = decipher1.update(decrypted2);
49
- decrypted1 = Buffer.concat([decrypted1, decipher1.final()]);
33
+ const originalEncrypted = reverseBuffer(encrypted, true);
34
+ const decipher5 = crypto.createDecipheriv('aes-256-ctr', key, iv5);
35
+ let decrypted5 = decipher5.update(originalEncrypted);
36
+ decrypted5 = Buffer.concat([decrypted5, decipher5.final()]);
37
+ const decipher4 = crypto.createDecipheriv('aes-256-ofb', key, iv4);
38
+ let decrypted4 = decipher4.update(decrypted5);
39
+ decrypted4 = Buffer.concat([decrypted4, decipher4.final()]);
40
+ const decipher3 = crypto.createDecipheriv('aes-256-cfb', key, iv3);
41
+ let decrypted3 = decipher3.update(decrypted4);
42
+ decrypted3 = Buffer.concat([decrypted3, decipher3.final()]);
43
+ const decipher2 = crypto.createDecipheriv('aes-256-cbc', key, iv2);
44
+ let decrypted2 = decipher2.update(decrypted3);
45
+ decrypted2 = Buffer.concat([decrypted2, decipher2.final()]);
46
+ const decipher1 = crypto.createDecipheriv('aes-256-gcm', key, iv1);
47
+ decipher1.setAuthTag(tag1);
48
+ let decrypted1 = decipher1.update(decrypted2);
49
+ decrypted1 = Buffer.concat([decrypted1, decipher1.final()]);
50
50
 
51
- return decrypted1;
51
+ return decrypted1;
52
52
  };
@@ -3,11 +3,11 @@ const { HMAC_KEY } = require('../constants');
3
3
  const { reverseHash } = require('./math');
4
4
 
5
5
  exports.hash = (data) => {
6
- const hmac = crypto.createHmac('sha512', HMAC_KEY);
7
- hmac.update(data);
8
- const digest = hmac.digest();
6
+ const hmac = crypto.createHmac('sha512', HMAC_KEY);
7
+ hmac.update(data);
8
+ const digest = hmac.digest();
9
9
 
10
- return reverseHash(digest);
10
+ return reverseHash(digest);
11
11
  };
12
12
 
13
- exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
13
+ exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
@@ -3,14 +3,14 @@ const { PBKDF2_ITERATIONS, KEY_SIZE, PEPPER } = require('../constants');
3
3
  const { enhanceKey } = require('./math');
4
4
 
5
5
  exports.deriveKey = (password, salt) => {
6
- return new Promise((resolve, reject) => {
7
- const pepperedPassword = password + PEPPER;
8
- crypto.pbkdf2(pepperedPassword, salt, PBKDF2_ITERATIONS, KEY_SIZE, 'sha512', (err, key) => {
9
- if (err) reject(err);
6
+ return new Promise((resolve, reject) => {
7
+ const pepperedPassword = password + PEPPER;
8
+ crypto.pbkdf2(pepperedPassword, salt, PBKDF2_ITERATIONS, KEY_SIZE, 'sha512', (err, key) => {
9
+ if (err) reject(err);
10
10
 
11
- const enhancedKey = enhanceKey(key);
11
+ const enhancedKey = enhanceKey(key);
12
12
 
13
- resolve(enhancedKey);
14
- });
13
+ resolve(enhancedKey);
15
14
  });
16
- };
15
+ });
16
+ };
package/src/utils/math.js CHANGED
@@ -1,15 +1,14 @@
1
1
  exports.reverseBuffer = (data, reverse = false) => {
2
- if (reverse) {
3
- return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
4
- }
2
+ if (reverse) {
5
3
  return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
4
+ }
5
+ return Buffer.from(data.toString('hex').split('').reverse().join(''), 'hex');
6
6
  };
7
7
 
8
8
  exports.reverseHash = (hash) => {
9
- return Buffer.from(hash.toString('hex').split('').reverse().join(''), 'hex');
9
+ return Buffer.from(hash.toString('hex').split('').reverse().join(''), 'hex');
10
10
  };
11
11
 
12
12
  exports.enhanceKey = (key) => {
13
- return Buffer.from(key.toString('hex').split('').reverse().join(''), 'hex');
13
+ return Buffer.from(key.toString('hex').split('').reverse().join(''), 'hex');
14
14
  };
15
-