@stacksjs/security 0.53.1 → 0.56.23

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/index.d.ts CHANGED
@@ -1,3 +1,14 @@
1
- export * from './key';
2
- export * from './crypt';
3
- export * from './hash';
1
+ declare function generateAppKey(): Promise<string>;
2
+
3
+ declare function encrypt(message: string): string;
4
+ declare function decrypt(encrypted: string): string;
5
+
6
+ declare function make(password: string, algorithm?: string): Promise<string>;
7
+ declare function verify(password: string, hash: string, algorithm?: string): Promise<boolean>;
8
+ declare function bcryptEncode(password: string): Promise<string>;
9
+ declare function bcryptVerify(password: string, hash: string): Promise<boolean>;
10
+ declare function base64Encode(password: string): Promise<string>;
11
+ declare function base64Verify(password: string, hash: string): Promise<boolean>;
12
+ declare function md5Encode(password: string): Promise<CryptoJS.lib.WordArray>;
13
+
14
+ export { base64Encode, base64Verify, bcryptEncode, bcryptVerify, decrypt, encrypt, generateAppKey, make as makeHash, md5Encode, verify as verifyHash };
package/dist/index.mjs CHANGED
@@ -1,3 +1,65 @@
1
- export * from "./key.mjs";
2
- export * from "./crypt.mjs";
3
- export * from "./hash.mjs";
1
+ import { getRandomValues } from 'node:crypto';
2
+ import utf8 from 'crypto-js/enc-utf8';
3
+ import base64 from 'crypto-js/enc-base64';
4
+ import aes from 'crypto-js/aes';
5
+ import { env } from '@stacksjs/validation';
6
+ import bcryptjs from 'bcryptjs';
7
+ import { Base64 } from 'js-base64';
8
+ import md5 from 'crypto-js/md5';
9
+ import { hashing } from '@stacksjs/config';
10
+
11
+ async function generateAppKey() {
12
+ const random = getRandomValues(new Uint8Array(32));
13
+ const encodedWord = utf8.parse(random.toString());
14
+ const key = base64.stringify(encodedWord);
15
+ return `base64:${key}`;
16
+ }
17
+
18
+ function encrypt(message) {
19
+ const passphrase = env.APP_KEY;
20
+ if (!passphrase)
21
+ throw new Error("APP_KEY is not defined");
22
+ return aes.encrypt(message, passphrase).toString();
23
+ }
24
+ function decrypt(encrypted) {
25
+ const passphrase = env.APP_KEY;
26
+ if (!passphrase)
27
+ throw new Error("APP_KEY is not defined");
28
+ return aes.decrypt(encrypted, passphrase).toString(utf8);
29
+ }
30
+
31
+ async function make(password, algorithm = "bcrypt") {
32
+ if (algorithm === "bcrypt")
33
+ return bcryptEncode(password);
34
+ if (algorithm === "base64")
35
+ return base64Encode(password);
36
+ throw new Error("Unsupported algorithm");
37
+ }
38
+ async function verify(password, hash, algorithm = "bcrypt") {
39
+ if (algorithm === "bcrypt")
40
+ return bcryptVerify(password, hash);
41
+ if (algorithm === "base64")
42
+ return base64Verify(password, hash);
43
+ throw new Error("Unsupported algorithm");
44
+ }
45
+ async function bcryptEncode(password) {
46
+ if (!hashing.bcrypt)
47
+ throw new Error("Bcrypt hashing is not configured");
48
+ const salt = await bcryptjs.genSaltSync(hashing.bcrypt.rounds);
49
+ const hash = await bcryptjs.hash(password, salt);
50
+ return hash;
51
+ }
52
+ async function bcryptVerify(password, hash) {
53
+ return await bcryptjs.compare(password, hash);
54
+ }
55
+ async function base64Encode(password) {
56
+ return await Base64.encode(password);
57
+ }
58
+ async function base64Verify(password, hash) {
59
+ return Base64.decode(hash) === password;
60
+ }
61
+ async function md5Encode(password) {
62
+ return md5(password);
63
+ }
64
+
65
+ export { base64Encode, base64Verify, bcryptEncode, bcryptVerify, decrypt, encrypt, generateAppKey, make as makeHash, md5Encode, verify as verifyHash };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@stacksjs/security",
3
3
  "type": "module",
4
- "version": "0.53.1",
5
- "packageManager": "pnpm@8.5.1",
4
+ "version": "0.56.23",
5
+ "packageManager": "pnpm@8.6.5",
6
6
  "description": "The Stacks framework security.",
7
7
  "author": "Chris Breuer",
8
8
  "license": "MIT",
@@ -28,6 +28,7 @@
28
28
  "import": "./dist/index.mjs"
29
29
  }
30
30
  },
31
+ "main": "dist/index.mjs",
31
32
  "module": "dist/index.mjs",
32
33
  "types": "dist/index.d.ts",
33
34
  "contributors": [
@@ -41,11 +42,12 @@
41
42
  "bcryptjs": "^2.4.3",
42
43
  "crypto-js": "^4.1.1",
43
44
  "js-base64": "^3.7.5",
44
- "@stacksjs/config": "0.53.1",
45
- "@stacksjs/types": "0.53.1"
45
+ "@stacksjs/config": "0.56.23",
46
+ "@stacksjs/types": "0.56.23",
47
+ "@stacksjs/validation": "0.56.23"
46
48
  },
47
49
  "devDependencies": {
48
- "@stacksjs/development": "0.53.1"
50
+ "@stacksjs/development": "0.56.23"
49
51
  },
50
52
  "scripts": {
51
53
  "build": "unbuild",
package/dist/crypt.d.ts DELETED
@@ -1,3 +0,0 @@
1
- declare function encrypt(message: string): string;
2
- declare function decrypt(encrypted: string): string;
3
- export { encrypt, decrypt };
package/dist/crypt.mjs DELETED
@@ -1,11 +0,0 @@
1
- import aes from "crypto-js/aes";
2
- import utf8 from "crypto-js/enc-utf8";
3
- function encrypt(message) {
4
- const passphrase = import.meta.env.APP_KEY;
5
- return aes.encrypt(message, passphrase).toString();
6
- }
7
- function decrypt(encrypted) {
8
- const passphrase = import.meta.env.APP_KEY;
9
- return aes.decrypt(encrypted, passphrase).toString(utf8);
10
- }
11
- export { encrypt, decrypt };
package/dist/hash.d.ts DELETED
@@ -1,8 +0,0 @@
1
- declare function make(password: string, algorithm?: string): Promise<any>;
2
- declare function verify(password: string, hash: string, algorithm?: string): Promise<any>;
3
- declare function bcryptEncode(password: string): Promise<any>;
4
- declare function bcryptVerify(password: string, hash: string): Promise<any>;
5
- declare function base64Encode(password: string): Promise<string>;
6
- declare function base64Verify(password: string, hash: string): Promise<boolean>;
7
- declare function md5Encode(password: string): Promise<any>;
8
- export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode };
package/dist/hash.mjs DELETED
@@ -1,38 +0,0 @@
1
- import bcryptjs from "bcryptjs";
2
- import { Base64 } from "js-base64";
3
- import md5 from "crypto-js/md5";
4
- import { hashing } from "@stacksjs/config";
5
- async function make(password, algorithm = "bcrypt") {
6
- if (algorithm === "bcrypt")
7
- return bcryptEncode(password);
8
- if (algorithm === "base64")
9
- return base64Encode(password);
10
- throw new Error("Unsupported algorithm");
11
- }
12
- async function verify(password, hash, algorithm = "bcrypt") {
13
- if (algorithm === "bcrypt")
14
- return bcryptVerify(password, hash);
15
- if (algorithm === "base64")
16
- return base64Verify(password, hash);
17
- throw new Error("Unsupported algorithm");
18
- }
19
- async function bcryptEncode(password) {
20
- if (!hashing.bcrypt)
21
- throw new Error("Bcrypt hashing is not configured");
22
- const salt = await bcryptjs.genSaltSync(hashing.bcrypt.rounds);
23
- const hash = await bcryptjs.hash(password, salt);
24
- return hash;
25
- }
26
- async function bcryptVerify(password, hash) {
27
- return await bcryptjs.compare(password, hash);
28
- }
29
- async function base64Encode(password) {
30
- return await Base64.encode(password);
31
- }
32
- async function base64Verify(password, hash) {
33
- return Base64.decode(hash) === password;
34
- }
35
- async function md5Encode(password) {
36
- return md5(password);
37
- }
38
- export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode };
package/dist/key.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function generateAppKey(): Promise<string>;
package/dist/key.mjs DELETED
@@ -1,9 +0,0 @@
1
- import { getRandomValues } from "node:crypto";
2
- import utf8 from "crypto-js/enc-utf8";
3
- import base64 from "crypto-js/enc-base64";
4
- export async function generateAppKey() {
5
- const random = getRandomValues(new Uint8Array(32));
6
- const encodedWord = utf8.parse(random.toString());
7
- const key = base64.stringify(encodedWord);
8
- return `base64:${key}`;
9
- }