@twin.org/crypto 0.0.3-next.37 → 0.0.3-next.38

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.
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
3
  import { pbkdf2 } from "@noble/hashes/pbkdf2.js";
4
4
  import { sha256, sha512 } from "@noble/hashes/sha2.js";
5
- import { Guards } from "@twin.org/core";
5
+ import { GeneralError, Guards } from "@twin.org/core";
6
6
  /**
7
7
  * Implementation of the password based key derivation function 2.
8
8
  */
@@ -18,12 +18,16 @@ export class Pbkdf2 {
18
18
  * @param iterations Number of iterations to perform.
19
19
  * @param keyLength The length of the key to derive.
20
20
  * @returns The derived key.
21
+ * @throws GeneralError If the keyLength is less than 1.
21
22
  */
22
23
  static sha256(password, salt, iterations, keyLength) {
23
24
  Guards.uint8Array(Pbkdf2.CLASS_NAME, "password", password);
24
25
  Guards.uint8Array(Pbkdf2.CLASS_NAME, "salt", salt);
25
26
  Guards.number(Pbkdf2.CLASS_NAME, "iterations", iterations);
26
27
  Guards.number(Pbkdf2.CLASS_NAME, "keyLength", keyLength);
28
+ if (keyLength < 1) {
29
+ throw new GeneralError(Pbkdf2.CLASS_NAME, "keyLengthTooSmall", { keyLength });
30
+ }
27
31
  return pbkdf2(sha256, password, salt, { c: iterations, dkLen: keyLength });
28
32
  }
29
33
  /**
@@ -33,12 +37,16 @@ export class Pbkdf2 {
33
37
  * @param iterations Number of iterations to perform.
34
38
  * @param keyLength The length of the key to derive.
35
39
  * @returns The derived key.
40
+ * @throws GeneralError If the keyLength is less than 1.
36
41
  */
37
42
  static sha512(password, salt, iterations, keyLength) {
38
43
  Guards.uint8Array(Pbkdf2.CLASS_NAME, "password", password);
39
44
  Guards.uint8Array(Pbkdf2.CLASS_NAME, "salt", salt);
40
45
  Guards.number(Pbkdf2.CLASS_NAME, "iterations", iterations);
41
46
  Guards.number(Pbkdf2.CLASS_NAME, "keyLength", keyLength);
47
+ if (keyLength < 1) {
48
+ throw new GeneralError(Pbkdf2.CLASS_NAME, "keyLengthTooSmall", { keyLength });
49
+ }
42
50
  return pbkdf2(sha512, password, salt, { c: iterations, dkLen: keyLength });
43
51
  }
44
52
  }
@@ -1 +1 @@
1
- {"version":3,"file":"pbkdf2.js","sourceRoot":"","sources":["../../../src/hashes/pbkdf2.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,QAAoB,EACpB,IAAgB,EAChB,UAAkB,EAClB,SAAiB;QAEjB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QACjE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,QAAoB,EACpB,IAAgB,EAChB,UAAkB,EAClB,SAAiB;QAEjB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QACjE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { pbkdf2 } from \"@noble/hashes/pbkdf2.js\";\nimport { sha256, sha512 } from \"@noble/hashes/sha2.js\";\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Implementation of the password based key derivation function 2.\n */\nexport class Pbkdf2 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Pbkdf2>();\n\n\t/**\n\t * Derive a key from the parameters using Sha256.\n\t * @param password The password to derive the key from.\n\t * @param salt The salt for the derivation.\n\t * @param iterations Number of iterations to perform.\n\t * @param keyLength The length of the key to derive.\n\t * @returns The derived key.\n\t */\n\tpublic static sha256(\n\t\tpassword: Uint8Array,\n\t\tsalt: Uint8Array,\n\t\titerations: number,\n\t\tkeyLength: number\n\t): Uint8Array {\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(password), password);\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(salt), salt);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(iterations), iterations);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(keyLength), keyLength);\n\t\treturn pbkdf2(sha256, password, salt, { c: iterations, dkLen: keyLength });\n\t}\n\n\t/**\n\t * Derive a key from the parameters using Sha512.\n\t * @param password The password to derive the key from.\n\t * @param salt The salt for the derivation.\n\t * @param iterations Number of iterations to perform.\n\t * @param keyLength The length of the key to derive.\n\t * @returns The derived key.\n\t */\n\tpublic static sha512(\n\t\tpassword: Uint8Array,\n\t\tsalt: Uint8Array,\n\t\titerations: number,\n\t\tkeyLength: number\n\t): Uint8Array {\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(password), password);\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(salt), salt);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(iterations), iterations);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(keyLength), keyLength);\n\t\treturn pbkdf2(sha512, password, salt, { c: iterations, dkLen: keyLength });\n\t}\n}\n"]}
1
+ {"version":3,"file":"pbkdf2.js","sourceRoot":"","sources":["../../../src/hashes/pbkdf2.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGtD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;;;;;;OAQG;IACI,MAAM,CAAC,MAAM,CACnB,QAAoB,EACpB,IAAgB,EAChB,UAAkB,EAClB,SAAiB;QAEjB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QACjE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAC/D,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;YACnB,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,UAAU,EAAE,mBAAmB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;QAC/E,CAAC;QACD,OAAO,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED;;;;;;;;OAQG;IACI,MAAM,CAAC,MAAM,CACnB,QAAoB,EACpB,IAAgB,EAChB,UAAkB,EAClB,SAAiB;QAEjB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QACjE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAC/D,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;YACnB,MAAM,IAAI,YAAY,CAAC,MAAM,CAAC,UAAU,EAAE,mBAAmB,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;QAC/E,CAAC;QACD,OAAO,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { pbkdf2 } from \"@noble/hashes/pbkdf2.js\";\nimport { sha256, sha512 } from \"@noble/hashes/sha2.js\";\nimport { GeneralError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Implementation of the password based key derivation function 2.\n */\nexport class Pbkdf2 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Pbkdf2>();\n\n\t/**\n\t * Derive a key from the parameters using Sha256.\n\t * @param password The password to derive the key from.\n\t * @param salt The salt for the derivation.\n\t * @param iterations Number of iterations to perform.\n\t * @param keyLength The length of the key to derive.\n\t * @returns The derived key.\n\t * @throws GeneralError If the keyLength is less than 1.\n\t */\n\tpublic static sha256(\n\t\tpassword: Uint8Array,\n\t\tsalt: Uint8Array,\n\t\titerations: number,\n\t\tkeyLength: number\n\t): Uint8Array {\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(password), password);\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(salt), salt);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(iterations), iterations);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(keyLength), keyLength);\n\t\tif (keyLength < 1) {\n\t\t\tthrow new GeneralError(Pbkdf2.CLASS_NAME, \"keyLengthTooSmall\", { keyLength });\n\t\t}\n\t\treturn pbkdf2(sha256, password, salt, { c: iterations, dkLen: keyLength });\n\t}\n\n\t/**\n\t * Derive a key from the parameters using Sha512.\n\t * @param password The password to derive the key from.\n\t * @param salt The salt for the derivation.\n\t * @param iterations Number of iterations to perform.\n\t * @param keyLength The length of the key to derive.\n\t * @returns The derived key.\n\t * @throws GeneralError If the keyLength is less than 1.\n\t */\n\tpublic static sha512(\n\t\tpassword: Uint8Array,\n\t\tsalt: Uint8Array,\n\t\titerations: number,\n\t\tkeyLength: number\n\t): Uint8Array {\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(password), password);\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(salt), salt);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(iterations), iterations);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(keyLength), keyLength);\n\t\tif (keyLength < 1) {\n\t\t\tthrow new GeneralError(Pbkdf2.CLASS_NAME, \"keyLengthTooSmall\", { keyLength });\n\t\t}\n\t\treturn pbkdf2(sha512, password, salt, { c: iterations, dkLen: keyLength });\n\t}\n}\n"]}
@@ -63,7 +63,7 @@ export class PasswordGenerator {
63
63
  * @internal
64
64
  */
65
65
  static getRandomChar(charSet) {
66
- let b = 0;
66
+ let b;
67
67
  do {
68
68
  b = RandomHelper.generate(1)[0];
69
69
  } while (b >= charSet.length);
@@ -1 +1 @@
1
- {"version":3,"file":"passwordGenerator.js","sourceRoot":"","sources":["../../../src/passwords/passwordGenerator.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAC7B;;OAEG;IACI,MAAM,CAAU,UAAU,uBAAuC;IAExE;;;;OAIG;IACK,MAAM,CAAU,4BAA4B,GAAW,EAAE,CAAC;IAElE;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,SAAiB,iBAAiB,CAAC,4BAA4B;QACrF,MAAM,KAAK,GAAG,4BAA4B,CAAC;QAC3C,MAAM,KAAK,GAAG,4BAA4B,CAAC;QAC3C,MAAM,MAAM,GAAG,YAAY,CAAC;QAC5B,MAAM,QAAQ,GAAG,gBAAgB,CAAC;QAClC,MAAM,QAAQ,GAAG,GAAG,KAAK,GAAG,KAAK,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,EAAE,CAAC;QAEnD,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,4BAA4B,CAAC,CAAC;QACtF,MAAM,KAAK,GAAa,EAAE,CAAC;QAE3B,iDAAiD;QACjD,iBAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACzC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CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACjC,CAAC,QAAQ,CAAC,IAAI,OAAO,CAAC,MAAM,EAAE;QAC9B,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,QAAQ,CAAC,KAAe,EAAE,OAAe;QACvD,IAAI,IAAI,GAAG,iBAAiB,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACpD,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC5E,IAAI,GAAG,iBAAiB,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Converter, Guards, RandomHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { Blake2b } from \"../hashes/blake2b.js\";\n\n/**\n * Generate random passwords.\n */\nexport class PasswordGenerator {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<PasswordGenerator>();\n\n\t/**\n\t * The minimum password length, 15 to match owasp rules.\n\t * @see https://www.owasp.org/index.php/Authentication_Cheat_Sheet#Implement_Proper_Password_Strength_Controls .\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_MIN_PASSWORD_LENGTH: number = 15;\n\n\t/**\n\t * Generate a password of given length.\n\t * @param length The length of the password to generate, default to 15.\n\t * @returns The random password.\n\t */\n\tpublic static generate(length: number = PasswordGenerator._DEFAULT_MIN_PASSWORD_LENGTH): string {\n\t\tconst lower = \"abcdefghijklmnopqrstuvwxyz\";\n\t\tconst upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n\t\tconst digits = \"0123456789\";\n\t\tconst specials = \"!#$£%^&*+=@~?}\";\n\t\tconst alphabet = `${lower}${upper}`;\n\t\tconst allChars = `${alphabet}${digits}${specials}`;\n\n\t\tconst targetLength = Math.max(length, PasswordGenerator._DEFAULT_MIN_PASSWORD_LENGTH);\n\t\tconst chars: string[] = [];\n\n\t\t// Ensure required character classes are present.\n\t\tPasswordGenerator.pushChar(chars, lower);\n\t\tPasswordGenerator.pushChar(chars, upper);\n\t\tPasswordGenerator.pushChar(chars, digits);\n\t\tPasswordGenerator.pushChar(chars, specials);\n\n\t\twhile (chars.length < targetLength) {\n\t\t\tconst charSet = chars.length === 0 ? alphabet : allChars;\n\t\t\tPasswordGenerator.pushChar(chars, charSet);\n\t\t}\n\n\t\treturn chars.join(\"\");\n\t}\n\n\t/**\n\t * Hash the password for the user.\n\t * @param passwordBytes The password bytes.\n\t * @param saltBytes The salt bytes.\n\t * @returns The hashed password.\n\t */\n\tpublic static async hashPassword(\n\t\tpasswordBytes: Uint8Array,\n\t\tsaltBytes: Uint8Array\n\t): Promise<string> {\n\t\tGuards.uint8Array(PasswordGenerator.CLASS_NAME, nameof(passwordBytes), passwordBytes);\n\t\tGuards.uint8Array(PasswordGenerator.CLASS_NAME, nameof(saltBytes), saltBytes);\n\n\t\tconst combined = new Uint8Array(saltBytes.length + passwordBytes.length);\n\t\tcombined.set(saltBytes);\n\t\tcombined.set(passwordBytes, saltBytes.length);\n\n\t\tconst hashedPassword = Blake2b.sum256(combined);\n\n\t\treturn Converter.bytesToBase64(hashedPassword);\n\t}\n\n\t/**\n\t * Get a random character from the given character set.\n\t * @param charSet The character set to get a random character from.\n\t * @returns A random character from the given character set.\n\t * @internal\n\t */\n\tprivate static getRandomChar(charSet: string): string {\n\t\tlet b = 0;\n\t\tdo {\n\t\t\tb = RandomHelper.generate(1)[0];\n\t\t} while (b >= charSet.length);\n\t\treturn charSet[b];\n\t}\n\n\t/**\n\t * Push a random character from the given character set to the chars array, ensuring no three repeated characters in a row.\n\t * @param chars The array to push the character to.\n\t * @param charSet The character set to get a random character from.\n\t * @internal\n\t */\n\tprivate static pushChar(chars: string[], charSet: string): void {\n\t\tlet next = PasswordGenerator.getRandomChar(charSet);\n\t\twhile (chars.length >= 2 && next === chars.at(-1) && next === chars.at(-2)) {\n\t\t\tnext = PasswordGenerator.getRandomChar(charSet);\n\t\t}\n\t\tchars.push(next);\n\t}\n}\n"]}
1
+ {"version":3,"file":"passwordGenerator.js","sourceRoot":"","sources":["../../../src/passwords/passwordGenerator.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAC7B;;OAEG;IACI,MAAM,CAAU,UAAU,uBAAuC;IAExE;;;;OAIG;IACK,MAAM,CAAU,4BAA4B,GAAW,EAAE,CAAC;IAElE;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,SAAiB,iBAAiB,CAAC,4BAA4B;QACrF,MAAM,KAAK,GAAG,4BAA4B,CAAC;QAC3C,MAAM,KAAK,GAAG,4BAA4B,CAAC;QAC3C,MAAM,MAAM,GAAG,YAAY,CAAC;QAC5B,MAAM,QAAQ,GAAG,gBAAgB,CAAC;QAClC,MAAM,QAAQ,GAAG,GAAG,KAAK,GAAG,KAAK,EAAE,CAAC;QACpC,MAAM,QAAQ,GAAG,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,EAAE,CAAC;QAEnD,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,4BAA4B,CAAC,CAAC;QACtF,MAAM,KAAK,GAAa,EAAE,CAAC;QAE3B,iDAAiD;QACjD,iBAAiB,CAAC,QAAQ,CAAC,KAAK,EA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,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACjC,CAAC,QAAQ,CAAC,IAAI,OAAO,CAAC,MAAM,EAAE;QAC9B,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC;IAED;;;;;OAKG;IACK,MAAM,CAAC,QAAQ,CAAC,KAAe,EAAE,OAAe;QACvD,IAAI,IAAI,GAAG,iBAAiB,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACpD,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC5E,IAAI,GAAG,iBAAiB,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Converter, Guards, RandomHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { Blake2b } from \"../hashes/blake2b.js\";\n\n/**\n * Generate random passwords.\n */\nexport class PasswordGenerator {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<PasswordGenerator>();\n\n\t/**\n\t * The minimum password length, 15 to match owasp rules.\n\t * @see https://www.owasp.org/index.php/Authentication_Cheat_Sheet#Implement_Proper_Password_Strength_Controls .\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_MIN_PASSWORD_LENGTH: number = 15;\n\n\t/**\n\t * Generate a password of given length.\n\t * @param length The length of the password to generate, default to 15.\n\t * @returns The random password.\n\t */\n\tpublic static generate(length: number = PasswordGenerator._DEFAULT_MIN_PASSWORD_LENGTH): string {\n\t\tconst lower = \"abcdefghijklmnopqrstuvwxyz\";\n\t\tconst upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n\t\tconst digits = \"0123456789\";\n\t\tconst specials = \"!#$£%^&*+=@~?}\";\n\t\tconst alphabet = `${lower}${upper}`;\n\t\tconst allChars = `${alphabet}${digits}${specials}`;\n\n\t\tconst targetLength = Math.max(length, PasswordGenerator._DEFAULT_MIN_PASSWORD_LENGTH);\n\t\tconst chars: string[] = [];\n\n\t\t// Ensure required character classes are present.\n\t\tPasswordGenerator.pushChar(chars, lower);\n\t\tPasswordGenerator.pushChar(chars, upper);\n\t\tPasswordGenerator.pushChar(chars, digits);\n\t\tPasswordGenerator.pushChar(chars, specials);\n\n\t\twhile (chars.length < targetLength) {\n\t\t\tconst charSet = chars.length === 0 ? alphabet : allChars;\n\t\t\tPasswordGenerator.pushChar(chars, charSet);\n\t\t}\n\n\t\treturn chars.join(\"\");\n\t}\n\n\t/**\n\t * Hash the password for the user.\n\t * @param passwordBytes The password bytes.\n\t * @param saltBytes The salt bytes.\n\t * @returns The hashed password.\n\t */\n\tpublic static async hashPassword(\n\t\tpasswordBytes: Uint8Array,\n\t\tsaltBytes: Uint8Array\n\t): Promise<string> {\n\t\tGuards.uint8Array(PasswordGenerator.CLASS_NAME, nameof(passwordBytes), passwordBytes);\n\t\tGuards.uint8Array(PasswordGenerator.CLASS_NAME, nameof(saltBytes), saltBytes);\n\n\t\tconst combined = new Uint8Array(saltBytes.length + passwordBytes.length);\n\t\tcombined.set(saltBytes);\n\t\tcombined.set(passwordBytes, saltBytes.length);\n\n\t\tconst hashedPassword = Blake2b.sum256(combined);\n\n\t\treturn Converter.bytesToBase64(hashedPassword);\n\t}\n\n\t/**\n\t * Get a random character from the given character set.\n\t * @param charSet The character set to get a random character from.\n\t * @returns A random character from the given character set.\n\t * @internal\n\t */\n\tprivate static getRandomChar(charSet: string): string {\n\t\tlet b;\n\t\tdo {\n\t\t\tb = RandomHelper.generate(1)[0];\n\t\t} while (b >= charSet.length);\n\t\treturn charSet[b];\n\t}\n\n\t/**\n\t * Push a random character from the given character set to the chars array, ensuring no three repeated characters in a row.\n\t * @param chars The array to push the character to.\n\t * @param charSet The character set to get a random character from.\n\t * @internal\n\t */\n\tprivate static pushChar(chars: string[], charSet: string): void {\n\t\tlet next = PasswordGenerator.getRandomChar(charSet);\n\t\twhile (chars.length >= 2 && next === chars.at(-1) && next === chars.at(-2)) {\n\t\t\tnext = PasswordGenerator.getRandomChar(charSet);\n\t\t}\n\t\tchars.push(next);\n\t}\n}\n"]}
@@ -13,6 +13,7 @@ export declare class Pbkdf2 {
13
13
  * @param iterations Number of iterations to perform.
14
14
  * @param keyLength The length of the key to derive.
15
15
  * @returns The derived key.
16
+ * @throws GeneralError If the keyLength is less than 1.
16
17
  */
17
18
  static sha256(password: Uint8Array, salt: Uint8Array, iterations: number, keyLength: number): Uint8Array;
18
19
  /**
@@ -22,6 +23,7 @@ export declare class Pbkdf2 {
22
23
  * @param iterations Number of iterations to perform.
23
24
  * @param keyLength The length of the key to derive.
24
25
  * @returns The derived key.
26
+ * @throws GeneralError If the keyLength is less than 1.
25
27
  */
26
28
  static sha512(password: Uint8Array, salt: Uint8Array, iterations: number, keyLength: number): Uint8Array;
27
29
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.0.3-next.38](https://github.com/iotaledger/twin-framework/compare/crypto-v0.0.3-next.37...crypto-v0.0.3-next.38) (2026-05-11)
4
+
5
+
6
+ ### Features
7
+
8
+ * typescript 6 update ([1d10f31](https://github.com/iotaledger/twin-framework/commit/1d10f31e6516ec622773f45e88af82fe749b384a))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/core bumped from 0.0.3-next.37 to 0.0.3-next.38
16
+ * @twin.org/nameof bumped from 0.0.3-next.37 to 0.0.3-next.38
17
+ * devDependencies
18
+ * @twin.org/nameof-transformer bumped from 0.0.3-next.37 to 0.0.3-next.38
19
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.3-next.37 to 0.0.3-next.38
20
+ * @twin.org/validate-locales bumped from 0.0.3-next.37 to 0.0.3-next.38
21
+
3
22
  ## [0.0.3-next.37](https://github.com/iotaledger/twin-framework/compare/crypto-v0.0.3-next.36...crypto-v0.0.3-next.37) (2026-05-07)
4
23
 
5
24
 
@@ -60,6 +60,10 @@ The length of the key to derive.
60
60
 
61
61
  The derived key.
62
62
 
63
+ #### Throws
64
+
65
+ GeneralError If the keyLength is less than 1.
66
+
63
67
  ***
64
68
 
65
69
  ### sha512() {#sha512}
@@ -99,3 +103,7 @@ The length of the key to derive.
99
103
  `Uint8Array`
100
104
 
101
105
  The derived key.
106
+
107
+ #### Throws
108
+
109
+ GeneralError If the keyLength is less than 1.
package/locales/en.json CHANGED
@@ -40,6 +40,9 @@
40
40
  "slip0010": {
41
41
  "invalidSeed": "The seed is invalid \"{seed}\""
42
42
  },
43
+ "pbkdf2": {
44
+ "keyLengthTooSmall": "The key length must be at least 1, it is \"{keyLength}\""
45
+ },
43
46
  "validation": {
44
47
  "minLengthRequired": "The value length should be at least {minLength}, it is {actualLength}",
45
48
  "maxLengthRequired": "The value length should be at most {maxLength}, it is {actualLength}",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/crypto",
3
- "version": "0.0.3-next.37",
3
+ "version": "0.0.3-next.38",
4
4
  "description": "Helper methods and classes which implement cryptographic functions",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,15 +14,15 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@noble/ciphers": "2.1.1",
18
- "@noble/curves": "2.0.1",
19
- "@noble/hashes": "2.0.1",
20
- "@scure/base": "2.0.0",
21
- "@scure/bip32": "2.0.1",
22
- "@scure/bip39": "2.0.1",
23
- "@twin.org/core": "0.0.3-next.37",
24
- "@twin.org/nameof": "0.0.3-next.37",
25
- "micro-key-producer": "0.8.5"
17
+ "@noble/ciphers": "2.2.0",
18
+ "@noble/curves": "2.2.0",
19
+ "@noble/hashes": "2.2.0",
20
+ "@scure/base": "2.2.0",
21
+ "@scure/bip32": "2.2.0",
22
+ "@scure/bip39": "2.2.0",
23
+ "@twin.org/core": "0.0.3-next.38",
24
+ "@twin.org/nameof": "0.0.3-next.38",
25
+ "micro-key-producer": "0.8.6"
26
26
  },
27
27
  "main": "./dist/es/index.js",
28
28
  "types": "./dist/types/index.d.ts",