Skip to content

Commit e146fe2

Browse files
feat(util): sha1 hash
1 parent 231c5fa commit e146fe2

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
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 { md5, randomString, slugify, supplant } from "./lib/string.js";
13+
export { md5, randomString, sha1, 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

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import slugifyString from "@sindresorhus/slugify";
88
*/
99
export const md5 = (string) => createHash("md5").update(string).digest("hex");
1010

11+
/**
12+
* Generate SHA1 hashed string
13+
* @param {string} string - String
14+
* @returns {string} SHA1 hashed string
15+
*/
16+
export const sha1 = (string) => createHash("sha1").update(string).digest("hex");
17+
1118
/**
1219
* Generate cryptographically random string
1320
* @param {number} [length] - Length of string

packages/util/test/unit/string.js

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

612
describe("util/lib/string", () => {
713
it("Generates MD5 hashed string", () => {
@@ -10,6 +16,12 @@ describe("util/lib/string", () => {
1016
assert.equal(result, "acbd18db4cc2f85cedef654fccc4a4d8");
1117
});
1218

19+
it("Generates SHA1 hashed string", () => {
20+
const result = sha1("foo");
21+
22+
assert.equal(result, "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33");
23+
});
24+
1325
it("Generates cryptographically random string", () => {
1426
const result = randomString(8);
1527

0 commit comments

Comments
 (0)