Skip to content

Commit cdd86d4

Browse files
committed
1024 iteration for deriveKey method
1 parent 2d390f5 commit cdd86d4

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

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

+9-25
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
const ALGORITHM = "aes-256-cbc";
96
const KEY_LENGTH = 32; // 256 bits
107
const IV_LENGTH = 16; // 128 bits
8+
const ITERATIONS = 1024;
9+
const DIGEST = "sha1";
1110

1211
// You must set these to match your Java config:
1312
const PASSWORD = process.env.LOWCODER_NODE_SERVICE_SECRET || "lowcoderpwd";
1413
const SALT_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-
function stringToHexFromBinary(str: string): string {
20-
// Convert string to binary (Buffer), then to hex string
21-
return Buffer.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
function deriveKey(password: string, saltHex: string): Buffer {
28-
// Convert salt string to binary, then to hex string
29-
const saltHexFromBinary = stringToHexFromBinary(saltHex);
30-
const salt = Buffer.from(saltHexFromBinary, "hex");
31-
const hash = createHash("sha256");
32-
hash.update(password);
33-
hash.update(salt);
34-
return hash.digest();
19+
const salt = Buffer.from(saltHex, "utf8");
20+
return pbkdf2Sync(password, salt, ITERATIONS, KEY_LENGTH, DIGEST);
3521
}
3622

3723
/**
3824
* Decrypt a string encrypted by Spring's Encryptors.text.
3925
*/
4026
export async function decryptString(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
const key = deriveKey(PASSWORD, SALT_HEX);
4530

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

0 commit comments

Comments
 (0)