Skip to content

Commit

Permalink
Convert to ESM and use node test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Nov 5, 2023
1 parent ca7c541 commit 49bf3b2
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 690 deletions.
2 changes: 0 additions & 2 deletions argon2.d.ts → argon2.d.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Type definitions for argon2 v0.19.2

/// <reference types="node" />

export const argon2d: 0;
Expand Down
63 changes: 34 additions & 29 deletions argon2.js → argon2.mjs
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
"use strict";
const assert = require("assert");
const { randomBytes, timingSafeEqual } = require("crypto");
const path = require("path");
const { promisify } = require("util");
const binary = require("@mapbox/node-pre-gyp");

const bindingPath = binary.find(path.resolve(__dirname, "./package.json"));
import assert from "node:assert/strict";
import { randomBytes, timingSafeEqual } from "node:crypto";
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import nodePreGyp from "@mapbox/node-pre-gyp";
import { deserialize, serialize } from "@phc/format";

const packageJson = fileURLToPath(new URL("./package.json", import.meta.url));
const bindingPath = nodePreGyp.find(packageJson);

const require = createRequire(import.meta.url);
const { hash: _hash } = require(bindingPath);

const { deserialize, serialize } = require("@phc/format");
export const argon2d = 0;
export const argon2i = 1;
export const argon2id = 2;

const types = Object.freeze({ argon2d: 0, argon2i: 1, argon2id: 2 });

const defaults = Object.freeze({
export const defaults = Object.freeze({
hashLength: 32,
saltLength: 16,
timeCost: 3,
memoryCost: 1 << 16,
memoryCost: 2 ** 16,
parallelism: 4,
type: types.argon2id,
type: argon2id,
version: 0x13,
});

const limits = Object.freeze({
export const limits = Object.freeze({
hashLength: { min: 4, max: 2 ** 32 - 1 },
memoryCost: { min: 1 << 10, max: 2 ** 32 - 1 },
memoryCost: { min: 2 ** 10, max: 2 ** 32 - 1 },
timeCost: { min: 2, max: 2 ** 32 - 1 },
parallelism: { min: 1, max: 2 ** 24 - 1 },
});

const names = Object.freeze({
[types.argon2d]: "argon2d",
[types.argon2i]: "argon2i",
[types.argon2id]: "argon2id",
});
const names = {
[argon2d]: "argon2d",
[argon2i]: "argon2i",
[argon2id]: "argon2id",
};

const types = { argon2d, argon2i, argon2id };

const bindingsHash = promisify(_hash);
const generateSalt = promisify(randomBytes);
Expand All @@ -48,7 +54,7 @@ const assertLimits =
);
};

const hash = async (plain, { raw, salt, ...options } = {}) => {
export const hash = async (plain, { raw, salt, ...options } = {}) => {
options = { ...defaults, ...options };

Object.entries(limits).forEach(assertLimits(options));
Expand Down Expand Up @@ -77,7 +83,7 @@ const hash = async (plain, { raw, salt, ...options } = {}) => {
});
};

const needsRehash = (digest, options) => {
export const needsRehash = (digest, options) => {
const { memoryCost, timeCost, version } = { ...defaults, ...options };

const {
Expand All @@ -87,13 +93,14 @@ const needsRehash = (digest, options) => {
return +v !== +version || +m !== +memoryCost || +t !== +timeCost;
};

const verify = async (digest, plain, options) => {
export const verify = async (digest, plain, options) => {
const obj = deserialize(digest);
// Only these have the "params" key, so if the password was encoded
// using any other method, the destructuring throws an error
if (!(obj.id in types)) {
return false;
}
assert(
obj.id in types,
`Invalid type, must be one of: ${Object.keys(types).join(", ")}`,
);

const {
id,
Expand All @@ -117,5 +124,3 @@ const verify = async (digest, plain, options) => {
hash,
);
};

module.exports = { defaults, limits, hash, needsRehash, verify, ...types };
77 changes: 39 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@
"name": "argon2",
"version": "0.31.2",
"description": "An Argon2 library for Node",
"main": "argon2.js",
"keywords": [
"argon2",
"crypto",
"encryption",
"hashing",
"password"
],
"homepage": "https://github.com/ranisalt/node-argon2#readme",
"bugs": {
"url": "https://github.com/ranisalt/node-argon2/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/ranisalt/node-argon2.git"
},
"license": "MIT",
"author": "Ranieri Althoff <[email protected]>",
"type": "module",
"main": "argon2.mjs",
"module": "argon2.mjs",
"types": "argon2.d.mts",
"files": [
"argon2_node.cpp",
"argon2.d.ts",
"argon2.d.mts",
"binding.gyp",
"argon2/CHANGELOG.md",
"argon2/LICENSE",
Expand All @@ -21,30 +41,22 @@
"argon2/src/thread.c",
"argon2/src/thread.h"
],
"types": "argon2.d.ts",
"binary": {
"module_name": "argon2",
"module_path": "./lib/binding/napi-v{napi_build_version}",
"remote_path": "v{version}",
"package_name": "{module_name}-v{version}-napi-v{napi_build_version}-{platform}-{arch}-{libc}.tar.gz",
"host": "https://github.com/ranisalt/node-argon2/releases/download/",
"napi_versions": [
3
]
},
"scripts": {
"format": "prettier --write \"**/*.{json,mjs,mts}\"",
"install": "node-pre-gyp install --fallback-to-build",
"format": "prettier --write \"**/*.{js,json,ts}\"",
"test": "c8 mocha --timeout 5000 test/test.js",
"test:ts": "tsc -p . && node test/test-d.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ranisalt/node-argon2.git"
"test": "c8 node --test test.mjs",
"test:ts": "tsc -p ."
},
"keywords": [
"argon2",
"crypto",
"encryption",
"hashing",
"password"
],
"author": "Ranieri Althoff <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/ranisalt/node-argon2/issues"
},
"homepage": "https://github.com/ranisalt/node-argon2#readme",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.11",
"@phc/format": "^1.0.0",
Expand All @@ -53,32 +65,21 @@
"devDependencies": {
"@types/node": "^20.8.10",
"c8": "^8.0.1",
"mocha": "^10.2.0",
"node-gyp": "^10.0.1",
"prettier": "^3.0.0",
"typescript": "^5.1.6"
},
"binary": {
"module_name": "argon2",
"module_path": "./lib/binding/napi-v{napi_build_version}",
"host": "https://github.com/ranisalt/node-argon2/releases/download/",
"remote_path": "v{version}",
"package_name": "{module_name}-v{version}-napi-v{napi_build_version}-{platform}-{arch}-{libc}.tar.gz",
"napi_versions": [
3
]
},
"engines": {
"node": ">=14.0.0"
},
"collective": {
"type": "opencollective",
"url": "https://opencollective.com/node-argon2"
},
"standard": {
"env": "mocha",
"ignore": [
"test/test-d.js"
]
},
"collective": {
"type": "opencollective",
"url": "https://opencollective.com/node-argon2"
}
}
2 changes: 1 addition & 1 deletion test/test-d.ts → test-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// <reference types="node" />

import * as argon2 from "../argon2";
import * as argon2 from "./argon2.mjs";

const password = "password";
const passwordBuffer = Buffer.from("password");
Expand Down
Loading

0 comments on commit 49bf3b2

Please sign in to comment.