Skip to content

Commit b09a8c4

Browse files
feat(util): md5
1 parent 25a68aa commit b09a8c4

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

packages/util/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export {
1010
} from "./lib/date.js";
1111
export { sanitise } from "./lib/object.js";
1212
export { ISO_6709_RE } from "./lib/regex.js";
13-
export { randomString, slugify, supplant } from "./lib/string.js";
13+
export { md5, randomString, slugify, supplant } from "./lib/string.js";
1414
export { getCanonicalUrl, isSameOrigin } from "./lib/url.js";
1515
export { isRequired } from "./lib/validation-schema.js";

packages/util/lib/string.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { randomBytes } from "node:crypto";
1+
import { randomBytes, createHash } from "node:crypto";
22
import slugifyString from "@sindresorhus/slugify";
33

4+
/**
5+
* Generate MD5 hashed string
6+
* @param {string} string - String
7+
* @returns {string} MD5 hashed string
8+
*/
9+
export const md5 = (string) => createHash("md5").update(string).digest("hex");
10+
411
/**
512
* Generate cryptographically random string
613
* @param {number} [length] - Length of string

packages/util/test/unit/string.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { strict as assert } from "node:assert";
22
import { Buffer } from "node:buffer";
33
import { describe, it } from "node:test";
4-
import { randomString, slugify, supplant } from "../../lib/string.js";
4+
import { md5, randomString, slugify, supplant } from "../../lib/string.js";
55

66
describe("util/lib/string", () => {
7+
it("Generates MD5 hashed string", () => {
8+
const result = md5("foo");
9+
10+
assert.equal(result, "acbd18db4cc2f85cedef654fccc4a4d8");
11+
});
12+
713
it("Generates cryptographically random string", () => {
814
const result = randomString(8);
915

0 commit comments

Comments
 (0)