@ttoss/auth-core 0.1.12 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -7,4 +7,34 @@ var encode = obj => {
7
7
  var decode = encoded => {
8
8
  return JSON.parse(Buffer.from(encoded, "base64").toString("utf8"));
9
9
  };
10
- export { decode, encode };
10
+
11
+ // src/hash.ts
12
+ import crypto from "node:crypto";
13
+ var ITERATIONS = 1e3;
14
+ var KEY_LENGTH = 64;
15
+ var DIGEST = "sha256";
16
+ var hashPassword = plainPassword => {
17
+ const salt = crypto.randomBytes(16).toString("hex");
18
+ return new Promise((resolve, reject) => {
19
+ crypto.pbkdf2(plainPassword, salt, ITERATIONS, KEY_LENGTH, DIGEST, (err, derivedKey) => {
20
+ if (err) {
21
+ reject(err);
22
+ }
23
+ const hash = derivedKey.toString("hex");
24
+ resolve(`${salt}:${hash}`);
25
+ });
26
+ });
27
+ };
28
+ var comparePassword = (plainPassword, storedHash) => {
29
+ const [salt, hash] = storedHash.split(":");
30
+ return new Promise((resolve, reject) => {
31
+ crypto.pbkdf2(plainPassword, salt, ITERATIONS, KEY_LENGTH, DIGEST, (err, derivedKey) => {
32
+ if (err) {
33
+ reject(err);
34
+ }
35
+ const hashToCompare = derivedKey.toString("hex");
36
+ resolve(hash === hashToCompare);
37
+ });
38
+ });
39
+ };
40
+ export { comparePassword, decode, encode, hashPassword };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,7 @@
1
1
  declare const encode: (obj: unknown) => string;
2
2
  declare const decode: (encoded: string) => any;
3
3
 
4
- export { decode, encode };
4
+ declare const hashPassword: (plainPassword: string) => Promise<string>;
5
+ declare const comparePassword: (plainPassword: string, storedHash: string) => Promise<boolean>;
6
+
7
+ export { comparePassword, decode, encode, hashPassword };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  declare const encode: (obj: unknown) => string;
2
2
  declare const decode: (encoded: string) => any;
3
3
 
4
- export { decode, encode };
4
+ declare const hashPassword: (plainPassword: string) => Promise<string>;
5
+ declare const comparePassword: (plainPassword: string, storedHash: string) => Promise<boolean>;
6
+
7
+ export { comparePassword, decode, encode, hashPassword };
package/dist/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
2
  "use strict";
3
3
 
4
+ var __create = Object.create;
4
5
  var __defProp = Object.defineProperty;
5
6
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
7
  var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
7
9
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
10
  var __export = (target, all) => {
9
11
  for (var name in all) __defProp(target, name, {
@@ -20,6 +22,15 @@ var __copyProps = (to, from, except, desc) => {
20
22
  }
21
23
  return to;
22
24
  };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
31
+ value: mod,
32
+ enumerable: true
33
+ }) : target, mod));
23
34
  var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
24
35
  value: true
25
36
  }), mod);
@@ -27,8 +38,10 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
27
38
  // src/index.ts
28
39
  var src_exports = {};
29
40
  __export(src_exports, {
41
+ comparePassword: () => comparePassword,
30
42
  decode: () => decode,
31
- encode: () => encode
43
+ encode: () => encode,
44
+ hashPassword: () => hashPassword
32
45
  });
33
46
  module.exports = __toCommonJS(src_exports);
34
47
 
@@ -39,8 +52,40 @@ var encode = obj => {
39
52
  var decode = encoded => {
40
53
  return JSON.parse(Buffer.from(encoded, "base64").toString("utf8"));
41
54
  };
55
+
56
+ // src/hash.ts
57
+ var import_node_crypto = __toESM(require("crypto"));
58
+ var ITERATIONS = 1e3;
59
+ var KEY_LENGTH = 64;
60
+ var DIGEST = "sha256";
61
+ var hashPassword = plainPassword => {
62
+ const salt = import_node_crypto.default.randomBytes(16).toString("hex");
63
+ return new Promise((resolve, reject) => {
64
+ import_node_crypto.default.pbkdf2(plainPassword, salt, ITERATIONS, KEY_LENGTH, DIGEST, (err, derivedKey) => {
65
+ if (err) {
66
+ reject(err);
67
+ }
68
+ const hash = derivedKey.toString("hex");
69
+ resolve(`${salt}:${hash}`);
70
+ });
71
+ });
72
+ };
73
+ var comparePassword = (plainPassword, storedHash) => {
74
+ const [salt, hash] = storedHash.split(":");
75
+ return new Promise((resolve, reject) => {
76
+ import_node_crypto.default.pbkdf2(plainPassword, salt, ITERATIONS, KEY_LENGTH, DIGEST, (err, derivedKey) => {
77
+ if (err) {
78
+ reject(err);
79
+ }
80
+ const hashToCompare = derivedKey.toString("hex");
81
+ resolve(hash === hashToCompare);
82
+ });
83
+ });
84
+ };
42
85
  // Annotate the CommonJS export names for ESM import in node:
43
86
  0 && (module.exports = {
87
+ comparePassword,
44
88
  decode,
45
- encode
89
+ encode,
90
+ hashPassword
46
91
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/auth-core",
3
- "version": "0.1.12",
3
+ "version": "0.2.0",
4
4
  "description": "Core authentication library for ttoss",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -48,6 +48,6 @@
48
48
  },
49
49
  "scripts": {
50
50
  "build": "tsup",
51
- "test": "echo jest"
51
+ "test": "jest --projects tests/unit"
52
52
  }
53
53
  }
package/src/hash.ts ADDED
@@ -0,0 +1,50 @@
1
+ import crypto from 'node:crypto';
2
+
3
+ const ITERATIONS = 1000;
4
+
5
+ const KEY_LENGTH = 64;
6
+
7
+ const DIGEST = 'sha256';
8
+
9
+ export const hashPassword = (plainPassword: string): Promise<string> => {
10
+ const salt = crypto.randomBytes(16).toString('hex');
11
+ return new Promise((resolve, reject) => {
12
+ crypto.pbkdf2(
13
+ plainPassword,
14
+ salt,
15
+ ITERATIONS,
16
+ KEY_LENGTH,
17
+ DIGEST,
18
+ (err, derivedKey) => {
19
+ if (err) {
20
+ reject(err);
21
+ }
22
+ const hash = derivedKey.toString('hex');
23
+ resolve(`${salt}:${hash}`);
24
+ }
25
+ );
26
+ });
27
+ };
28
+
29
+ export const comparePassword = (
30
+ plainPassword: string,
31
+ storedHash: string
32
+ ): Promise<boolean> => {
33
+ const [salt, hash] = storedHash.split(':');
34
+ return new Promise((resolve, reject) => {
35
+ crypto.pbkdf2(
36
+ plainPassword,
37
+ salt,
38
+ ITERATIONS,
39
+ KEY_LENGTH,
40
+ DIGEST,
41
+ (err, derivedKey) => {
42
+ if (err) {
43
+ reject(err);
44
+ }
45
+ const hashToCompare = derivedKey.toString('hex');
46
+ resolve(hash === hashToCompare);
47
+ }
48
+ );
49
+ });
50
+ };
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { encode, decode } from './encodeDecode';
2
+ export { hashPassword, comparePassword } from './hash';