Skip to content

Commit b65ac1d

Browse files
committed
feat(加密): 为加密功能添加盐值支持以增强安全性
在加密和解密过程中添加随机盐值生成,替代原有的固定盐值 修改数据模型、控制器及加密函数以支持盐值存储和使用
1 parent 4bd9f52 commit b65ac1d

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/server/control/data-control.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import { nanoid } from 'nanoid'
44
import { enc, dec } from './enc'
55

66
export async function createData (data: string, userId: string): Promise<Data> {
7-
const { encrypted, iv } = await enc(data)
7+
const { encrypted, iv, salt } = await enc(data)
88
return await DataModel.create({
99
id: nanoid(),
1010
data: encrypted,
1111
userId,
12-
iv
12+
iv,
13+
salt
1314
})
1415
}
1516

1617
export async function getData (id: string): Promise<{ id: string, data: string, userId: string }> {
1718
const data = await DataModel.get(id)
1819
if (data.data !== '{}') {
19-
const decryptedData = await dec(data.data, data.iv)
20+
const decryptedData = await dec(data.data, data.iv, data.salt ?? 'salt')
2021
data.data = decryptedData
2122
}
2223
return {
@@ -27,12 +28,13 @@ export async function getData (id: string): Promise<{ id: string, data: string,
2728
}
2829

2930
export async function updateData (id: string, data: string): Promise<string> {
30-
const { encrypted, iv } = await enc(data)
31+
const { encrypted, iv, salt } = await enc(data)
3132
await DataModel.update({
3233
id
3334
}, {
3435
data: encrypted,
35-
iv
36+
iv,
37+
salt
3638
})
3739
return 'ok'
3840
}

src/server/control/enc.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,26 @@ export const enc = async function (
2121
str: string = '',
2222
password: string = process.env.JWT_SECRET as string,
2323
algorithm: string = algorithmDefault,
24-
iv: Buffer = crypto.randomBytes(16)
25-
): Promise<{ encrypted: string, iv: string }> {
26-
const key = await scryptAsync(password, 'salt', 24)
27-
// Use `crypto.randomBytes` to generate a random iv instead of the static iv
24+
iv: Buffer = crypto.randomBytes(16),
25+
salt: string = crypto.randomBytes(16).toString('hex')
26+
): Promise<{ encrypted: string, iv: string, salt: string }> {
27+
const key = await scryptAsync(password, salt, 24)
2828
const cipher = crypto.createCipheriv(algorithm, key, iv)
2929
let encrypted = cipher.update(str, 'utf8', 'hex')
3030
encrypted += cipher.final('hex')
31-
return { encrypted, iv: iv.toString('hex') }
31+
return { encrypted, iv: iv.toString('hex'), salt }
3232
}
3333

3434
export const dec = async function (
3535
encrypted: string = '',
3636
ivStr: string,
37+
salt: string,
3738
password: string = process.env.JWT_SECRET as string,
3839
algorithm: string = algorithmDefault
3940
): Promise<string> {
4041
const iv = Buffer.from(ivStr, 'hex')
41-
// Use the async `crypto.scrypt()` instead.
42-
const key = await scryptAsync(password, 'salt', 24)
42+
const key = await scryptAsync(password, salt, 24)
4343
const decipher = crypto.createDecipheriv(algorithm, key, iv)
44-
// Encrypted using same algorithm, key and iv.
4544
let decrypted = decipher.update(encrypted, 'hex', 'utf8')
4645
decrypted += decipher.final('utf8')
4746
return decrypted

src/server/models/data-model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Item } from 'dynamoose/dist/Item'
55
export interface Data extends Item {
66
id: string
77
iv: string
8+
salt: string
89
userId: string
910
data: string
1011
}
@@ -23,5 +24,8 @@ export const dataSchema = {
2324
},
2425
iv: {
2526
type: String
27+
},
28+
salt: {
29+
type: String
2630
}
2731
}

0 commit comments

Comments
 (0)