@stacksjs/security 0.59.11 → 0.61.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/security",
3
3
  "type": "module",
4
- "version": "0.59.11",
4
+ "version": "0.61.1",
5
5
  "description": "The Stacks framework security.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
@@ -56,7 +56,6 @@
56
56
  "@stacksjs/env": "latest",
57
57
  "@stacksjs/types": "latest",
58
58
  "@stacksjs/validation": "latest",
59
- "bcryptjs": "^2.4.3",
60
59
  "crypto-js": "^4.2.0",
61
60
  "js-base64": "^3.7.7"
62
61
  },
package/src/crypt.ts CHANGED
@@ -1,12 +1,11 @@
1
+ import { env } from '@stacksjs/env'
1
2
  import aes from 'crypto-js/aes'
2
3
  import utf8 from 'crypto-js/enc-utf8'
3
- import { env } from '@stacksjs/env'
4
4
 
5
5
  function encrypt(message: string): string {
6
6
  const passphrase = env.APP_KEY
7
7
 
8
- if (!passphrase)
9
- throw new Error('APP_KEY is not defined')
8
+ if (!passphrase) throw new Error('APP_KEY is not defined')
10
9
 
11
10
  return aes.encrypt(message, passphrase).toString()
12
11
  }
@@ -14,8 +13,7 @@ function encrypt(message: string): string {
14
13
  function decrypt(encrypted: string): string {
15
14
  const passphrase = env.APP_KEY
16
15
 
17
- if (!passphrase)
18
- throw new Error('APP_KEY is not defined')
16
+ if (!passphrase) throw new Error('APP_KEY is not defined')
19
17
 
20
18
  return aes.decrypt(encrypted, passphrase).toString(utf8)
21
19
  }
package/src/hash.ts CHANGED
@@ -1,54 +1,75 @@
1
- import bcryptjs from 'bcryptjs'
2
- import { Base64 } from 'js-base64'
3
- import md5 from 'crypto-js/md5'
4
1
  import { hashing } from '@stacksjs/config'
2
+ import md5 from 'crypto-js/md5'
3
+ import { Base64 } from 'js-base64'
4
+
5
+ interface MakeOptions {
6
+ algorithm?: 'bcrypt' | 'base64' | 'argon2'
7
+ type?: 'argon2id' | 'argon2i' | 'argon2d'
8
+ }
9
+
10
+ async function make(password: string, options?: MakeOptions) {
11
+ if (options?.algorithm === 'argon2') return argon2Encode(password, { type: 'argon2id' })
5
12
 
6
- async function make(password: string, algorithm = 'bcrypt') {
7
- if (algorithm === 'bcrypt')
8
- return bcryptEncode(password)
13
+ if (options?.algorithm === 'bcrypt') return bcryptEncode(password)
9
14
 
10
- if (algorithm === 'base64')
11
- return base64Encode(password)
15
+ if (options?.algorithm === 'base64') return base64Encode(password)
12
16
 
13
17
  throw new Error('Unsupported algorithm')
14
18
  }
15
19
 
16
- type Algorithm = 'bcrypt' | 'base64'
20
+ type Algorithm = 'bcrypt' | 'base64' | 'argon2'
17
21
 
18
22
  async function verify(password: string, hash: string, algorithm?: Algorithm) {
19
- if (algorithm === 'bcrypt')
20
- return bcryptVerify(password, hash)
23
+ if (algorithm === 'argon2') return argon2Verify(password, hash)
21
24
 
22
- if (algorithm === 'base64')
23
- return base64Verify(password, hash)
25
+ if (algorithm === 'bcrypt') return bcryptVerify(password, hash)
26
+
27
+ if (algorithm === 'base64') return base64Verify(password, hash)
24
28
 
25
29
  throw new Error('Unsupported algorithm')
26
30
  }
27
31
 
28
- async function bcryptEncode(password: string) {
29
- if (!hashing.bcrypt)
30
- throw new Error('Bcrypt hashing is not configured')
32
+ export async function bcryptEncode(password: string) {
33
+ if (!hashing.bcrypt) throw new Error('Bcrypt hashing is not configured')
34
+
35
+ const bcryptHash = await Bun.password.hash(password, {
36
+ algorithm: 'bcrypt',
37
+ cost: hashing.bcrypt.cost,
38
+ })
39
+
40
+ return bcryptHash
41
+ }
31
42
 
32
- const salt = bcryptjs.genSaltSync(hashing.bcrypt.rounds)
33
- const hash = await bcryptjs.hash(password, salt)
43
+ export async function argon2Encode(password: string, options?: { type: 'argon2id' | 'argon2i' | 'argon2d' }) {
44
+ if (!hashing.argon2) throw new Error('Argon2 hashing is not configured')
45
+
46
+ const argon2Hash = await Bun.password.hash(password, {
47
+ algorithm: options?.type || 'argon2id',
48
+ memoryCost: hashing.argon2.memory,
49
+ timeCost: hashing.argon2.time,
50
+ })
51
+
52
+ return argon2Hash
53
+ }
34
54
 
35
- return hash
55
+ export async function argon2Verify(password: string, hash: string) {
56
+ return await Bun.password.verify(password, hash)
36
57
  }
37
58
 
38
- async function bcryptVerify(password: string, hash: string) {
39
- return await bcryptjs.compare(password, hash)
59
+ export async function bcryptVerify(password: string, hash: string) {
60
+ return await Bun.password.verify(password, hash)
40
61
  }
41
62
 
42
- function base64Encode(password: string) {
63
+ export function base64Encode(password: string) {
43
64
  return Base64.encode(password)
44
65
  }
45
66
 
46
- function base64Verify(password: string, hash: string) {
67
+ export function base64Verify(password: string, hash: string) {
47
68
  return Base64.decode(hash) === password
48
69
  }
49
70
 
50
- function md5Encode(password: string) {
71
+ export function md5Encode(password: string) {
51
72
  return md5(password)
52
73
  }
53
74
 
54
- export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode }
75
+ export { make as makeHash, verify as verifyHash }
package/src/key.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { getRandomValues } from 'node:crypto'
2
- import utf8 from 'crypto-js/enc-utf8'
3
2
  import base64 from 'crypto-js/enc-base64'
3
+ import utf8 from 'crypto-js/enc-utf8'
4
4
 
5
5
  export function generateAppKey() {
6
6
  const random = getRandomValues(new Uint8Array(32))
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/hash.d.ts DELETED
@@ -1,9 +0,0 @@
1
- declare function make(password: string, algorithm?: string): Promise<string>;
2
- type Algorithm = 'bcrypt' | 'base64';
3
- declare function verify(password: string, hash: string, algorithm?: Algorithm): Promise<boolean>;
4
- declare function bcryptEncode(password: string): Promise<string>;
5
- declare function bcryptVerify(password: string, hash: string): Promise<boolean>;
6
- declare function base64Encode(password: string): string;
7
- declare function base64Verify(password: string, hash: string): boolean;
8
- declare function md5Encode(password: string): CryptoJS.lib.WordArray;
9
- export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode };
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './key';
2
- export * from './crypt';
3
- export * from './hash';
package/dist/key.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function generateAppKey(): string;