Skip to content

Commit de626ec

Browse files
authored
fix(core): Use sha256 instead of md5 to generate uuids from string (#619)
1 parent e7f1af4 commit de626ec

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

packages/bundler-plugin-core/src/utils.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,23 @@ function lookupPackageJson(cwd: string, stopAt: string): PackageJson | undefined
173173
* Deterministically hashes a string and turns the hash into a uuid.
174174
*/
175175
export function stringToUUID(str: string): string {
176-
const md5sum = crypto.createHash("md5");
177-
md5sum.update(str);
178-
const md5Hash = md5sum.digest("hex");
176+
const sha256Hash = crypto.createHash("sha256").update(str).digest("hex");
179177

180178
// Position 16 is fixed to either 8, 9, a, or b in the uuid v4 spec (10xx in binary)
181179
// RFC 4122 section 4.4
182-
const v4variant = ["8", "9", "a", "b"][md5Hash.substring(16, 17).charCodeAt(0) % 4] as string;
180+
const v4variant = ["8", "9", "a", "b"][sha256Hash.substring(16, 17).charCodeAt(0) % 4] as string;
183181

184182
return (
185-
md5Hash.substring(0, 8) +
183+
sha256Hash.substring(0, 8) +
186184
"-" +
187-
md5Hash.substring(8, 12) +
185+
sha256Hash.substring(8, 12) +
188186
"-4" +
189-
md5Hash.substring(13, 16) +
187+
sha256Hash.substring(13, 16) +
190188
"-" +
191189
v4variant +
192-
md5Hash.substring(17, 20) +
190+
sha256Hash.substring(17, 20) +
193191
"-" +
194-
md5Hash.substring(20)
192+
sha256Hash.substring(20, 32)
195193
).toLowerCase();
196194
}
197195

packages/bundler-plugin-core/test/utils.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe("getDependencies", () => {
179179

180180
describe("stringToUUID", () => {
181181
test("should return a deterministic UUID", () => {
182-
expect(stringToUUID("Nothing personnel kid")).toBe("52c7a762-5ddf-49a7-af16-6874a8cb2512");
182+
expect(stringToUUID("Nothing personnel kid")).toBe("95543648-7392-49e4-b46a-67dfd0235986");
183183
});
184184
});
185185

0 commit comments

Comments
 (0)