diff --git a/argon2.d.ts b/argon2.d.mts similarity index 96% rename from argon2.d.ts rename to argon2.d.mts index 0e7bc7a..6c52040 100644 --- a/argon2.d.ts +++ b/argon2.d.mts @@ -1,5 +1,3 @@ -// Type definitions for argon2 v0.19.2 - /// export const argon2d: 0; diff --git a/argon2.js b/argon2.mjs similarity index 62% rename from argon2.js rename to argon2.mjs index 0e6cfa2..d0caf08 100644 --- a/argon2.js +++ b/argon2.mjs @@ -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); @@ -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)); @@ -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 { @@ -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, @@ -117,5 +124,3 @@ const verify = async (digest, plain, options) => { hash, ); }; - -module.exports = { defaults, limits, hash, needsRehash, verify, ...types }; diff --git a/package.json b/package.json index bf4ec74..f827c8e 100644 --- a/package.json +++ b/package.json @@ -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 ", + "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", @@ -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": { - "install": "node-pre-gyp install --fallback-to-build", "format": "prettier --write \"*.{js,json,ts}\"", - "test": "c8 node --test test.js", - "test:ts": "tsc -p . && node test/test-d.js" - }, - "repository": { - "type": "git", - "url": "https://github.com/ranisalt/node-argon2.git" - }, - "keywords": [ - "argon2", - "crypto", - "encryption", - "hashing", - "password" - ], - "author": "Ranieri Althoff ", - "license": "MIT", - "bugs": { - "url": "https://github.com/ranisalt/node-argon2/issues" + "install": "node-pre-gyp install --fallback-to-build", + "test": "c8 node --test test.mjs", + "test:ts": "tsc -p ." }, - "homepage": "https://github.com/ranisalt/node-argon2#readme", "dependencies": { "@mapbox/node-pre-gyp": "^1.0.11", "@phc/format": "^1.0.0", @@ -57,26 +69,16 @@ "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": ">=16.17.0" }, + "collective": { + "type": "opencollective", + "url": "https://opencollective.com/node-argon2" + }, "standard": { "ignore": [ "test-d.js" ] - }, - "collective": { - "type": "opencollective", - "url": "https://opencollective.com/node-argon2" } } diff --git a/test-d.ts b/test-d.mts similarity index 98% rename from test-d.ts rename to test-d.mts index 2cf85c4..b46ade8 100644 --- a/test-d.ts +++ b/test-d.mts @@ -3,7 +3,7 @@ /// -import * as argon2 from "./argon2"; +import * as argon2 from "./argon2.mjs"; const password = "password"; const passwordBuffer = Buffer.from("password"); diff --git a/test.js b/test.mjs similarity index 98% rename from test.js rename to test.mjs index 51cdf35..c647e29 100644 --- a/test.js +++ b/test.mjs @@ -1,6 +1,6 @@ -const assert = require("node:assert/strict"); -const { describe, it } = require("node:test"); -const argon2 = require("./argon2.js"); +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import * as argon2 from "./argon2.mjs"; const { argon2i, argon2d, argon2id } = argon2; const password = "password"; diff --git a/tsconfig.json b/tsconfig.json index 1775286..720f991 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { - "target": "es2015", - "module": "commonjs", - "moduleResolution": "node", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "noEmit": true, "strict": true }, - "files": ["test-d.ts"] + "files": ["test-d.mts"] }