{"id":17973325,"url":"https://github.com/pramsh/cipher","last_synced_at":"2026-05-16T22:31:27.644Z","repository":{"id":259544092,"uuid":"878160510","full_name":"Pramsh/Cipher","owner":"Pramsh","description":"Cipher is a Node.js cryptographic library offering SHA-256 hashing, AES-256 encryption/decryption, JWT creation/validation, and RSA key pair generation. ","archived":false,"fork":false,"pushed_at":"2024-10-24T23:14:18.000Z","size":49,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-29T17:26:25.675Z","etag":null,"topics":["cryptography","digital-signature","javascript","jwt-auth","jwt-token","jwt-tokens","nodejs","rsa-key-pair","rsa-signature","sha256-crypt","sha256-decryptor","sha256-hash"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pramsh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-24T21:40:37.000Z","updated_at":"2024-11-09T20:46:12.000Z","dependencies_parsed_at":"2024-10-26T11:33:07.425Z","dependency_job_id":"827941ae-69e8-40d2-ab44-b659ffadb37f","html_url":"https://github.com/Pramsh/Cipher","commit_stats":null,"previous_names":["pramsh/cipher"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pramsh%2FCipher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pramsh%2FCipher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pramsh%2FCipher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pramsh%2FCipher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pramsh","download_url":"https://codeload.github.com/Pramsh/Cipher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230049542,"owners_count":18164902,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","digital-signature","javascript","jwt-auth","jwt-token","jwt-tokens","nodejs","rsa-key-pair","rsa-signature","sha256-crypt","sha256-decryptor","sha256-hash"],"created_at":"2024-10-29T16:30:27.534Z","updated_at":"2026-05-16T22:31:27.608Z","avatar_url":"https://github.com/Pramsh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cipher Class\n\nThis `Cipher` class provides various cryptographic functionalities using Node.js's built-in `crypto` module. It supports hashing, JWT creation and validation, RSA key pair generation, and AES-256 encryption and decryption.\n\n## Installation\n\nTo use this class, ensure you have Node.js installed.\n\n## Usage\n\n### Importing the Class\n\n```javascript\nconst { Cipher } = require('./path/to/cipher');\n```\n\n### Initialization\n\nCreate an instance of the `Cipher` class by providing the necessary keys and tokens.\n\n```javascript\nconst cipher = new Cipher(aes256Key, aes256Iv, appKey, appToken);\n```\n\n### Methods\n\n#### `hash(input, [algorithm='sha256'])`\n\nGenerates a hash of the input using the specified algorithm.\n\n```javascript\nconst hash = cipher.hash('your-input-string', 'sha256');\n```\n\n#### `CheckClientHeaders(clientAppKey, clientAppToken)`\n\nChecks the validity of client headers.\n\n```javascript\ncipher.CheckClientHeaders(clientAppKey, clientAppToken)\n    .then(isValid =\u003e console.log(isValid))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `createJWT(payload, jwtPrivateKey, [validationInputPromise])`\n\nCreates a JWT with the given payload and private key. Optionally, a validation promise can be provided.\n\n```javascript\ncipher.createJWT(payload, jwtPrivateKey, validationInputPromise)\n    .then(token =\u003e console.log(token))\n    .catch(error =\u003e console.error(error));\n```\n#### `getSessionData(JWTtoken)`\n\nGets session data from a JWT.\n\n```javascript\ncipher.getSessionData(JWTtoken)\n    .then(sessionData =\u003e console.log(sessionData))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `validateJWT(ip, jwt, jwtpublickey)`\n\nValidates a JWT with the given IP and public key.\n\n```javascript\ncipher.validateJWT(ip, jwt, jwtpublickey, validationInputPromise)\n    .then(isValid =\u003e console.log(isValid))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `RSAGenerateKeyPair([encodingPrivateKeyType], [encodingFormatBoth], [encodingPublicKeyType], [bitLength])`\n\nGenerates an RSA key pair.\n\n```javascript\ncipher.RSAGenerateKeyPair()\n    .then(([publicKey, privateKey]) =\u003e console.log(publicKey, privateKey))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `RSAsignDocument(privateKey, dataToSign, [encodingFormat], [encodingType])`\n\nSigns data using RSA.\n\n```javascript\ncipher.RSAsignDocument(privateKey, dataToSign)\n    .then(signature =\u003e console.log(signature))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `RSAverifySignature(publicKey, dataToVerify, signature, [encodingFormat], [publicKeyType])`\n\nVerifies an RSA signature.\n\n```javascript\ncipher.RSAverifySignature(publicKey, dataToVerify, signature)\n    .then(isValid =\u003e console.log(isValid))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `AES256encrypt(text, salt)`\n\nEncrypts text using AES-256-CBC.\n\n```javascript\ncipher.AES256encrypt('your-text', 'your-salt')\n    .then(encryptedText =\u003e console.log(encryptedText))\n    .catch(error =\u003e console.error(error));\n```\n\n#### `AES256decrypt(cryptedText, salt)`\n\nDecrypts text using AES-256-CBC.\n\n```javascript\ncipher.AES256decrypt(encryptedText, 'your-salt')\n    .then(decryptedText =\u003e console.log(decryptedText))\n    .catch(error =\u003e console.error(error));\n```\n\n\n## Detailed Explanation of Salt Usage in AES256encrypt\n\nThe `AES256encrypt` method uses a salt to derive the key and IV for AES-256 encryption, ensuring unique and secure encryption for each salt value.\n\n### Key and IV Derivation\n\nThe key and IV are derived using the `pbkdf2Sync` function from the `crypto` module with the following parameters:\n- `this.#aes256Key`: AES-256 key.\n- `this.#aes256Iv`: AES-256 IV.\n- `salt`: Salt value.\n- `100000`: Iterations for PBKDF2.\n- `32`: Key length in bytes.\n- `16`: IV length in bytes.\n- `'sha512'`: Hash function.\n\n### Encryption Process\n\nThe `AES256encrypt` method uses the derived key and IV to perform AES-256-CBC encryption:\n- `text`: Text to encrypt.\n- `salt`: Salt value.\n- `createCipheriv('aes-256-cbc', key, iv)`: Creates a cipher object.\n- `cipher.update(text, 'utf-8', 'hex') + cipher.final('hex')`: Encrypts the text.\n\n### Example Usage\n\n```javascript\nconst aes256Key = 'your-aes256-key';\nconst aes256Iv = 'your-aes256-iv';\nconst cipher = new Cipher(aes256Key, aes256Iv);\n\nconst textToEncrypt = 'Hello, World!';\nconst salt = 'random_salt';\n\ncipher.AES256encrypt(textToEncrypt, salt)\n    .then(encryptedText =\u003e console.log(encryptedText))\n    .catch(error =\u003e console.error(error));\n```\n\nUsing a unique salt for each encryption ensures different derived keys and IVs, enhancing security.\n\n\n## License\n\nThis project is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpramsh%2Fcipher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpramsh%2Fcipher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpramsh%2Fcipher/lists"}