From 143c5cf0b2547ba15b3a8254dcc628075735dd7f Mon Sep 17 00:00:00 2001 From: baryon2 Date: Thu, 13 Jun 2024 11:25:58 +0530 Subject: [PATCH] Fix linting issues --- src/encryption-utils/encryption-utils.ts | 42 ++++++++++++------------ tests/encryption-utils.test.ts | 6 ++-- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/encryption-utils/encryption-utils.ts b/src/encryption-utils/encryption-utils.ts index 597e8d9..9bd472e 100644 --- a/src/encryption-utils/encryption-utils.ts +++ b/src/encryption-utils/encryption-utils.ts @@ -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; @@ -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); +}; diff --git a/tests/encryption-utils.test.ts b/tests/encryption-utils.test.ts index 726752a..0493174 100644 --- a/tests/encryption-utils.test.ts +++ b/tests/encryption-utils.test.ts @@ -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); - }) + }); });