k9crypt 1.0.5

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 ADDED
@@ -0,0 +1,36 @@
1
+ # K9Crypt Encryption Algorithm
2
+
3
+ This is a special encryption algorithm created for K9Crypt.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install k9crypt
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ const k9crypt = require('k9crypt');
15
+
16
+ async function test() {
17
+ const secretKey = 'VeryLongSecretKey!@#1234567890';
18
+ const encryptor = new k9crypt(secretKey);
19
+ const plaintext = 'Hello, World!';
20
+
21
+ try {
22
+ const encrypted = await encryptor.encrypt(plaintext);
23
+ console.log('Encrypted data:', encrypted);
24
+
25
+ const decrypted = await encryptor.decrypt(encrypted);
26
+ console.log('Decrypted data:', decrypted);
27
+ } catch (error) {
28
+ console.error('Encryption error:', error);
29
+ }
30
+ }
31
+
32
+ test();
33
+ ```
34
+
35
+ ## License
36
+ 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 ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "k9crypt",
3
+ "version": "1.0.5",
4
+ "description": "A special encryption algorithm created for K9Crypt.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "secure",
11
+ "encryption",
12
+ "k9crypt",
13
+ "crypto",
14
+ "crypto-js",
15
+ "bcrypt",
16
+ "decrypt"
17
+ ],
18
+ "author": "K9Crypt Team",
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "bcrypt": "^5.1.1",
22
+ "crypto-js": "^4.2.0",
23
+ "lz4": "^0.6.5",
24
+ "node-forge": "^1.3.1",
25
+ "xxhash": "^0.3.0"
26
+ },
27
+ "devDependencies": {},
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/K9Crypt/module.git"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/K9Crypt/module/issues"
34
+ },
35
+ "homepage": "https://k9crypt.xyz"
36
+ }
@@ -0,0 +1,10 @@
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!@#$%^&*()_+[]{}|;:,.<>?',
10
+ };
package/src/index.js ADDED
@@ -0,0 +1,54 @@
1
+ const crypto = require('crypto');
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 } = require('./constants');
7
+
8
+ class K9crypt {
9
+ constructor(secretKey) {
10
+ this.secretKey = secretKey;
11
+ }
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 { iv, encrypted, tag } = encrypt(compressed, key);
19
+ const dataToHash = Buffer.concat([salt, iv, encrypted, tag]);
20
+ const dataHash = hash(dataToHash);
21
+ const result = Buffer.concat([salt, iv, encrypted, tag, dataHash]);
22
+ return result.toString('base64');
23
+ } catch (error) {
24
+ console.error('Encryption error:', error);
25
+ throw new Error('Encryption failed');
26
+ }
27
+ }
28
+
29
+ async decrypt(ciphertext) {
30
+ try {
31
+ const data = Buffer.from(ciphertext, 'base64');
32
+ const salt = data.slice(0, SALT_SIZE);
33
+ const iv = data.slice(SALT_SIZE, SALT_SIZE + IV_SIZE);
34
+ const encrypted = data.slice(SALT_SIZE + IV_SIZE, -TAG_SIZE - 64);
35
+ const tag = data.slice(-TAG_SIZE - 64, -64);
36
+ const dataHash = data.slice(-64);
37
+
38
+ const dataToVerify = data.slice(0, -64);
39
+ if (!verifyHash(dataToVerify, dataHash)) {
40
+ throw new Error('Data integrity check failed');
41
+ }
42
+
43
+ const key = await deriveKey(this.secretKey, salt);
44
+ const decrypted = decrypt(encrypted, key, iv, tag);
45
+ const decompressed = await decompress(decrypted);
46
+ return decompressed.toString('utf8');
47
+ } catch (error) {
48
+ console.error('Decryption error:', error);
49
+ throw new Error('Decryption failed');
50
+ }
51
+ }
52
+ }
53
+
54
+ module.exports = K9crypt;
@@ -0,0 +1,19 @@
1
+ const zlib = require('zlib');
2
+
3
+ exports.compress = (data) => {
4
+ return new Promise((resolve, reject) => {
5
+ zlib.brotliCompress(Buffer.from(data, 'utf8'), (err, compressed) => {
6
+ if (err) reject(err);
7
+ else resolve(compressed);
8
+ });
9
+ });
10
+ };
11
+
12
+ exports.decompress = (data) => {
13
+ return new Promise((resolve, reject) => {
14
+ zlib.brotliDecompress(data, (err, decompressed) => {
15
+ if (err) reject(err);
16
+ else resolve(decompressed);
17
+ });
18
+ });
19
+ };
@@ -0,0 +1,19 @@
1
+ const crypto = require('crypto');
2
+ const { IV_SIZE } = require('../constants');
3
+
4
+ exports.encrypt = (data, key) => {
5
+ const iv = crypto.randomBytes(IV_SIZE);
6
+ const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
7
+ let encrypted = cipher.update(data);
8
+ encrypted = Buffer.concat([encrypted, cipher.final()]);
9
+ const tag = cipher.getAuthTag();
10
+ return { iv, encrypted, tag };
11
+ };
12
+
13
+ exports.decrypt = (encrypted, key, iv, tag) => {
14
+ const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
15
+ decipher.setAuthTag(tag);
16
+ let decrypted = decipher.update(encrypted);
17
+ decrypted = Buffer.concat([decrypted, decipher.final()]);
18
+ return decrypted;
19
+ };
@@ -0,0 +1,10 @@
1
+ const crypto = require('crypto');
2
+ const { HASH_SEED, HMAC_KEY } = require('../constants');
3
+
4
+ exports.hash = (data) => {
5
+ const hmac = crypto.createHmac('sha512', HMAC_KEY);
6
+ hmac.update(data);
7
+ return hmac.digest();
8
+ };
9
+
10
+ exports.verifyHash = (data, hash) => crypto.timingSafeEqual(exports.hash(data), hash);
@@ -0,0 +1,12 @@
1
+ const crypto = require('crypto');
2
+ const { PBKDF2_ITERATIONS, KEY_SIZE, PEPPER } = require('../constants');
3
+
4
+ exports.deriveKey = (password, salt) => {
5
+ return new Promise((resolve, reject) => {
6
+ const pepperedPassword = password + PEPPER;
7
+ crypto.pbkdf2(pepperedPassword, salt, PBKDF2_ITERATIONS, KEY_SIZE, 'sha512', (err, key) => {
8
+ if (err) reject(err);
9
+ resolve(key);
10
+ });
11
+ });
12
+ };