{"id":20424048,"url":"https://github.com/nhedger/nestjs-encryption","last_synced_at":"2025-04-12T18:42:13.816Z","repository":{"id":196347926,"uuid":"682464240","full_name":"nhedger/nestjs-encryption","owner":"nhedger","description":"Encryption module for NestJS 🔑","archived":false,"fork":false,"pushed_at":"2023-08-25T03:50:38.000Z","size":64,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2024-05-01T17:22:14.724Z","etag":null,"topics":["encryption","nestjs","nestjs-module"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@hedger/nestjs-encryption","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nhedger.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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":"2023-08-24T08:19:16.000Z","updated_at":"2024-03-10T04:53:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"a5c7e3a0-5257-49b3-98a8-cd7c94765be6","html_url":"https://github.com/nhedger/nestjs-encryption","commit_stats":null,"previous_names":["nhedger/nestjs-encryption"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhedger%2Fnestjs-encryption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhedger%2Fnestjs-encryption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhedger%2Fnestjs-encryption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhedger%2Fnestjs-encryption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nhedger","download_url":"https://codeload.github.com/nhedger/nestjs-encryption/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248617078,"owners_count":21134185,"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":["encryption","nestjs","nestjs-module"],"created_at":"2024-11-15T07:08:32.582Z","updated_at":"2025-04-12T18:42:13.810Z","avatar_url":"https://github.com/nhedger.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NestJS Encryption Module\n\n**NestJS Encrytion** is a NestJS 9+ module that provides _plug-and-play_ encryption\nand decryption functionality to your NestJS application.\n\n-   Uses `aes-256-cbc` by default, but supports [other ciphers](#supported-ciphers) as well.\n-   Provides a keygen (API and CLI) for generating random and secure encryption keys.\n-   Thoroughly tested.\n\n## Installation\n\n**NestJS Encryption** can be installed with your favorite package manager.\n\n```bash\n# NPM\nnpm install @hedger/nestjs-encryption\n\n# Yarn\nyarn add @hedger/nestjs-encryption\n\n# PNPM\npnpm add @hedger/nestjs-encryption\n```\n\n## Setup\n\nSetting up the module inside your NestJS application is a matter of registering\nthe module within your `AppModule`. The module is registered globally by default\nand can be used anywhere in your application.\n\nYou may use either the `forRoot` or `forRootAsync` method to register the module in your `AppModule`.\n\n### Using `forRoot`\n\nThe `forRoot` method is the simplest way to register the module.\n\n```typescript\nimport { EncryptionModule, Cipher } from \"@hedger/nestjs-encryption\";\n\n@Module({\n\timports: [\n\t\tEncryptionModule.forRoot({\n\t\t\tkey: process.env.APP_KEY,\n\t\t\tcipher: Cipher.AES_256_CBC,\n\t\t}),\n\t],\n})\nexport class AppModule {}\n```\n\n### Using `forRootAsync`\n\nThe `forRootAsync` method allows you to register the module asynchronously,\noptionally resolving the encryption key from a configuration service. Here's\nan example that uses the `ConfigService` from `@nestjs/config` to resolve the\nencryption key from the `APP_KEY` environment variable.\n\n```typescript\nimport { ConfigModule, ConfigService } from \"@nestjs/config\";\nimport { EncryptionModule, Cipher } from \"@hedger/nestjs-encryption\";\n\n@Module({\n\timports: [\n\t\tConfigModule.forRoot({\n\t\t\tisGlobal: true,\n\t\t}),\n\t\tEncryptionModule.forRootAsync({\n\t\t\tuseFactory: (configService: ConfigService) =\u003e ({\n\t\t\t\tkey: configService.get\u003cstring\u003e(\"APP_KEY\"),\n\t\t\t}),\n\t\t\tinject: [ConfigService],\n\t\t}),\n\t],\n\tcontrollers: [AppController],\n\tproviders: [AppService],\n})\nexport class AppModule {}\n```\n\n## Usage\n\nInject the EncryptionService in your service or controller.\n\n```typescript\nimport { EncryptionService } from \"@hedger/nestjs-encryption\";\n\n@Injectable()\nexport class FooService {\n\tconstructor(private readonly crypto: EncryptionService) {}\n\n\tsomeMethod() {\n\t\tconst encrypted = this.crypto.encrypt(\"some value\");\n\t\tconst decrypted = this.crypto.decrypt(encrypted);\n\t}\n}\n```\n\n## Encryption key\n\nThis package expects the encryption key to be a base64-encoded string of N random\nbytes, where N is the key length of the cipher you're using. For example, the\n`aes-256-cbc` cipher has a key length of 32 bytes, so the encryption key must\nbe a base64-encoded string of 32 random bytes.\n\n### Generating a key\n\nThis package provides CLI utility for generating random and secure encryption\nkeys.\n\n```bash\n# Generates a random key for the aes-256-cbc cipher (default)\nnpm exec nestjs-encryption-keygen\n```\n\nBy default, the keygen generates keys for the `aes-256-cbc` cipher. You may\nspecify a different cipher by passing the `--cipher` option.\n\n```bash\n# Generates a random key for the aes-128-cbc cipher\nnpm exec nestjs-encryption-keygen --cipher aes-128-cbc\n```\n\nSee the [Supported ciphers](#supported-ciphers) section for a list of supported\nciphers.\n\n### Generating a key programmatically\n\nRandom and secure encryption keys may also be generated programmatically by\ncalling the `generateKey` method on the `EncryptionService` class.\n\n```typescript\nimport { Cipher, EncryptionService } from \"@hedger/nestjs-encryption\";\n\n// Pass the desired cipher as the first argument.\nconst key = EncryptionService.generateKey(Cipher.AES_256_CBC);\n```\n\n## Supported ciphers\n\nThe following ciphers are supported by this package.\n\n-   `aes-256-cbc` (default)\n-   `aes-256-gcm`\n-   `aes-128-cbc`\n-   `aes-128-gcm`\n\n## License\n\nCopyright © 2023, [Nicolas Hedger](https://github.com/nhedger). Released under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhedger%2Fnestjs-encryption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnhedger%2Fnestjs-encryption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhedger%2Fnestjs-encryption/lists"}