Skip to content

Commit

Permalink
Convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Nov 16, 2023
1 parent bb944ab commit 06a82bd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 76 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 };
76 changes: 39 additions & 37 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": {
"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 <[email protected]>",
"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",
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion 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
6 changes: 3 additions & 3 deletions test.js → test.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}

0 comments on commit 06a82bd

Please sign in to comment.