@irfanshadikrishad/cipher 1.1.0 → 1.2.1

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
@@ -43,17 +43,18 @@ console.log(caesar.encrypt("hello world")) // Output: "nkrru cuxrj"
43
43
 
44
44
  This library provides implementations of various classical and modern ciphers:
45
45
 
46
- | Cipher | Type | Key required? | Strength | Used In/Notes |
47
- | --------------------------------------------------- | ---------------------------------------- | ------------- | -------- | --------------------------------------------------- |
48
- | [Caesar Cipher](/docs/en/ciphers/CAESAR.md) | Substitution | No | Low | Ancient Rome, Simple Obsfuscation |
49
- | [Atbash Cipher](/docs/en/ciphers/ATBASH.md) | Substitution | No | Low | Hebrew Cipher, Basic Encryption |
50
- | [Playfair Cipher](/docs/en/ciphers/PLAYFAIR.md) | Diagraph-based | Yes | Medium | Used in WWI & WWII |
51
- | [Vigenère Cipher](/docs/en/ciphers/VIGENERE.md) | Polyalphabetic | Yes | Medium | Used in Historical Documents |
52
- | [The Alphabet Cipher](/docs/en/ciphers/ALPHABET.md) | Polyalphabetic | Yes | Medium | Inspired by Vigenere, Cryptography Puzzles |
53
- | [Salsa20](/docs/en/ciphers/SALSA20.md) | Stream Cipher | Yes | High | Modern Cryptography, Secure Communications |
54
- | [ADFGVX](/docs/en/ciphers/ADFGVX.md) | Polybius Square + Columnar Transposition | Yes | Medium | Used in WWI, Known for 6x6 polybius square |
55
- | [AES](/docs/en/ciphers/AES.md) | Symmetric Block Cipher | Yes | High | Also known as, Rijndael |
56
- | [DES](/docs/en/ciphers/DES.md) | Symmetric Block Cipher | Yes | Medium | 56-bit key, Used in legacy systems, replaced by AES |
46
+ | Cipher | Type | Key required? | Strength | Used In/Notes |
47
+ | --------------------------------------------------- | ---------------------------------------- | ------------- | --------- | --------------------------------------------------- |
48
+ | [Caesar Cipher](/docs/en/ciphers/CAESAR.md) | Substitution | No | Low | Ancient Rome, Simple Obsfuscation |
49
+ | [Atbash Cipher](/docs/en/ciphers/ATBASH.md) | Substitution | No | Low | Hebrew Cipher, Basic Encryption |
50
+ | [Playfair Cipher](/docs/en/ciphers/PLAYFAIR.md) | Diagraph-based | Yes | Medium | Used in WWI & WWII |
51
+ | [Vigenère Cipher](/docs/en/ciphers/VIGENERE.md) | Polyalphabetic | Yes | Medium | Used in Historical Documents |
52
+ | [The Alphabet Cipher](/docs/en/ciphers/ALPHABET.md) | Polyalphabetic | Yes | Medium | Inspired by Vigenere, Cryptography Puzzles |
53
+ | [Salsa20](/docs/en/ciphers/SALSA20.md) | Stream Cipher | Yes | High | Modern Cryptography, Secure Communications |
54
+ | [ADFGVX](/docs/en/ciphers/ADFGVX.md) | Polybius Square + Columnar Transposition | Yes | Medium | Used in WWI, Known for 6x6 polybius square |
55
+ | [AES](/docs/en/ciphers/AES.md) | Symmetric Block Cipher | Yes | High | Also known as, Rijndael |
56
+ | [DES](/docs/en/ciphers/DES.md) | Symmetric Block Cipher | Yes | Medium | 56-bit key, Used in legacy systems, replaced by AES |
57
+ | [ECC](/docs/en/ciphers/ECC.md) | Asymmetric (Public-Key Cryptography) | Yes | Very High | Used in modern systems like Bitcoin, TLS, JWT, etc. |
57
58
 
58
59
  More ciphers coming soon...
59
60
 
package/dist/Cipher.d.ts CHANGED
@@ -7,6 +7,7 @@ import { Salsa20 } from "./ciphers/Salsa20.js";
7
7
  import { ADFGVX } from "./ciphers/ADFGVX.js";
8
8
  import { AES } from "./ciphers/AES.js";
9
9
  import { DES } from "./ciphers/DES.js";
10
+ import { ECC } from "./ciphers/ECC.js";
10
11
  export declare abstract class Cipher {
11
12
  /**
12
13
  * Caesar cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of places down the alphabet.
@@ -58,6 +59,10 @@ export declare abstract class Cipher {
58
59
  * @param iv 8 characters long
59
60
  */
60
61
  static DES: typeof DES;
61
- abstract encrypt(text: string): string;
62
- abstract decrypt(text: string): string;
62
+ /**
63
+ * Elliptic-curve cryptography (ECC) is an approach to public-key cryptography based on the algebraic structure of elliptic curves over finite fields.
64
+ */
65
+ static ECC: typeof ECC;
66
+ abstract encrypt(text: string): string | Promise<string>;
67
+ abstract decrypt(text: string): string | Promise<string>;
63
68
  }
@@ -78,12 +78,11 @@ export class ADFGVX extends Cipher {
78
78
  return this.columnarTranspose(polybiusText);
79
79
  }
80
80
  decrypt(text) {
81
- var _a;
82
81
  const encrypted = text.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
83
82
  const intermediate = this.columnarTranspose(encrypted, true);
84
- const polybiusText = ((_a = intermediate.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((char) => {
83
+ const polybiusText = intermediate.match(/.{1,2}/g)?.map((char) => {
85
84
  return this.reverseSquare.get(char) || "";
86
- })) || [];
85
+ }) || [];
87
86
  return polybiusText.join("");
88
87
  }
89
88
  }
@@ -0,0 +1,15 @@
1
+ import { Cipher } from "../Cipher.js";
2
+ export declare class ECC extends Cipher {
3
+ private recipientPublicKey?;
4
+ private ownPrivateKey?;
5
+ static recipientPublicKey: string;
6
+ static ownPrivateKey: string;
7
+ constructor(recipientPublicKey?: CryptoKey, ownPrivateKey?: CryptoKey);
8
+ static generate(): Promise<ECC>;
9
+ encrypt(plaintext: string): Promise<string>;
10
+ decrypt(data: string): Promise<string>;
11
+ exportPublicKey(): Promise<string>;
12
+ static importPublicKey(hex: string): Promise<CryptoKey>;
13
+ static bufToHex(buf: Uint8Array): string;
14
+ static hexToBuf(hex: string): Uint8Array;
15
+ }
@@ -0,0 +1,55 @@
1
+ import crypto from "crypto";
2
+ import { Cipher } from "../Cipher.js";
3
+ export class ECC extends Cipher {
4
+ constructor(recipientPublicKey, ownPrivateKey) {
5
+ super();
6
+ this.recipientPublicKey = recipientPublicKey;
7
+ this.ownPrivateKey = ownPrivateKey;
8
+ }
9
+ static async generate() {
10
+ const { publicKey, privateKey } = await crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, true, ["deriveKey", "deriveBits"]);
11
+ return new ECC(publicKey, privateKey);
12
+ }
13
+ async encrypt(plaintext) {
14
+ if (!this.recipientPublicKey)
15
+ throw new Error("Recipient public key not set");
16
+ const ephemeral = await ECC.generate();
17
+ const sharedKey = await crypto.subtle.deriveKey({ name: "ECDH", public: this.recipientPublicKey }, ephemeral.ownPrivateKey, { name: "AES-GCM", length: 256 }, false, ["encrypt"]);
18
+ const iv = crypto.getRandomValues(new Uint8Array(12));
19
+ const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, sharedKey, new TextEncoder().encode(plaintext));
20
+ const payload = {
21
+ iv: ECC.bufToHex(iv),
22
+ ciphertext: ECC.bufToHex(new Uint8Array(encrypted)),
23
+ ephemeralPublicHex: await ephemeral.exportPublicKey(),
24
+ };
25
+ return JSON.stringify(payload);
26
+ }
27
+ async decrypt(data) {
28
+ if (!this.ownPrivateKey)
29
+ throw new Error("Own private key not set");
30
+ const { iv, ciphertext, ephemeralPublicHex } = JSON.parse(data);
31
+ const ephemeralPubKey = await ECC.importPublicKey(ephemeralPublicHex);
32
+ const sharedKey = await crypto.subtle.deriveKey({ name: "ECDH", public: ephemeralPubKey }, this.ownPrivateKey, { name: "AES-GCM", length: 256 }, false, ["decrypt"]);
33
+ const decrypted = await crypto.subtle.decrypt({ name: "AES-GCM", iv: ECC.hexToBuf(iv) }, sharedKey, ECC.hexToBuf(ciphertext));
34
+ return new TextDecoder().decode(decrypted);
35
+ }
36
+ async exportPublicKey() {
37
+ const raw = await crypto.subtle.exportKey("raw", this.recipientPublicKey || this.ownPrivateKey);
38
+ return ECC.bufToHex(new Uint8Array(raw));
39
+ }
40
+ static async importPublicKey(hex) {
41
+ const raw = ECC.hexToBuf(hex);
42
+ return crypto.subtle.importKey("raw", raw, { name: "ECDH", namedCurve: "P-256" }, true, []);
43
+ }
44
+ // Helpers
45
+ static bufToHex(buf) {
46
+ return [...buf].map((b) => b.toString(16).padStart(2, "0")).join("");
47
+ }
48
+ static hexToBuf(hex) {
49
+ const bytes = new Uint8Array(hex.length / 2);
50
+ for (let i = 0; i < bytes.length; i++) {
51
+ bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
52
+ }
53
+ return bytes;
54
+ }
55
+ }
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ import { Salsa20 } from "./ciphers/Salsa20.js";
8
8
  import { ADFGVX } from "./ciphers/ADFGVX.js";
9
9
  import { AES } from "./ciphers/AES.js";
10
10
  import { DES } from "./ciphers/DES.js";
11
+ import { ECC } from "./ciphers/ECC.js";
11
12
  Cipher.Caesar = Caesar;
12
13
  Cipher.Atbash = Atbash;
13
14
  Cipher.Playfair = Playfair;
@@ -17,4 +18,5 @@ Cipher.Salsa20 = Salsa20;
17
18
  Cipher.ADFGVX = ADFGVX;
18
19
  Cipher.AES = AES;
19
20
  Cipher.DES = DES;
21
+ Cipher.ECC = ECC;
20
22
  export { Cipher };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@irfanshadikrishad/cipher",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "A versatile and secure cryptographic library for implementing various cipher algorithms in Node.js applications with zero/0 dependencies.",
5
5
  "homepage": "https://github.com/irfanshadikrishad/cipher#readme",
6
6
  "bugs": {
@@ -40,18 +40,18 @@
40
40
  "all": "npm run build && npm run format && npm run lint && npm run test"
41
41
  },
42
42
  "devDependencies": {
43
- "@eslint/js": "^9.25.1",
44
- "@types/jest": "^29.5.14",
45
- "@typescript-eslint/eslint-plugin": "^8.31.1",
46
- "@typescript-eslint/parser": "^8.31.1",
47
- "eslint": "^9.25.1",
48
- "globals": "^16.0.0",
43
+ "@eslint/js": "^9.29.0",
44
+ "@types/jest": "^30.0.0",
45
+ "@typescript-eslint/eslint-plugin": "^8.34.0",
46
+ "@typescript-eslint/parser": "^8.34.0",
47
+ "eslint": "^9.29.0",
48
+ "globals": "^16.2.0",
49
49
  "husky": "^9.1.7",
50
- "jest": "^29.7.0",
50
+ "jest": "^30.0.2",
51
51
  "prettier": "^3.5.3",
52
- "ts-jest": "^29.3.2",
52
+ "ts-jest": "^29.4.0",
53
53
  "typescript": "^5.8.3",
54
- "typescript-eslint": "^8.31.1"
54
+ "typescript-eslint": "^8.34.0"
55
55
  },
56
56
  "keywords": [
57
57
  "security",