cipher-fox/nestjs-cryptography

GitHub: cipher-fox/nestjs-cryptography

为 NestJS 框架提供安全、易用的密码学模块,封装了现代加密算法以简化数据保护流程并避免常见密码学误用。

Stars: 5 | Forks: 0

[![codecov](https://codecov.io/github/cipher-fox/nestjs-cryptography/branch/main/graph/badge.svg?token=I0ZFVZTREB)](https://codecov.io/github/mjorgegulab/nestjs-cryptography) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/42923e1b0a6a40269d8deb79e6957394)](https://app.codacy.com/gh/cipher-fox/nestjs-cryptography/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [![CodeQL](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/ec31129223185245.svg)](https://github.com/mjorgegulab/nestjs-cryptography/actions/workflows/github-code-scanning/codeql) [![Publish Wiki](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/59d571243e185246.svg)](https://nestjs-cryptography.cypher-fox.com) # NestJS - Cryptography

Nest Logo

## 快速开始 [概述与教程][6] ## 简介 创建这个库是为了解决在我们的项目中进行加密操作时遇到的一个常见问题。 它简化并优化了整个流程,使得实现安全且高效的加密解决方案变得更加容易。 此外,它还有助于避免常见的错误, 例如 _**[初始化向量的重复使用][1]**_, _**[加密密钥的重复使用][2]**_, 或者仅仅是使用了 _**[不具备密码学安全性的密钥][3]**_。 我们的库采用了现代密码学标准,以提供强大的安全性并保护您的数据免受高级威胁。我们使用了一套在密码学领域备受认可的可靠算法和实践: - **Argon2**:一种前沿的密钥派生函数,旨在抵御 GPU 和 ASIC 攻击,使其在对抗暴力破解时非常有效。它提供可配置的内存和时间成本,以在性能和安全性之间取得平衡。 - **SHA3**:Secure Hash Algorithm 家族的最新成员,SHA3 比其前身(SHA-1 和 SHA-2)提供了更高的安全性,并且能够抵御已知的密码学攻击。 - **AES-256-GCM**:在 Galois/Counter Mode 下使用 256 位密钥的 Advanced Encryption Standard,可确保数据的机密性和完整性。AES-256-GCM 因其高安全性和高性能而被广泛使用和信赖。 - **SHAKE256**:SHA-3 系列中一种多功能且可扩展输出长度的函数(XOF),SHAKE256 允许可变长度输出,使其适用于各种密码学应用,如密钥生成和哈希处理。 - **HKDF-SHA3-256**:一种基于 HMAC 的密钥派生函数,底层使用 SHA3-256 作为哈希函数。HKDF-SHA3-256 确保从主密钥中安全可靠地派生出密码学密钥。 - **HMAC-SHA3-256**:一种使用 SHA3-256 的消息身份验证机制。HMAC-SHA3-256 通过允许验证消息是否被篡改,来提供数据的完整性和真实性。 - **常量时间密钥比较**:为了防范时序攻击,我们的库在比较密钥时实现了常量时间算法。这意味着执行比较所需的时间不依赖于被比较的数据,从而防止攻击者根据执行时间推断出信息。 ## 安装 此包可在 [npm][4] 仓库中获取。 ``` yarn add nestjs-cryptography ``` 或者 ``` npm install nestjs-cryptography ``` ## 在服务中使用 要通过我们的 `CryptographyService` 访问加密方法,您可以使用标准的构造函数注入它 ``` import { Injectable } from '@nestjs/common'; import { CryptographyService } from 'nestjs-cryptography'; @Injectable() export class SomeService { constructor( // Inject using constructor injection private readonly cryptographyService: CryptographyService ) {} async someMethod(): Promise { // Access service methods return this.cryptographyService.genUUID(); } } ``` ## 配置 安装完成后,可以像配置任何其他 Nest 包一样,使用 _`forRoot`_ 或 _`forRootAsync`_ 方法来配置 `CryptographyModule`。 您可以在此处查看完整的可用设置[这里][5] ``` import { CryptographyModule, CryptographyOptionsInterface, } from 'nestjs-cryptography'; @Module({ imports: [ CryptographyModule.forRoot({ // The rest of the configuration encryption: { symmetric: { masterKey: '5f7f...46bf' } } }), ], }) export class AppModule {} ``` ``` import { CryptographyModule, CryptographyOptionsInterface, } from 'nestjs-cryptography'; @Module({ imports: [ CryptographyModule.forRootAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ // The rest of the configuration encryption: { symmetric: { masterKey: configService.get('CRYPTOGRAPHY.MASTER_KEY') } } }), inject: [ConfigService], }), ], }) export class AppModule {} ``` _`forRoot()`_ 和 _`forRootAsync`_ 方法接受一个选项对象作为参数。 这些选项会被传递给实例模块底层的加密操作。
标签:GNU通用公共许可证, MITM代理, NestJS, Node.js, 加密解密, 密码学, 开发库, 手动系统调用