跳到主要内容

@nest-boot/hash

Classes

HashModule

Defined in: packages/hash/src/hash.module.ts:80

Module that provides password hashing services using Argon2.

Example

import { HashModule } from '@nest-boot/hash';

@Module({
imports: [
HashModule.register({
secret: process.env.HASH_SECRET
}),
],
})
export class AppModule {}

Generate a secure secret:

node -e "console.log(require('crypto').randomBytes(32).toString('base64url'))"

Extends

  • ConfigurableModuleClass

Indexable

[key: string]: any

Constructors

Constructor
new HashModule(): HashModule;

Defined in: node_modules/.pnpm/@nestjs+common@11.1.11/node_modules/@nestjs/common/module-utils/interfaces/configurable-module-cls.interface.d.ts:12

Returns

HashModule

Inherited from
ConfigurableModuleClass.constructor

Methods

register()
static register(options): DynamicModule;

Defined in: packages/hash/src/hash.module.ts:86

Registers the HashModule with the given options.

Parameters
ParameterTypeDescription
optionsHashModuleOptions & Partial<{ }>Configuration options including secret and isGlobal
Returns

DynamicModule

Dynamic module configuration

Overrides
ConfigurableModuleClass.register
registerAsync()
static registerAsync(options): DynamicModule;

Defined in: packages/hash/src/hash.module.ts:95

Registers the HashModule asynchronously with factory functions.

Parameters
ParameterTypeDescription
optionsConfigurableModuleAsyncOptions<HashModuleOptions, "create"> & Partial<{ }>Async configuration options
Returns

DynamicModule

Dynamic module configuration

Overrides
ConfigurableModuleClass.registerAsync

HashService

Defined in: packages/hash/src/hash.service.ts:19

Service that provides password hashing and verification using Argon2 algorithm.

Example

import { HashService } from '@nest-boot/hash';

// Initialize at application startup
HashService.init(process.env.HASH_SECRET);

// Use static methods
const hashed = await HashService.hash(password);
const isValid = await HashService.verify(hashed, password);

Constructors

Constructor
new HashService(secret?): HashService;

Defined in: packages/hash/src/hash.service.ts:111

Creates an instance of HashService.

Parameters
ParameterTypeDescription
secret?stringOptional secret key to use for hashing
Returns

HashService

Accessors

instance
Get Signature
get static instance(): HashService;

Defined in: packages/hash/src/hash.service.ts:27

Gets the static HashService instance.

Throws

Error if HashService has not been initialized via init()

Returns

HashService

The HashService instance

Methods

create()
create(value, secret?): Promise<string>;

Defined in: packages/hash/src/hash.service.ts:122

Creates a hash from the given value using Argon2.

Parameters
ParameterTypeDescription
valuestring | Buffer<ArrayBufferLike>The value to hash (password or other sensitive data)
secret?stringOptional secret key to use instead of the default
Returns

Promise<string>

The hashed string

Deprecated

Use hash instead

hash()
hash(value, secret?): Promise<string>;

Defined in: packages/hash/src/hash.service.ts:132

Creates a hash from the given value using Argon2.

Parameters
ParameterTypeDescription
valuestring | Buffer<ArrayBufferLike>The value to hash (password or other sensitive data)
secret?stringOptional secret key to use instead of the default
Returns

Promise<string>

The hashed string

hashSync()
hashSync(value, secret?): string;

Defined in: packages/hash/src/hash.service.ts:144

Creates a hash synchronously from the given value using Argon2.

Parameters
ParameterTypeDescription
valuestring | Buffer<ArrayBufferLike>The value to hash (password or other sensitive data)
secret?stringOptional secret key to use instead of the default
Returns

string

The hashed string

verify()
verify(
hashed,
value,
secret?): Promise<boolean>;

Defined in: packages/hash/src/hash.service.ts:157

Verifies a value against a hash.

Parameters
ParameterTypeDescription
hashedstring | Buffer<ArrayBufferLike>The hash to verify against
valuestring | Buffer<ArrayBufferLike>The value to verify
secret?stringOptional secret key to use instead of the default
Returns

Promise<boolean>

True if the value matches the hash, false otherwise

verifySync()
verifySync(
hashed,
value,
secret?): boolean;

Defined in: packages/hash/src/hash.service.ts:174

Verifies a value against a hash synchronously.

Parameters
ParameterTypeDescription
hashedstring | Buffer<ArrayBufferLike>The hash to verify against
valuestring | Buffer<ArrayBufferLike>The value to verify
secret?stringOptional secret key to use instead of the default
Returns

boolean

True if the value matches the hash, false otherwise

hash()
static hash(value, secret?): Promise<string>;

Defined in: packages/hash/src/hash.service.ts:58

Creates a hash from the given value using the static instance.

Parameters
ParameterTypeDescription
valuestring | Buffer<ArrayBufferLike>The value to hash (password or other sensitive data)
secret?stringOptional secret key to use instead of the default
Returns

Promise<string>

The hashed string

Throws

Error if HashService has not been initialized via init()

hashSync()
static hashSync(value, secret?): string;

Defined in: packages/hash/src/hash.service.ts:69

Creates a hash synchronously from the given value using the static instance.

Parameters
ParameterTypeDescription
valuestring | Buffer<ArrayBufferLike>The value to hash (password or other sensitive data)
secret?stringOptional secret key to use instead of the default
Returns

string

The hashed string

Throws

Error if HashService has not been initialized via init()

init()
static init(secret?): void;

Defined in: packages/hash/src/hash.service.ts:47

Initializes the static HashService instance with the given secret. Call this method at application startup to configure the default secret.

Parameters
ParameterTypeDescription
secret?stringThe secret key to use for hashing
Returns

void

Example
// In your application bootstrap
HashService.init(process.env.HASH_SECRET);
verify()
static verify(
hashed,
value,
secret?): Promise<boolean>;

Defined in: packages/hash/src/hash.service.ts:81

Verifies a value against a hash using the static instance.

Parameters
ParameterTypeDescription
hashedstring | Buffer<ArrayBufferLike>The hash to verify against
valuestring | Buffer<ArrayBufferLike>The value to verify
secret?stringOptional secret key to use instead of the default
Returns

Promise<boolean>

True if the value matches the hash, false otherwise

Throws

Error if HashService has not been initialized via init()

verifySync()
static verifySync(
hashed,
value,
secret?): boolean;

Defined in: packages/hash/src/hash.service.ts:97

Verifies a value against a hash synchronously using the static instance.

Parameters
ParameterTypeDescription
hashedstring | Buffer<ArrayBufferLike>The hash to verify against
valuestring | Buffer<ArrayBufferLike>The value to verify
secret?stringOptional secret key to use instead of the default
Returns

boolean

True if the value matches the hash, false otherwise

Throws

Error if HashService has not been initialized via init()

Interfaces

HashModuleOptions

Defined in: packages/hash/src/hash-module-options.interface.ts:4

Configuration options for the HashModule.

Properties

secret?
optional secret: string;

Defined in: packages/hash/src/hash-module-options.interface.ts:9

The secret key used for hashing. If not provided, falls back to HASH_SECRET or APP_SECRET environment variables.