Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
baryon2 committed Jun 13, 2024
1 parent 117fb04 commit 143c5cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
42 changes: 21 additions & 21 deletions src/encryption-utils/encryption-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//import CryptoJS from 'crypto-js';
import { cbc } from '@noble/ciphers/aes'
import { pbkdf2 } from '@noble/hashes/pbkdf2'
import { randomBytes } from '@noble/ciphers/webcrypto'
import { sha1 } from '@noble/hashes/sha1'
import { base64, hex } from '@scure/base'
import { cbc } from '@noble/ciphers/aes';
import { pbkdf2 } from '@noble/hashes/pbkdf2';
import { randomBytes } from '@noble/ciphers/webcrypto';
import { sha1 } from '@noble/hashes/sha1';
import { base64, hex } from '@scure/base';

const keySize = 256;

Expand Down Expand Up @@ -46,25 +46,25 @@ const new_iterations = 10_000;
// };

export const encrypt = (msg: string, pass: string, iterations?: number) => {
const salt = randomBytes(128 / 8)
const key = pbkdf2(sha1, pass, salt, { c: iterations ?? new_iterations, dkLen: keySize / 8 })
const iv = randomBytes(128 / 8)
const stream = cbc(key, iv)
const encoder = new TextEncoder()
const encrypted = stream.encrypt(encoder.encode(msg))
const saltString = hex.encode(salt)
const ivString = hex.encode(iv)
const encryptedString = base64.encode(encrypted)
return saltString + ivString + encryptedString
}
const salt = randomBytes(128 / 8);
const key = pbkdf2(sha1, pass, salt, { c: iterations ?? new_iterations, dkLen: keySize / 8 });
const iv = randomBytes(128 / 8);
const stream = cbc(key, iv);
const encoder = new TextEncoder();
const encrypted = stream.encrypt(encoder.encode(msg));
const saltString = hex.encode(salt);
const ivString = hex.encode(iv);
const encryptedString = base64.encode(encrypted);
return saltString + ivString + encryptedString;
};

export const decrypt = (transitmessage: string, pass: string, iterations?: number): string => {
const salt = hex.decode(transitmessage.substring(0, 32));
const iv = hex.decode(transitmessage.substring(32, 64));
const encrypted = base64.decode(transitmessage.substring(64));

const key = pbkdf2(sha1, pass, salt, { c: iterations ?? new_iterations, dkLen: keySize / 8 })
const stream = cbc(key, iv)
const decrypted = stream.decrypt(encrypted)
return new TextDecoder().decode(decrypted)
}
const key = pbkdf2(sha1, pass, salt, { c: iterations ?? new_iterations, dkLen: keySize / 8 });
const stream = cbc(key, iv);
const decrypted = stream.decrypt(encrypted);
return new TextDecoder().decode(decrypted);
};
6 changes: 2 additions & 4 deletions tests/encryption-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { decrypt, encrypt } from '../src';

describe('encryption-utils', () => {

test('encrypt-decrypt-2', () => {
const originalMessage = 'Hello World';
const cipher = encrypt(originalMessage, 'password');
const plain = decrypt(cipher, 'password');
expect(plain).toBe(originalMessage);
})

});

test('test with non ascii characters', () => {
const originalMessage = 'Hello World! 🐸';
const cipher = encrypt(originalMessage, 'password');
const plain = decrypt(cipher, 'password');
expect(plain).toBe(originalMessage);
})
});
});

0 comments on commit 143c5cf

Please sign in to comment.