@1auth/crypto 0.0.0-beta.1 → 0.0.0-beta.2

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.
Files changed (3) hide show
  1. package/README.md +44 -0
  2. package/index.js +22 -34
  3. package/package.json +13 -10
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ <div align="center">
2
+ <h1>@1auth/crypto</h1>
3
+ <!--<img alt="1auth logo" src="https://raw.githubusercontent.com/willfarrell/1auth/main/docs/img/logo.svg"/>-->
4
+ <p><strong>Cryptographic utilities for encryption, hashing, and signing with modern algorithms</strong></p>
5
+ <p>
6
+ <a href="https://github.com/willfarrell/1auth/actions/workflows/test-unit.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-unit.yml/badge.svg" alt="GitHub Actions unit test status"></a>
7
+ <a href="https://github.com/willfarrell/1auth/actions/workflows/test-dast.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-dast.yml/badge.svg" alt="GitHub Actions dast test status"></a>
8
+ <a href="https://github.com/willfarrell/1auth/actions/workflows/test-perf.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-perf.yml/badge.svg" alt="GitHub Actions perf test status"></a>
9
+ <a href="https://github.com/willfarrell/1auth/actions/workflows/test-sast.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-sast.yml/badge.svg" alt="GitHub Actions SAST test status"></a>
10
+ <a href="https://github.com/willfarrell/1auth/actions/workflows/test-lint.yml"><img src="https://github.com/willfarrell/1auth/actions/workflows/test-lint.yml/badge.svg" alt="GitHub Actions lint test status"></a>
11
+ <br/>
12
+ <a href="https://www.npmjs.com/package/@1auth/crypto"><img alt="npm version" src="https://img.shields.io/npm/v/@1auth/crypto.svg"></a>
13
+ <a href="https://packagephobia.com/result?p=@1auth/crypto"><img src="https://packagephobia.com/badge?p=@1auth/crypto" alt="npm install size"></a>
14
+ <a href="https://www.npmjs.com/package/@1auth/crypto">
15
+ <img alt="npm weekly downloads" src="https://img.shields.io/npm/dw/@1auth/crypto.svg"></a>
16
+ <a href="https://www.npmjs.com/package/@1auth/crypto#provenance">
17
+ <img alt="npm provenance" src="https://img.shields.io/badge/provenance-Yes-brightgreen"></a>
18
+ <br/>
19
+ <a href="https://scorecard.dev/viewer/?uri=github.com/willfarrell/1auth"><img src="https://api.scorecard.dev/projects/github.com/willfarrell/1auth/badge" alt="Open Source Security Foundation (OpenSSF) Scorecard"></a>
20
+ <a href="https://slsa.dev"><img src="https://slsa.dev/images/gh-badge-level3.svg" alt="SLSA 3"></a>
21
+ <a href="https://github.com/willfarrell/1auth/blob/main/docs/CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg"></a>
22
+ <a href="https://biomejs.dev"><img alt="Checked with Biome" src="https://img.shields.io/badge/Checked_with-Biome-60a5fa?style=flat&logo=biome"></a>
23
+ <a href="https://conventionalcommits.org"><img alt="Conventional Commits" src="https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white"></a>
24
+ </p>
25
+ <p>You can read the documentation at: <a href="https://1auth.js.org">https://1auth.js.org</a></p>
26
+ </div>
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ npm install @1auth/crypto
32
+ ```
33
+
34
+ ## Documentation and examples
35
+
36
+ For documentation and examples, refer to the main [1auth monorepo on GitHub](https://github.com/willfarrell/1auth) or the [1auth website](https://1auth.js.org).
37
+
38
+ ## Contributing
39
+
40
+ Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/willfarrell/1auth/issues) or to [submit Pull Requests](https://github.com/willfarrell/1auth/pulls).
41
+
42
+ ## License
43
+
44
+ Licensed under [MIT License](LICENSE). Copyright (c) 2020-2026 [will Farrell](https://github.com/willfarrell) and [contributors](https://github.com/willfarrell/1auth/graphs/contributors).
package/index.js CHANGED
@@ -34,13 +34,17 @@ const defaults = {
34
34
  digestChecksumEncoding: undefined,
35
35
  digestChecksumSalt: undefined, // randomChecksumSalt()
36
36
  digestChecksumPepper: undefined, // randomChecksumPepper()
37
+ // Password hashing defaults - based on OWASP Password Storage Cheat Sheet
38
+ // https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
39
+ // OWASP minimums: 19 MiB memory, 2 iterations, 1 parallelism
40
+ // We use 32 MiB (exceeds minimum) for enhanced security
37
41
  secretArgon2Algorithm: "argon2id",
38
- secretArgon2Version: 19, // argon2id
39
- secretArgon2Parallelism: 1, // argon2id
40
- secretArgon2MemoryCost: 15, // argon2id
41
- secretArgon2TimeCost: 3, // argon2id
42
- secretArgon2NonceLength: 16, // argon2id
43
- secretArgon2HashLength: 64, // argon2id
42
+ secretArgon2Version: 19,
43
+ secretArgon2Parallelism: 1, // OWASP: 1 (matches)
44
+ secretArgon2MemoryCost: 15, // 2^15 KiB = 32 MiB (exceeds OWASP minimum of 19 MiB)
45
+ secretArgon2TimeCost: 3, // OWASP: 2 (we use 3 for better security)
46
+ secretArgon2NonceLength: 16,
47
+ secretArgon2HashLength: 64,
44
48
 
45
49
  defaultEncoding: "base64",
46
50
  defaultHashAlgorithm: "sha3-384",
@@ -121,35 +125,12 @@ export const makeOptionsBuffer = (
121
125
 
122
126
  export const getOptions = () => options;
123
127
 
124
- // *** entropy *** //
125
- // export const characterPoolSize = (value) => {
126
- // const chars = value.split('')
127
- // let min = chars[0].charCodeAt()
128
- // let max = chars[0].charCodeAt()
129
- // for(const char of value.split('')) {
130
- // const code = char.charCodeAt()
131
- // if (code < min) {
132
- // min = code
133
- // } else if (max < code) {
134
- // max = code
135
- // }
136
- // }
137
- // return max - min
138
- // }
139
-
140
128
  // *** Helpers *** //
141
129
  // Ref: https://therootcompany.com/blog/how-many-bits-of-entropy-per-character/
142
130
  export const entropyToCharacterLength = (bits, characterPoolSize) => {
143
131
  // bits*ln(2)/ln(characterPoolSize)
144
132
  return Math.ceil((bits * Math.LN2) / Math.log(characterPoolSize));
145
133
  };
146
- /* export const characterLengthToEntropy = (
147
- characterLength,
148
- characterPoolSize,
149
- ) => {
150
- // log_2(characterPoolSize^characterLength)
151
- return Math.floor(Math.log2(characterPoolSize ** characterLength));
152
- }; */
153
134
 
154
135
  // *** Random generators *** //
155
136
  export { randomBytes, randomInt, randomUUID } from "node:crypto";
@@ -179,7 +160,7 @@ export const randomAlphaNumeric = (characterLength) => {
179
160
  export const randomNumeric = (characterLength) => {
180
161
  let value = "";
181
162
  for (let i = characterLength; i--; ) {
182
- value += randomInt(9);
163
+ value += randomInt(0, 10);
183
164
  }
184
165
  return value;
185
166
  };
@@ -442,7 +423,7 @@ export const symmetricGenerateEncryptionKey = (
442
423
  { encryptionKey, signatureSecret } = {},
443
424
  ) => {
444
425
  encryptionKey ??= options.symmetricEncryptionKey;
445
- signatureSecret ??= options.symemeticSignatureSecret;
426
+ signatureSecret ??= options.symmetricSignatureSecret;
446
427
 
447
428
  const rowEncryptionKey = symmetricRandomEncryptionKey();
448
429
  const rowEncryptedKey = symmetricEncrypt(rowEncryptionKey, {
@@ -539,7 +520,7 @@ export const symmetricDecryptKey = (
539
520
  { sub, encryptionKey, signatureSecret } = {},
540
521
  ) => {
541
522
  encryptionKey ??= options.symmetricEncryptionKey;
542
- signatureSecret ??= options.symemeticSignatureSecret;
523
+ signatureSecret ??= options.symmetricSignatureSecret;
543
524
  return Buffer.from(
544
525
  symmetricDecrypt(encryptedKey, {
545
526
  encryptionKey,
@@ -575,7 +556,9 @@ export const symmetricDecrypt = (
575
556
  );
576
557
 
577
558
  if (encryptedDataPacket === false) {
578
- throw new Error("Signature incorrect");
559
+ throw new Error("Signature incorrect", {
560
+ cause: { signedEncryptedDataPacket },
561
+ });
579
562
  }
580
563
  const iv = Buffer.from(
581
564
  encryptedDataPacket.substring(0, symmetricEncryptionEncodingLengths.iv),
@@ -666,7 +649,10 @@ export const symmetricRotation = (
666
649
  return data;
667
650
  },
668
651
  ) => {
669
- if (oldOptions.sub !== newOptions.sub) throw new Error("Mismatching `sub`");
652
+ if (oldOptions.sub !== newOptions.sub)
653
+ throw new Error("Mismatching `sub`", {
654
+ cause: { sub: oldOptions.sub },
655
+ });
670
656
 
671
657
  const oldEncryptedValuesClone = structuredClone(oldEncryptedValues);
672
658
  // Don't use structuredClone, converts Buffer to Uint8Array ...
@@ -754,6 +740,8 @@ export const verifyAsymmetricSignature = async (
754
740
  );
755
741
  };
756
742
 
743
+ export const nowInSeconds = () => Math.floor(Date.now() / 1000);
744
+
757
745
  export const safeEqual = (input, expected) => {
758
746
  const bufferInput = Buffer.from(input);
759
747
  const bufferExpected = Buffer.from(expected);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@1auth/crypto",
3
- "version": "0.0.0-beta.1",
4
- "description": "",
3
+ "version": "0.0.0-beta.2",
4
+ "description": "Cryptographic utilities for encryption, hashing, and signing with modern algorithms",
5
5
  "type": "module",
6
6
  "engines": {
7
7
  "node": ">=24"
@@ -12,18 +12,14 @@
12
12
  },
13
13
  "main": "./index.js",
14
14
  "module": "./index.js",
15
+ "sideEffects": false,
15
16
  "exports": {
16
17
  ".": {
17
- "import": {
18
- "types": "./index.d.ts",
19
- "default": "./index.js"
20
- }
18
+ "import": "./index.js"
21
19
  }
22
20
  },
23
- "types": "index.d.ts",
24
21
  "files": [
25
- "index.js",
26
- "index.d.ts"
22
+ "index.js"
27
23
  ],
28
24
  "scripts": {
29
25
  "test": "npm run test:unit",
@@ -34,7 +30,14 @@
34
30
  "type": "github",
35
31
  "url": "https://github.com/sponsors/willfarrell"
36
32
  },
37
- "keywords": [],
33
+ "keywords": [
34
+ "1auth",
35
+ "OWASP",
36
+ "ASVS",
37
+ "crypto",
38
+ "encryption",
39
+ "hashing"
40
+ ],
38
41
  "author": {
39
42
  "name": "1auth contributors",
40
43
  "url": "https://github.com/willfarrell/1auth/graphs/contributors"