Socket
Socket
Sign inDemoInstall

@adonisjs/hash

Package Overview
Dependencies
Maintainers
2
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adonisjs/hash - npm Package Compare versions

Comparing version 1.0.8 to 1.0.9

build/standalone.d.ts

77

build/adonis-typings/hash.d.ts
declare module '@ioc:Adonis/Core/Hash' {
import { ManagerContract } from '@poppinss/manager';
/**
* Every driver must implement the Hash driver
* contract
*/
export interface HashDriverContract {

@@ -8,6 +12,20 @@ ids: string[];

};
/**
* Hash plain text value using the default mapping
*/
hash(value: string): Promise<string>;
/**
* Check the hash against the current config to find it needs
* to be re-hashed or not
*/
needsReHash(hashedValue: string): boolean;
/**
* Verify plain value against the hashed value to find if it's
* valid or not
*/
verify(hashedValue: string, plainValue: string): Promise<boolean>;
}
/**
* Shape of bcrypt config
*/
export type BcryptConfigContract = {

@@ -17,2 +35,5 @@ driver: 'bcrypt';

};
/**
* Bcrypt driver contract
*/
export interface BcryptContract extends HashDriverContract {

@@ -24,2 +45,5 @@ ids: ['bcrypt'];

}
/**
* Shape of argon2 config
*/
export type ArgonConfigContract = {

@@ -33,2 +57,5 @@ driver: 'argon2';

};
/**
* Argon2 driver contract
*/
export interface ArgonContract extends HashDriverContract {

@@ -42,16 +69,56 @@ ids: ['argon2d', 'argon2i', 'argon2id'];

}
export interface HashList {
/**
* Default list of available drivers. One can you reference this type
* to setup the `HashersList`.
*/
export type HashDrivers = {
bcrypt: {
config: BcryptConfigContract;
implementation: BcryptContract;
};
argon: {
config: ArgonConfigContract;
implementation: ArgonContract;
};
};
/**
* List of hash mappings used by the app. Using declaration merging, one
* must extend this interface.
*
* MUST BE SET IN THE USER LAND.
*/
export interface HashersList {
}
/**
* Shape of config accepted by the Hash module.
*/
export interface HashConfigContract {
default: keyof HashList;
default: keyof HashersList;
list: {
[P in keyof HashList]: HashList[P]['config'];
[P in keyof HashersList]: HashersList[P]['config'];
};
}
/**
* Piggy back on the driver method when driver exists, otherwise fallback to `never`
*/
export type DriverMethod<T, K extends keyof HashDriverContract> = T extends HashDriverContract ? HashDriverContract[K] : never;
export interface HashContract<DefaultDriver = HashList[HashConfigContract['default']]['implementation']> extends ManagerContract<HashDriverContract, {
[P in keyof HashList]: HashList[P]['implementation'];
/**
* Hash mananger interface
*/
export interface HashContract<DefaultDriver = HashersList[HashConfigContract['default']]['implementation']> extends ManagerContract<HashDriverContract, {
[P in keyof HashersList]: HashersList[P]['implementation'];
}> {
/**
* Hash plain text value using the default mapping
*/
hash(value: string): ReturnType<DriverMethod<DefaultDriver, 'hash'>>;
/**
* Verify plain value against the hashed value to find if it's
* valid or not
*/
verify(hashedValue: string, plainValue: string): ReturnType<DriverMethod<DefaultDriver, 'verify'>>;
/**
* Check the hash against the current config to find it needs
* to be re-hashed or not
*/
needsReHash(hashedValue: string): ReturnType<DriverMethod<DefaultDriver, 'needsReHash'>>;

@@ -58,0 +125,0 @@ }

@@ -0,1 +1,4 @@

/**
* @module @adonisjs/hash
*/
import { IocContract } from '@adonisjs/fold';

@@ -2,0 +5,0 @@ export default class HashProvider {

"use strict";
/**
* @module @adonisjs/hash
*/
Object.defineProperty(exports, "__esModule", { value: true });

@@ -3,0 +6,0 @@ const Hash_1 = require("../src/Hash");

/// <reference path="../../adonis-typings/hash.d.ts" />
import { ArgonConfigContract, ArgonContract } from '@ioc:Adonis/Core/Hash';
/**
* Hash driver built on top of argon hashing algorithm. The driver adheres
* to `phc` string format.
*/
export declare class Argon implements ArgonContract {
private _config;
private config;
/**
* A list of ids to find if hash belongs to this driver
* or not.
*/
ids: ArgonContract['ids'];
/**
* A list of params encoded to the hash value.
*/
params: ArgonContract['params'];
/**
* The current argon version in use
*/
version: number;
constructor(_config: ArgonConfigContract);
constructor(config: ArgonConfigContract);
/**
* Hash a value using argon algorithm. The options can be used to override
* default settings.
*/
hash(value: string): Promise<string>;
/**
* Verifies the hash against a plain value to find if it's
* a valid hash or not.
*/
verify(hashedValue: string, plainValue: string): Promise<boolean>;
/**
* Returns a boolean telling if the hash needs a rehash or not. The rehash is
* required when
*
* 1. The argon2 version is changed
* 2. Number of iterations are changed.
* 3. The memory value is changed.
* 4. The parellelism value is changed.
* 5. The argon variant is changed.
*/
needsReHash(value: string): boolean;
}
"use strict";
/*
* @adonisjs/hash
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -6,8 +14,20 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
/// <reference path="../../adonis-typings/hash.ts" />
const argon2_1 = __importDefault(require("@phc/argon2"));
const format_1 = __importDefault(require("@phc/format"));
/**
* Hash driver built on top of argon hashing algorithm. The driver adheres
* to `phc` string format.
*/
class Argon {
constructor(_config) {
this._config = _config;
constructor(config) {
this.config = config;
/**
* A list of ids to find if hash belongs to this driver
* or not.
*/
this.ids = ['argon2d', 'argon2i', 'argon2id'];
/**
* A list of params encoded to the hash value.
*/
this.params = {

@@ -18,10 +38,31 @@ iterations: 't',

};
/**
* The current argon version in use
*/
this.version = 19;
}
/**
* Hash a value using argon algorithm. The options can be used to override
* default settings.
*/
hash(value) {
return argon2_1.default.hash(value, this._config);
return argon2_1.default.hash(value, this.config);
}
/**
* Verifies the hash against a plain value to find if it's
* a valid hash or not.
*/
verify(hashedValue, plainValue) {
return argon2_1.default.verify(hashedValue, plainValue);
}
/**
* Returns a boolean telling if the hash needs a rehash or not. The rehash is
* required when
*
* 1. The argon2 version is changed
* 2. Number of iterations are changed.
* 3. The memory value is changed.
* 4. The parellelism value is changed.
* 5. The argon variant is changed.
*/
needsReHash(value) {

@@ -32,10 +73,19 @@ const deserialized = format_1.default.deserialize(value);

}
/**
* Version mis-match
*/
if (deserialized.version !== this.version) {
return true;
}
if (deserialized.id !== `argon2${this._config.variant}`) {
/**
* Variant mis-match
*/
if (deserialized.id !== `argon2${this.config.variant}`) {
return true;
}
/**
* Check for params mis-match
*/
return !!Object.keys(this.params).find((key) => {
return deserialized.params[this.params[key]] !== this._config[key];
return deserialized.params[this.params[key]] !== this.config[key];
});

@@ -42,0 +92,0 @@ }

/// <reference path="../../adonis-typings/hash.d.ts" />
import { BcryptConfigContract, BcryptContract } from '@ioc:Adonis/Core/Hash';
/**
* Generates and verifies hash using Bcrypt as underlying
* algorigthm.
*/
export declare class Bcrypt implements BcryptContract {
private _config;
private config;
ids: BcryptContract['ids'];
params: BcryptContract['params'];
version: number;
constructor(_config: BcryptConfigContract);
constructor(config: BcryptConfigContract);
/**
* Returns hash for a given value
*/
hash(value: string): Promise<string>;
/**
* Verify hash to know if two values are same.
*/
verify(hashedValue: string, plainValue: string): Promise<boolean>;
/**
* Returns a boolean telling if hash needs a rehash. Returns true when
* one of the original params have been changed.
*/
needsReHash(value: string): boolean;
}
"use strict";
/*
* @adonisjs/hash
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -6,7 +14,12 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
/// <reference path="../../adonis-typings/hash.ts" />
const format_1 = __importDefault(require("@phc/format"));
const bcrypt_1 = __importDefault(require("@phc/bcrypt"));
/**
* Generates and verifies hash using Bcrypt as underlying
* algorigthm.
*/
class Bcrypt {
constructor(_config) {
this._config = _config;
constructor(config) {
this.config = config;
this.ids = ['bcrypt'];

@@ -16,10 +29,23 @@ this.params = { rounds: 'r' };

}
/**
* Returns hash for a given value
*/
hash(value) {
return bcrypt_1.default.hash(value, this._config);
return bcrypt_1.default.hash(value, this.config);
}
/**
* Verify hash to know if two values are same.
*/
verify(hashedValue, plainValue) {
return bcrypt_1.default.verify(hashedValue, plainValue);
}
/**
* Returns a boolean telling if hash needs a rehash. Returns true when
* one of the original params have been changed.
*/
needsReHash(value) {
const deserialized = format_1.default.deserialize(value);
/**
* Phc formatted Bycrpt hash
*/
if (deserialized.id === 'bcrypt') {

@@ -30,5 +56,8 @@ if (this.version !== deserialized.version) {

return !!Object.keys(this.params).find((key) => {
return deserialized.params[this.params[key]] !== this._config[key];
return deserialized.params[this.params[key]] !== this.config[key];
});
}
/**
* Re-format non phc formatted bcrypt hashes.
*/
if (value.startsWith('$2b') || value.startsWith('$2a')) {

@@ -35,0 +64,0 @@ return true;

/// <reference path="../adonis-typings/hash.d.ts" />
import { Manager } from '@poppinss/manager';
import { HashContract, HashDriverContract, HashConfigContract, HashList } from '@ioc:Adonis/Core/Hash';
import { HashersList, HashContract, HashDriverContract, HashConfigContract } from '@ioc:Adonis/Core/Hash';
/**
* The Hash module exposes the API to hash values using an underlying
* Hash driver.
*/
export declare class Hash<Config extends HashConfigContract> extends Manager<HashDriverContract, {
[P in keyof HashList]: HashList[P]['implementation'];
[P in keyof HashersList]: HashersList[P]['implementation'];
}> implements HashContract<HashDriverContract> {

@@ -10,10 +14,36 @@ config: Config;

protected $cacheMappings: boolean;
/**
* Pulling the default driver name from the user config.
*/
protected getDefaultMappingName(): string;
/**
* Returns the config for a mapping
*/
protected getMappingConfig(name: string): any;
/**
* Returns the driver name for a mapping
*/
protected getMappingDriver(name: string): string | undefined;
protected createBcrypt(_mappingName: string, config: any): any;
protected createArgon2(_mappingName: string, config: any): any;
/**
* Creating bcrypt driver. The manager will call this method anytime
* someone will ask for the `bcrypt` driver.
*/
protected createBcrypt(_: string, config: any): any;
/**
* Creating argon driver. The manager will call this method anytime
* someone will ask for the `argon` driver.
*/
protected createArgon2(_: string, config: any): any;
/**
* Hash value using the default driver
*/
hash(value: string): never | any;
verify(hashedValue: string, plainValue: string): Promise<boolean>;
needsReHash(hashedValue: string): boolean;
/**
* Verify value using the default driver
*/
verify(hashedValue: string, plainValue: string): never;
/**
* Find if value needs to be re-hashed as per the default driver.
*/
needsReHash(hashedValue: string): never;
}
"use strict";
/*
* @adonisjs/hash
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference path="../adonis-typings/hash.ts" />
const manager_1 = require("@poppinss/manager");
/**
* The Hash module exposes the API to hash values using an underlying
* Hash driver.
*/
class Hash extends manager_1.Manager {

@@ -10,8 +23,17 @@ constructor(container, config) {

}
/**
* Pulling the default driver name from the user config.
*/
getDefaultMappingName() {
return this.config.default;
}
/**
* Returns the config for a mapping
*/
getMappingConfig(name) {
return this.config.list[name];
}
/**
* Returns the driver name for a mapping
*/
getMappingDriver(name) {

@@ -21,16 +43,33 @@ const config = this.getMappingConfig(name);

}
createBcrypt(_mappingName, config) {
/**
* Creating bcrypt driver. The manager will call this method anytime
* someone will ask for the `bcrypt` driver.
*/
createBcrypt(_, config) {
const { Bcrypt } = require('./Drivers/Bcrypt');
return new Bcrypt(config);
}
createArgon2(_mappingName, config) {
/**
* Creating argon driver. The manager will call this method anytime
* someone will ask for the `argon` driver.
*/
createArgon2(_, config) {
const { Argon } = require('./Drivers/Argon');
return new Argon(config);
}
/**
* Hash value using the default driver
*/
hash(value) {
return this.use().hash(value);
}
/**
* Verify value using the default driver
*/
verify(hashedValue, plainValue) {
return this.use().verify(hashedValue, plainValue);
}
/**
* Find if value needs to be re-hashed as per the default driver.
*/
needsReHash(hashedValue) {

@@ -37,0 +76,0 @@ return this.use().needsReHash(hashedValue);

4

build/templates/config/hash.txt

@@ -22,6 +22,6 @@ /**

|--------------------------------------------------------------------------
| Default mapping
| Default hasher
|--------------------------------------------------------------------------
|
| By default we make use of the bcrypt mapping to hash values. However, feel
| By default we make use of the bcrypt hasher to hash values. However, feel
| free to change the default value

@@ -28,0 +28,0 @@ |

@@ -9,12 +9,8 @@ /**

declare module '@ioc:Adonis/Core/Hash' {
interface HashList {
bcrypt: {
config: BcryptConfigContract,
implementation: BcryptContract,
},
argon: {
config: ArgonConfigContract,
implementation: ArgonContract,
}
import { HashDrivers } from '@ioc:Adonis/Core/Hash'
interface HashersList {
bcrypt: HashDrivers.bcrypt,
argon: HashDrivers.argon,
}
}
{
"name": "@adonisjs/hash",
"version": "1.0.8",
"version": "1.0.9",
"description": "Multi driver hash module with support for PHC string formats",
"main": "build/providers/HashProvider",
"files": [

@@ -17,3 +18,3 @@ "build/adonis-typings",

"test": "node japaFile.js",
"lint": "tslint --project tsconfig.json",
"lint": "eslint . --ext=.ts",
"clean": "del build",

@@ -43,7 +44,7 @@ "compile": "npm run lint && npm run clean && tsc",

"devDependencies": {
"@adonisjs/fold": "^6.1.9",
"@adonisjs/mrm-preset": "^2.1.0",
"@adonisjs/fold": "^6.2.3",
"@adonisjs/mrm-preset": "^2.2.3",
"@phc/argon2": "^1.0.9",
"@phc/bcrypt": "^1.0.2",
"@types/node": "^12.7.4",
"@types/node": "^12.12.21",
"commitizen": "^4.0.3",

@@ -54,13 +55,13 @@ "copyfiles": "^2.1.1",

"doctoc": "^1.4.0",
"husky": "^3.0.5",
"eslint": "^6.7.2",
"eslint-plugin-adonis": "^1.0.4",
"husky": "^3.1.0",
"japa": "^3.0.1",
"mrm": "^1.2.2",
"np": "^5.0.3",
"ts-node": "^8.3.0",
"tslint": "^5.19.0",
"tslint-eslint-rules": "^5.4.0",
"typedoc": "^0.15.0",
"mrm": "^2.0.2",
"np": "^5.2.1",
"ts-node": "^8.5.4",
"typedoc": "^0.15.5",
"typedoc-plugin-external-module-name": "^2.1.0",
"typedoc-plugin-markdown": "^2.1.11",
"typescript": "^3.6.2"
"typedoc-plugin-markdown": "^2.2.14",
"typescript": "^3.7.3"
},

@@ -92,3 +93,3 @@ "nyc": {

"@phc/format": "^0.5.0",
"@poppinss/manager": "^2.0.0"
"@poppinss/manager": "^2.1.3"
},

@@ -95,0 +96,0 @@ "adonisjs": {

@@ -10,3 +10,3 @@ <div align="center">

This module is used by [AdonisJs](https://adonisjs.com) to hash user password with first class support for upgrading logic. A big thanks to the author of [uphash](https://github.com/simonepri/upash), who inspired me to use [PHC string format](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md). I would have used uphash directly, but the user facing API is different from what I desire.
This module is used by [AdonisJs](https://adonisjs.com) to hash the user password with first class support for upgrading logic. A big thanks to the author of [uphash](https://github.com/simonepri/upash), who inspired me to use [PHC string format](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md). I would have used uphash directly, but the user facing API is different from what I desire.

@@ -22,3 +22,2 @@ <!-- START doctoc generated TOC please keep comment here to allow auto update -->

- [API Docs](#api-docs)
- [Maintainers](#maintainers)

@@ -56,7 +55,7 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Using with AdonisJs
The `@adonisjs/core` module includes this module by default. However, here's how you can set it up manually.
**The `@adonisjs/core` module includes this module by default**. However, here's how you can set it up manually.
```ts
const providers = [
'@adonisjs/hash/build/providers/HashProvider'
'@adonisjs/hash'
]

@@ -93,14 +92,12 @@ ```

## Maintainers
[Harminder virk](https://github.com/thetutlage)
[circleci-image]: https://img.shields.io/circleci/project/github/adonisjs/hash/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/adonisjs/hash "circleci"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[npm-image]: https://img.shields.io/npm/v/@adonisjs/hash.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@adonisjs/hash "npm"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[license-url]: LICENSE.md
[license-image]: https://img.shields.io/aur/license/pac.svg?style=for-the-badge
[license-image]: https://img.shields.io/npm/l/@adonisjs/hash?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc