ironpass 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aryan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # IronPass 🔐
2
+
3
+ Modern password hashing library for Node.js built on PBKDF2.
4
+
5
+ ## Features
6
+
7
+ - Secure PBKDF2 hashing
8
+ - Configurable iterations
9
+ - Salted password hashing
10
+ - Structured encoded output
11
+
12
+ ## Installation
13
+
14
+ npm install ironpass
15
+
16
+ ## Usage
17
+
18
+ import { hash } from 'ironpass';
19
+
20
+ const hashed = await hash('password123');
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "ironpass",
3
+ "version": "0.1.0",
4
+ "description": "Secure password hashing library for Node.js using PBKDF2.",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "files": [
11
+ "src"
12
+ ],
13
+ "scripts": {
14
+ "test": "vitest run",
15
+ "test:watch": "vitest --watch",
16
+ "lint": "eslint .",
17
+ "format": "prettier --write .",
18
+ "check-format": "prettier --check .",
19
+ "prepare": "npm run lint && npm run test"
20
+ },
21
+ "keywords": [
22
+ "password",
23
+ "password-hashing",
24
+ "security",
25
+ "authentication",
26
+ "pbkdf2",
27
+ "crypto",
28
+ "node",
29
+ "nodejs"
30
+ ],
31
+ "author": "Aryan Yadav",
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/krptonox/Iron-Pass.git"
36
+ },
37
+ "engines": {
38
+ "node": ">=18"
39
+ },
40
+ "devDependencies": {
41
+ "@eslint/js": "^10.0.1",
42
+ "eslint": "^10.6.0",
43
+ "globals": "^17.7.0",
44
+ "prettier": "^3.9.4",
45
+ "vitest": "^4.1.9"
46
+ }
47
+ }
@@ -0,0 +1,11 @@
1
+ export const DEFAULT_SALT_LENGTH = 16;
2
+
3
+ export const DEFAULT_ITERATIONS = 300000;
4
+
5
+ export const DEFAULT_KEY_LENGTH = 32;
6
+
7
+ export const DEFAULT_DIGEST = 'sha256';
8
+
9
+ export const DEFAULT_ALGORITHM = 'pbkdf2';
10
+
11
+ export const DEFAULT_VERSION = 'v1';
@@ -0,0 +1 @@
1
+ export const MAX_PASSWORD_LENGTH = 1024;
@@ -0,0 +1 @@
1
+ //TO DO
@@ -0,0 +1 @@
1
+ //TO DO
@@ -0,0 +1,25 @@
1
+ import { normalizePassword } from '../password/normalizePassword.js';
2
+
3
+ import { validatePassword } from '../validators/validatePassword.js';
4
+
5
+ import { generateSalt } from '../crypto/generateSalt.js';
6
+
7
+ import { deriveKey } from '../crypto/deriveKey.js';
8
+
9
+ import { encodeHash } from '../crypto/encodeHash.js';
10
+
11
+
12
+ export async function hash(password, options = {}){
13
+
14
+ const normalizedPassword = normalizePassword(password);
15
+
16
+ const validatedPassword = validatePassword(normalizedPassword);
17
+
18
+ const salt = generateSalt();
19
+
20
+ const derivedKey = await deriveKey(validatedPassword, salt, options);
21
+
22
+ const encodedHash = encodeHash(derivedKey, salt, options);
23
+
24
+ return encodedHash;
25
+ }
@@ -0,0 +1 @@
1
+ //TO DO
@@ -0,0 +1,30 @@
1
+ import { DEFAULT_ITERATIONS, DEFAULT_KEY_LENGTH,DEFAULT_DIGEST } from '../constants/defaults.js';
2
+ import crypto from 'node:crypto';
3
+ import { validateOptions } from '../validators/validateDeriveKeyOptions.js';
4
+ import { validatePassword } from '../validators/validatePassword.js';
5
+ import { validateSalt } from '../validators/validateSalt.js';
6
+
7
+ export function deriveKey(password, salt, options = {
8
+ }){
9
+
10
+ validatePassword(password);
11
+ validateOptions(options);
12
+ validateSalt(salt);
13
+
14
+ const {
15
+ iterations = DEFAULT_ITERATIONS,
16
+ keyLength = DEFAULT_KEY_LENGTH,
17
+ digest = DEFAULT_DIGEST,
18
+ } = options;
19
+
20
+ return new Promise((resolve, reject) => {
21
+ crypto.pbkdf2(password,salt,iterations,keyLength,digest,(error, derivedKey)=>{
22
+ if(error){
23
+ reject(error);
24
+ return;
25
+ }
26
+
27
+ resolve(derivedKey);
28
+ });
29
+ });
30
+ }
@@ -0,0 +1,32 @@
1
+ import { DEFAULT_ALGORITHM, DEFAULT_VERSION, DEFAULT_DIGEST, DEFAULT_ITERATIONS, DEFAULT_KEY_LENGTH } from '../constants/defaults.js';
2
+
3
+ import { validateSalt } from '../validators/validateSalt.js';
4
+
5
+ import { validateDerivedKey } from '../validators/validateDerivedKey.js';
6
+
7
+ import { validateOptions } from '../validators/validateDeriveKeyOptions.js';
8
+
9
+
10
+
11
+ export function encodeHash(derivedKey, salt, options={}){
12
+
13
+ validateSalt(salt);
14
+ validateDerivedKey(derivedKey);
15
+ validateOptions(options);
16
+
17
+ const version = DEFAULT_VERSION;
18
+ const algorithm = DEFAULT_ALGORITHM;
19
+
20
+ const {
21
+ iterations = DEFAULT_ITERATIONS,
22
+ keyLength = DEFAULT_KEY_LENGTH,
23
+ digest = DEFAULT_DIGEST,
24
+ } = options;
25
+
26
+ const derivedKeyHex = derivedKey.toString('hex');
27
+
28
+ const hash = `${version}$${algorithm}$${digest}$${iterations}$${keyLength}$${salt}$${derivedKeyHex}`;
29
+
30
+ return hash;
31
+
32
+ }
@@ -0,0 +1,12 @@
1
+ import {randomBytes} from '../crypto/random.js';
2
+ import { DEFAULT_SALT_LENGTH } from '../constants/defaults.js';
3
+ import { validateSalt } from '../validators/validateSalt.js';
4
+
5
+ export function generateSalt(length = DEFAULT_SALT_LENGTH){
6
+
7
+ const buffer = randomBytes(length);
8
+
9
+ validateSalt(buffer.toString('hex'));
10
+
11
+ return buffer.toString('hex');
12
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Generate cryptographically secure random bytes.
3
+ *
4
+ * @param {number} length
5
+ * @returns {Buffer}
6
+ */
7
+
8
+
9
+ import crypto from 'node:crypto';
10
+ import {InvalidLengthError} from '../errors/InvalidLengthError.js';
11
+
12
+ export function randomBytes(length){
13
+ if(typeof length !== 'number' || length <= 0 || !Number.isInteger(length)){
14
+ throw new InvalidLengthError();
15
+ }
16
+
17
+ const buffer = crypto.randomBytes(length);
18
+
19
+ return buffer;
20
+ }
@@ -0,0 +1,10 @@
1
+ class InvalidLengthError extends Error {
2
+ constructor(
3
+ message = 'Length must be a positive integer greater than 0.'
4
+ ) {
5
+ super(message);
6
+ this.name = 'InvalidLengthError';
7
+ }
8
+ }
9
+
10
+ export { InvalidLengthError };
@@ -0,0 +1,8 @@
1
+ class InvalidNormalizationFormError extends Error {
2
+ constructor(message= 'Invalid normalization form. Must be one of NFC, NFD, NFKC, or NFKD. or pass a String'){
3
+ super(message);
4
+ this.message = message;
5
+ }
6
+ }
7
+
8
+ export { InvalidNormalizationFormError };
@@ -0,0 +1,8 @@
1
+ class InvalidOptionsError extends Error{
2
+ constructor(message='Invalid options provided. Please check the options and try again.'){
3
+ super(message);
4
+ this.message = message;
5
+ }
6
+ }
7
+
8
+ export default InvalidOptionsError;
@@ -0,0 +1,9 @@
1
+ class InvalidPasswordError extends Error{
2
+ constructor(message = 'Invalid password provided.'){
3
+ super(message);
4
+ this.name = 'InvalidPasswordError';
5
+ this.message = message;
6
+ }
7
+ }
8
+
9
+ export default InvalidPasswordError;
@@ -0,0 +1,8 @@
1
+ class InvalidSaltError extends Error {
2
+ constructor(message='Invalid salt provided'){
3
+ super(message);
4
+ this.name = message;
5
+ }
6
+ }
7
+
8
+ export {InvalidSaltError};
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ // src/index.js
2
+ export { hash } from './core/hash.core.js';
@@ -0,0 +1,19 @@
1
+ import { InvalidNormalizationFormError } from '../errors/InvalidNormalizationFormError.js';
2
+
3
+
4
+ const VALID_NORMALIZATION_FORMS = ['NFC','NFD','NFKC','NFKD'];
5
+
6
+
7
+ export function normalizePassword(password,form = 'NFC'){
8
+ if(typeof password !== 'string'){
9
+ throw new TypeError('Password must be a string.');
10
+ }
11
+
12
+ if(!VALID_NORMALIZATION_FORMS.includes(form)){
13
+ throw new InvalidNormalizationFormError('Invalid normalization form');
14
+ }
15
+
16
+ const normalizedPassword = password.normalize(form);
17
+
18
+ return normalizedPassword;
19
+ }
@@ -0,0 +1,39 @@
1
+ import InvalidOptionsError from '../errors/InvalidOptionsError.js';
2
+
3
+ export function validateOptions(options){
4
+ //to check the options , are they valid or not
5
+ const VALID_KEYS = [
6
+ 'iterations',
7
+ 'keyLength',
8
+ 'digest'
9
+ ];
10
+
11
+
12
+ if (!options || typeof options !== 'object') {
13
+ throw new InvalidOptionsError('Options must be an object');
14
+ }
15
+
16
+ for(const key of Object.keys(options)){
17
+
18
+ if(!VALID_KEYS.includes(key)){
19
+ throw new InvalidOptionsError(`Invalid option provided: ${key}`);
20
+ }
21
+
22
+ if(key === 'digest' && typeof options[key] !== 'string'){
23
+ throw new InvalidOptionsError(`Invalid type for option ${key}. Expected a string.`);
24
+ }
25
+
26
+ if(key === 'iterations' && (options[key] <= 0 || !Number.isInteger(options[key]))){
27
+ throw new InvalidOptionsError(`Invalid value for option ${key}. Must be a positive integer.`);
28
+ }
29
+
30
+ if(key === 'keyLength' && (options[key] <= 0 || !Number.isInteger(options[key]))){
31
+ throw new InvalidOptionsError(`Invalid value for option ${key}. Must be a positive integer.`);
32
+ }
33
+
34
+ if(key === 'digest' && !['sha256', 'sha512'].includes(options[key])){
35
+ throw new InvalidOptionsError(`Invalid value for option ${key}. Must be either 'sha256' or 'sha512'.`);
36
+ }
37
+
38
+ }
39
+ }
@@ -0,0 +1,5 @@
1
+ export function validateDerivedKey(derivedKey) {
2
+ if (!Buffer.isBuffer(derivedKey)) {
3
+ throw new Error('Derived key must be a Buffer');
4
+ }
5
+ }
@@ -0,0 +1,18 @@
1
+ import InvalidPasswordError from '../errors/InvalidPasswordError.js';
2
+ import { MAX_PASSWORD_LENGTH } from '../constants/password.js';
3
+
4
+ export function validatePassword(password){
5
+ if(typeof password !== 'string'){
6
+ throw new InvalidPasswordError('Password must be a string.');
7
+ }
8
+
9
+ if(password === ''){
10
+ throw new InvalidPasswordError('Password is required.');
11
+ }
12
+
13
+ if(password.length > MAX_PASSWORD_LENGTH){
14
+ throw new InvalidPasswordError('Password is too long.');
15
+ }
16
+
17
+ return password;
18
+ }
@@ -0,0 +1,10 @@
1
+ import {InvalidSaltError} from '../errors/InvalidSaltError.js';
2
+
3
+ export function validateSalt(salt) {
4
+ if (!salt) {
5
+ throw new InvalidSaltError();
6
+ }
7
+ if (typeof salt !== 'string') {
8
+ throw new InvalidSaltError('Salt must be a string');
9
+ }
10
+ }