New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@alwatr/crypto

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alwatr/crypto - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

api.d.ts

18

CHANGELOG.md

@@ -6,2 +6,20 @@ # Change Log

# [3.0.0](https://github.com/Alwatr/alwatr-es-sdk/compare/@alwatr/crypto@2.0.0...@alwatr/crypto@3.0.0) (2023-12-09)
### Bug Fixes
* **crypto:** use import type ([30e3bac](https://github.com/Alwatr/alwatr-es-sdk/commit/30e3bacb187d58417cb62e2a1511de4ade3f80c0)) by @njfamirm
### Features
* **crypto/api:** AlwatrCryptoFactory with secret and device id ([2d754a1](https://github.com/Alwatr/alwatr-es-sdk/commit/2d754a19b2f04f64d0828e31ba004fc192f466d3)) by @njfamirm
* **crypto/api:** device id generator preconfig ([71e80d6](https://github.com/Alwatr/alwatr-es-sdk/commit/71e80d63743579505a6be17d014c364e9f6cf55c)) by @njfamirm
* **crypto:** complete rewrite with new api ([3d56861](https://github.com/Alwatr/alwatr-es-sdk/commit/3d56861a2857e760c7cd5f03be98f003738fc7a9)) by @AliMD
* **crypto:** rename user file to api ([52343ea](https://github.com/Alwatr/alwatr-es-sdk/commit/52343ead04c23f50bedac2caa01f46bf489ab318)) by @njfamirm
### BREAKING CHANGES
* **crypto:** new api
* **crypto/api:** rename AlwatrUserGenerator to AlwatrCryptoFactory and change config
# [2.0.0](https://github.com/Alwatr/eslib/compare/@alwatr/crypto@1.2.1...@alwatr/crypto@2.0.0) (2023-11-29)

@@ -8,0 +26,0 @@

25

hash.d.ts
/// <reference types="node" />
import { type BinaryLike } from 'node:crypto';
import type { HashGeneratorConfig } from './type.js';
import type { CryptoAlgorithm, CryptoEncoding } from './type.js';
/**
* Represents the configuration for a hash generator.
*/
export interface HashGeneratorConfig {
/**
* The prefix to be added to the generated hash.
*/
prefix: string;
/**
* The algorithm used for hashing.
*/
algorithm: CryptoAlgorithm;
/**
* The encoding used for the generated hash.
*/
encoding: CryptoEncoding;
/**
* The length of the CRC (Cyclic Redundancy Check) value.
*/
crcLength: number;
}
/**
* Secure **self-validate** hash generator.

@@ -78,3 +99,3 @@ */

* if (!hashGenerator.verifySelfValidate(hash)) {
* new Error('invalid_user');
* new Error('invalid_hash');
* }

@@ -81,0 +102,0 @@ * ```

6

hash.js

@@ -92,3 +92,3 @@ import { createHash, randomBytes } from 'node:crypto';

* if (!hashGenerator.verifySelfValidate(hash)) {
* new Error('invalid_user');
* new Error('invalid_hash');
* }

@@ -98,5 +98,3 @@ * ```

verifySelfValidate(hash) {
const gapPos = this.config.crcLength == null || this.config.crcLength < 1
? hash.length - (hash.length - this.config.prefix.length) / 2
: hash.length - this.config.crcLength;
const gapPos = hash.length - this.config.crcLength;
const mainHash = hash.slice(0, gapPos);

@@ -103,0 +101,0 @@ const crcHash = hash.slice(gapPos);

export * from './hash.js';
export * from './token.js';
export * from './user.js';
export * from './api.js';
export * from './type.js';
export * from './pre-config.js';
//# sourceMappingURL=index.d.ts.map
import { definePackage } from '@alwatr/logger';
export * from './hash.js';
export * from './token.js';
export * from './user.js';
export * from './api.js';
export * from './type.js';
export * from './pre-config.js';
definePackage('crypto', '1.x');
definePackage('crypto', '3.x');
//# sourceMappingURL=index.js.map
{
"name": "@alwatr/crypto",
"version": "2.0.0",
"version": "3.0.0",
"description": "A robust generator of secure authentication HOTP tokens, employing the HMAC-based One-Time Password algorithm, accompanied by a suite of cryptographic utilities, all encapsulated within a compact TypeScript module.",

@@ -35,18 +35,18 @@ "keywords": [

"type": "git",
"url": "https://github.com/Alwatr/eslib",
"url": "https://github.com/Alwatr/alwatr-es-sdk",
"directory": "packages/crypto"
},
"homepage": "https://github.com/Alwatr/eslib/tree/next/packages/crypto#readme",
"homepage": "https://github.com/Alwatr/alwatr-es-sdk/tree/next/packages/crypto#readme",
"bugs": {
"url": "https://github.com/Alwatr/eslib/issues"
"url": "https://github.com/Alwatr/alwatr-es-sdk/issues"
},
"dependencies": {
"@alwatr/logger": "^2.3.1",
"@alwatr/math": "^1.2.2",
"@alwatr/util": "^1.3.2"
"@alwatr/logger": "^2.3.2",
"@alwatr/math": "^1.2.3",
"@alwatr/util": "^1.3.3"
},
"devDependencies": {
"@types/node": "^20.10.0"
"@types/node": "^20.10.4"
},
"gitHead": "27cb935580d5ccdc4459f1018c66f23ea0a42ddf"
"gitHead": "932c439c39ad9aa340cf53e0704cece565d68326"
}

@@ -1,14 +0,19 @@

import { HashGeneratorConfig, TokenGeneratorConfig } from './type.js';
import type { HashGeneratorConfig } from './hash.js';
import type { TokenGeneratorConfig } from './token.js';
/**
* Hash generator pre configuration for making random self-validate **secrets**.
* Alwatr hash generator recommended configuration for making random self-validate **user-id**.
*/
export declare const secretGeneratorPreConfig: HashGeneratorConfig;
export declare const userIdGeneratorRecommendedConfig: HashGeneratorConfig;
/**
* Hash generator pre configuration for making random self-validate **user-id**.
* Hash generator recommended configuration for making random self-validate **device-id**.
*/
export declare const userIdGeneratorPreConfig: HashGeneratorConfig;
export declare const deviceIdGeneratorRecommendedConfig: HashGeneratorConfig;
/**
* Token generator pre configuration for making secure self-validate **user-token**.
* Hash generator pre configuration for making random self-validate **secrets**.
*/
export declare const userTokenGeneratorPreConfig: Pick<TokenGeneratorConfig, 'algorithm' | 'encoding' | 'prefix'>;
export declare const secretGeneratorRecommendedConfig: HashGeneratorConfig;
/**
* Token generator recommended configuration for making secure self-validate **user-token**.
*/
export declare const userTokenGeneratorRecommendedConfig: Omit<TokenGeneratorConfig, 'secret' | 'duration'>;
//# sourceMappingURL=pre-config.d.ts.map
/**
* Hash generator pre configuration for making random self-validate **secrets**.
* Alwatr hash generator recommended configuration for making random self-validate **user-id**.
*/
export const secretGeneratorPreConfig = {
prefix: 's',
algorithm: 'sha384',
export const userIdGeneratorRecommendedConfig = {
prefix: 'u',
algorithm: 'sha1',
encoding: 'base64url',

@@ -11,7 +11,14 @@ crcLength: 4,

/**
* Hash generator pre configuration for making random self-validate **user-id**.
* Hash generator recommended configuration for making random self-validate **device-id**.
*/
export const userIdGeneratorPreConfig = {
prefix: 'u',
algorithm: 'sha1',
export const deviceIdGeneratorRecommendedConfig = {
...userIdGeneratorRecommendedConfig,
prefix: 'd',
};
/**
* Hash generator pre configuration for making random self-validate **secrets**.
*/
export const secretGeneratorRecommendedConfig = {
prefix: 's',
algorithm: 'sha384',
encoding: 'base64url',

@@ -21,5 +28,5 @@ crcLength: 4,

/**
* Token generator pre configuration for making secure self-validate **user-token**.
* Token generator recommended configuration for making secure self-validate **user-token**.
*/
export const userTokenGeneratorPreConfig = {
export const userTokenGeneratorRecommendedConfig = {
prefix: 't',

@@ -26,0 +33,0 @@ algorithm: 'sha224',

@@ -1,3 +0,30 @@

import type { TokenGeneratorConfig, TokenStatus } from './type.js';
import { DurationString } from '@alwatr/math';
import type { CryptoAlgorithm, CryptoEncoding } from './type.js';
export type TokenValidity = 'valid' | 'invalid' | 'expired';
/**
* Represents the configuration for a token generator.
*/
export interface TokenGeneratorConfig {
/**
* The prefix to be added to the generated hash.
*/
prefix: string;
/**
* The algorithm used for hashing.
*/
algorithm: CryptoAlgorithm;
/**
* The encoding used for the generated hash.
*/
encoding: CryptoEncoding;
/**
* The secret used for encryption and decryption tokens.
*/
secret: string;
/**
* The duration for which the token is valid.
*/
duration: DurationString | 'infinite';
}
/**
* Secure authentication HOTP token generator (HMAC-based One-Time Password algorithm).

@@ -7,7 +34,7 @@ */

config: TokenGeneratorConfig;
protected _duration: number | null;
private _duration;
/**
* The current epoch based on the configured duration.
*/
get epoch(): number;
protected get _epoch(): number;
/**

@@ -32,9 +59,9 @@ * Creates a new instance of AlwatrTokenGenerator.

* @param token The token to verify.
* @returns The status of the token verification.
* @returns The validity of the token.
* @example
* ```typescript
* const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);
* const validateStatus = tokenGenerator.verify([user.id,user.role].join(), user.auth);
* ```
*/
verify(data: string, token: string): TokenStatus;
verify(data: string, token: string): TokenValidity;
/**

@@ -41,0 +68,0 @@ * Generates a cryptographic token based on the provided data and epoch.

@@ -10,4 +10,4 @@ import { createHmac } from 'node:crypto';

*/
get epoch() {
return this._duration == null ? 0 : Math.floor(Date.now() / this._duration);
get _epoch() {
return this._duration == 0 ? 0 : Math.floor(Date.now() / this._duration);
}

@@ -20,3 +20,3 @@ /**

this.config = config;
this._duration = config.duration == null ? null : parseDuration(config.duration);
this._duration = config.duration == 'infinite' ? 0 : parseDuration(config.duration);
}

@@ -33,3 +33,3 @@ /**

generate(data) {
return this._generate(data, this.epoch);
return this._generate(data, this._epoch);
}

@@ -40,22 +40,17 @@ /**

* @param token The token to verify.
* @returns The status of the token verification.
* @returns The validity of the token.
* @example
* ```typescript
* const validateStatus = tokenGenerator.verify(`${user.id}-${user.role}`, user.auth);
* const validateStatus = tokenGenerator.verify([user.id,user.role].join(), user.auth);
* ```
*/
verify(data, token) {
const epoch = this.epoch;
if (token === this._generate(data, epoch)) {
const epoch = this._epoch;
if (token === this._generate(data, epoch))
return 'valid';
}
else if (this._duration == null) {
if (this._duration == 0)
return 'invalid';
}
else if (token === this._generate(data, epoch - 1)) {
if (token === this._generate(data, epoch - 1))
return 'expired';
}
else {
return 'invalid';
}
return 'invalid';
}

@@ -69,7 +64,8 @@ /**

_generate(data, epoch) {
return this.config.prefix + createHmac(this.config.algorithm, data)
.update(data + epoch)
.digest(this.config.encoding);
return (this.config.prefix +
createHmac(this.config.algorithm, data)
.update(data + epoch)
.digest(this.config.encoding));
}
}
//# sourceMappingURL=token.js.map

@@ -1,46 +0,11 @@

import type { DurationString } from '@alwatr/math';
/**
* Represents a cryptographic algorithm.
* Supported algorithms include: 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'.
*/
export type CryptoAlgorithm = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';
/**
* Represents the encoding options for cryptographic operations.
* The encoding can be one of the following: 'base64', 'base64url', 'hex', or 'binary'.
*/
export type CryptoEncoding = 'base64' | 'base64url' | 'hex' | 'binary';
export type TokenStatus = 'valid' | 'invalid' | 'expired';
export type HashStatus = 'valid' | 'invalid';
export interface TokenGeneratorConfig {
prefix: string;
/**
* Secret string data to generate token.
*/
secret: string;
/**
* Token expiration time.
*
* `null` mean without expiration time
*/
duration: DurationString | null;
/**
* OpenSSl digest algorithm.
*/
algorithm: CryptoAlgorithm;
/**
* Encoding of token.
*/
encoding: CryptoEncoding;
}
export interface HashGeneratorConfig {
prefix: string;
/**
* OpenSSl digest algorithm.
*/
algorithm: CryptoAlgorithm;
/**
* Encoding of hash.
*/
encoding: CryptoEncoding;
/**
* CRC hash max length.
*/
crcLength?: number;
}
export interface UserGeneratorConfig {
userId: HashGeneratorConfig;
token: TokenGeneratorConfig;
}
//# sourceMappingURL=type.d.ts.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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