Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcdd86d4

Browse files
author
Thomasr
committed
1024 iteration for deriveKey method
1 parent2d390f5 commitcdd86d4

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

‎server/node-service/src/utils/encryption.ts‎

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,34 @@
1-
import{createDecipheriv,createHash}from"crypto";
1+
import{createDecipheriv,pbkdf2Sync}from"crypto";
22
import{badRequest}from"../common/error";
33

4-
// Spring's Encryptors.text uses AES-256-CBC with a key derived from password and salt (hex).
5-
// The encrypted string format is: hex(salt) + encryptedBase64
6-
// See: https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/encrypt/Encryptors.html
7-
4+
// Spring's Encryptors.text uses AES-256-CBC with PBKDF2 (HmacSHA1, 1024 iterations).
85
constALGORITHM="aes-256-cbc";
96
constKEY_LENGTH=32;// 256 bits
107
constIV_LENGTH=16;// 128 bits
8+
constITERATIONS=1024;
9+
constDIGEST="sha1";
1110

1211
// You must set these to match your Java config:
1312
constPASSWORD=process.env.LOWCODER_NODE_SERVICE_SECRET||"lowcoderpwd";
1413
constSALT_HEX=process.env.LOWCODER_NODE_SERVICE_SECRET_SALT||"lowcodersalt";
1514

1615
/**
17-
* Convert a string to its binary representation, then to a hex string.
18-
*/
19-
functionstringToHexFromBinary(str:string):string{
20-
// Convert string to binary (Buffer), then to hex string
21-
returnBuffer.from(str,"utf8").toString("hex");
22-
}
23-
24-
/**
25-
* Derive key from password and salt using SHA-256 (Spring's default).
16+
* Derive key from password and salt using PBKDF2WithHmacSHA1 (Spring's default).
2617
*/
2718
functionderiveKey(password:string,saltHex:string):Buffer{
28-
// Convert salt string to binary, then to hex string
29-
constsaltHexFromBinary=stringToHexFromBinary(saltHex);
30-
constsalt=Buffer.from(saltHexFromBinary,"hex");
31-
consthash=createHash("sha256");
32-
hash.update(password);
33-
hash.update(salt);
34-
returnhash.digest();
19+
constsalt=Buffer.from(saltHex,"utf8");
20+
returnpbkdf2Sync(password,salt,ITERATIONS,KEY_LENGTH,DIGEST);
3521
}
3622

3723
/**
3824
* Decrypt a string encrypted by Spring's Encryptors.text.
3925
*/
4026
exportasyncfunctiondecryptString(encrypted:string):Promise<string>{
4127
try{
42-
// Spring's format: hex(salt) + encryptedBase64
43-
// But if you know salt, encrypted is just Base64(IV + ciphertext)
28+
// Spring's format: hex(salt) + encryptedHex(IV + ciphertext)
4429
constkey=deriveKey(PASSWORD,SALT_HEX);
4530

46-
// Spring's Encryptors.text prepends a random IV (16 bytes) to the ciphertext, all base64 encoded.
47-
constencryptedBuf=Buffer.from(encrypted,"base64");
31+
constencryptedBuf=Buffer.from(encrypted,"hex");
4832
constiv=encryptedBuf.slice(0,IV_LENGTH);
4933
constciphertext=encryptedBuf.slice(IV_LENGTH);
5034

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp