@stacksjs/security 0.58.48 → 0.58.50
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 +3 -2
- package/src/crypt.ts +23 -0
- package/src/hash.ts +54 -0
- package/src/index.ts +3 -0
- package/src/key.ts +11 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/security",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.58.
|
|
4
|
+
"version": "0.58.50",
|
|
5
5
|
"description": "The Stacks framework security.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
],
|
|
39
39
|
"files": [
|
|
40
40
|
"README.md",
|
|
41
|
-
"dist"
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
42
43
|
],
|
|
43
44
|
"scripts": {
|
|
44
45
|
"build": "bun --bun build.ts",
|
package/src/crypt.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import aes from 'crypto-js/aes'
|
|
2
|
+
import utf8 from 'crypto-js/enc-utf8'
|
|
3
|
+
import { env } from '@stacksjs/env'
|
|
4
|
+
|
|
5
|
+
function encrypt(message: string): string {
|
|
6
|
+
const passphrase = env.APP_KEY
|
|
7
|
+
|
|
8
|
+
if (!passphrase)
|
|
9
|
+
throw new Error('APP_KEY is not defined')
|
|
10
|
+
|
|
11
|
+
return aes.encrypt(message, passphrase).toString()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function decrypt(encrypted: string): string {
|
|
15
|
+
const passphrase = env.APP_KEY
|
|
16
|
+
|
|
17
|
+
if (!passphrase)
|
|
18
|
+
throw new Error('APP_KEY is not defined')
|
|
19
|
+
|
|
20
|
+
return aes.decrypt(encrypted, passphrase).toString(utf8)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { encrypt, decrypt }
|
package/src/hash.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
|
|
6
|
+
async function make(password: string, algorithm = 'bcrypt') {
|
|
7
|
+
if (algorithm === 'bcrypt')
|
|
8
|
+
return bcryptEncode(password)
|
|
9
|
+
|
|
10
|
+
if (algorithm === 'base64')
|
|
11
|
+
return base64Encode(password)
|
|
12
|
+
|
|
13
|
+
throw new Error('Unsupported algorithm')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Algorithm = 'bcrypt' | 'base64'
|
|
17
|
+
|
|
18
|
+
async function verify(password: string, hash: string, algorithm?: Algorithm) {
|
|
19
|
+
if (algorithm === 'bcrypt')
|
|
20
|
+
return bcryptVerify(password, hash)
|
|
21
|
+
|
|
22
|
+
if (algorithm === 'base64')
|
|
23
|
+
return base64Verify(password, hash)
|
|
24
|
+
|
|
25
|
+
throw new Error('Unsupported algorithm')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function bcryptEncode(password: string) {
|
|
29
|
+
if (!hashing.bcrypt)
|
|
30
|
+
throw new Error('Bcrypt hashing is not configured')
|
|
31
|
+
|
|
32
|
+
const salt = bcryptjs.genSaltSync(hashing.bcrypt.rounds)
|
|
33
|
+
const hash = await bcryptjs.hash(password, salt)
|
|
34
|
+
|
|
35
|
+
return hash
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function bcryptVerify(password: string, hash: string) {
|
|
39
|
+
return await bcryptjs.compare(password, hash)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function base64Encode(password: string) {
|
|
43
|
+
return Base64.encode(password)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function base64Verify(password: string, hash: string) {
|
|
47
|
+
return Base64.decode(hash) === password
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function md5Encode(password: string) {
|
|
51
|
+
return md5(password)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { make as makeHash, verify as verifyHash, base64Encode, base64Verify, bcryptEncode, bcryptVerify, md5Encode }
|
package/src/index.ts
ADDED
package/src/key.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getRandomValues } from 'node:crypto'
|
|
2
|
+
import utf8 from 'crypto-js/enc-utf8'
|
|
3
|
+
import base64 from 'crypto-js/enc-base64'
|
|
4
|
+
|
|
5
|
+
export function generateAppKey() {
|
|
6
|
+
const random = getRandomValues(new Uint8Array(32))
|
|
7
|
+
const encodedWord = utf8.parse(random.toString())
|
|
8
|
+
const key = base64.stringify(encodedWord)
|
|
9
|
+
|
|
10
|
+
return `base64:${key}`
|
|
11
|
+
}
|