@checkdigit/hash 0.0.2 → 1.0.0-PR.14-e694

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+
2
+ The MIT License
3
+
4
+ Copyright (c) 2021-2023 Check Digit, LLC
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Hash
2
2
 
3
+ Copyright (c) 2021 [Check Digit, LLC](https://checkdigit.com)
4
+
3
5
  Hash is a small function for Check Digit services to create a UUID derived from a hash generated from a given value.
4
6
 
5
7
  ### Install
@@ -7,7 +9,6 @@ Hash is a small function for Check Digit services to create a UUID derived from
7
9
  ```
8
10
  $ npm install @checkdigit/hash
9
11
  ```
10
-
11
12
 
12
13
  ### Use
13
14
 
@@ -17,5 +18,6 @@ import { hash } from '@checkdigit/hash';
17
18
  const derivedUUID = hash('some value');
18
19
  ```
19
20
 
20
- Check Digit: https://checkdigit.com
21
-
21
+ ## License
22
+
23
+ MIT
package/package.json CHANGED
@@ -1,59 +1 @@
1
- {
2
- "name": "@checkdigit/hash",
3
- "version": "0.0.2",
4
- "description": "Hash is a small function for Check Digit services to create a uuid derived from a hash generated from a given value.",
5
- "typings": "./dist/index.d.ts",
6
- "main": "./dist/index.js",
7
- "prettier": "@checkdigit/prettier-config",
8
- "engines": {
9
- "node": ">=12.18",
10
- "npm": ">=6.14"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/checkdigit/hash.git"
15
- },
16
- "author": "Check Digit, LLC",
17
- "license": "UNLICENSED",
18
- "bugs": {
19
- "url": "https://github.com/checkdigit/hash/issues"
20
- },
21
- "homepage": "https://github.com/checkdigit/hash#readme",
22
- "devDependencies": {
23
- "@checkdigit/eslint-config": "3.3.5",
24
- "@checkdigit/prettier-config": "^1.0.1",
25
- "@checkdigit/publish": "^1.0.14",
26
- "@checkdigit/typescript": "^3.9.3",
27
- "@types/jest": "^26.0.0",
28
- "@types/uuid": "^8.3.0",
29
- "eslint": "^7.3.0",
30
- "jest": "^26.0.1",
31
- "prettier": "^2.0.5",
32
- "rimraf": "^3.0.2",
33
- "ts-jest": "^26.1.1",
34
- "ts-node": "^8.10.2"
35
- },
36
- "jest": {
37
- "collectCoverage": true,
38
- "preset": "ts-jest",
39
- "testPathIgnorePatterns": [
40
- "<rootDir>/build/",
41
- "<rootDir>/node_modules/"
42
- ]
43
- },
44
- "scripts": {
45
- "prepublishOnly": "publish",
46
- "postpublish": "rimraf dist .npmignore",
47
- "preversion": "npm test",
48
- "postversion": "git push && git push --tags",
49
- "lint": "eslint -f unix 'src/**/*.ts'",
50
- "lint:fix": "eslint -f unix 'src/**/*.ts' --fix",
51
- "prettier": "prettier --list-different 'src/**/*.ts'",
52
- "prettier:fix": "prettier --write 'src/**/*.ts'",
53
- "test": "jest && npm run lint && npm run prettier"
54
- },
55
- "dependencies": {
56
- "crypto": "^1.0.1",
57
- "uuid": "^8.3.0"
58
- }
59
- }
1
+ {"name":"@checkdigit/hash","version":"1.0.0-PR.14-e694","description":"Hash is a small function for Check Digit services to create a uuid derived from a hash generated from a given value.","typings":"./dist/index.d.ts","main":"./dist/index.js","files":["index.js","/src/"],"prettier":"@checkdigit/prettier-config","engines":{"node":">=16"},"repository":{"type":"git","url":"git+https://github.com/checkdigit/hash.git"},"author":"Check Digit, LLC","license":"MIT","bugs":{"url":"https://github.com/checkdigit/hash/issues"},"homepage":"https://github.com/checkdigit/hash#readme","devDependencies":{"@checkdigit/eslint-config":"^7.8.0","@checkdigit/jest-config":"^2.2.1","@checkdigit/prettier-config":"^3.4.0","@checkdigit/typescript-config":"^3.4.0","@types/uuid":"^9.0.1","rimraf":"^5.0.1","uuid":"^9.0.0"},"eslintConfig":{"extends":["@checkdigit/eslint-config"]},"jest":{"preset":"@checkdigit/jest-config"},"scripts":{"prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","lint:fix":"eslint -f unix --ext .ts,.mts src --fix","lint":"eslint -f unix --ext .ts,.mts src","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style","ci:compile":"tsc --noEmit","ci:test":"jest --coverage=false","ci:coverage":"jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier"}}
package/src/hash.ts ADDED
@@ -0,0 +1,22 @@
1
+ // hash.ts
2
+
3
+ /*
4
+ * Copyright (c) 2021-2023 Check Digit, LLC
5
+ *
6
+ * This code is licensed under the MIT license (see LICENSE.txt for details).
7
+ */
8
+
9
+ import { createHash } from 'node:crypto';
10
+
11
+ import uuidV5 from './uuid-v5';
12
+
13
+ // eslint-disable-next-line @checkdigit/no-uuid
14
+ export const HASH_NAMESPACE = 'f25d4515-fea7-44c7-8baf-f3ca50865e66';
15
+
16
+ /**
17
+ * @param value - a string to be hashed and converted into a derived uuid
18
+ */
19
+
20
+ export default function (value: string): string {
21
+ return uuidV5(createHash('sha512').update(value).digest('base64'), HASH_NAMESPACE);
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ // index.ts
2
+
3
+ /*
4
+ * Copyright (c) 2021-2023 Check Digit, LLC
5
+ *
6
+ * This code is licensed under the MIT license (see LICENSE.txt for details).
7
+ */
8
+
9
+ export { default } from './hash';
10
+ export { default as hash, HASH_NAMESPACE } from './hash';
package/src/uuid-v5.ts ADDED
@@ -0,0 +1,77 @@
1
+ // uuid-v5.ts
2
+
3
+ /*
4
+ * Copyright (c) 2021 Check Digit, LLC
5
+ *
6
+ * This code is derived from https://github.com/uuidjs/uuid,
7
+ * licensed under the MIT license (see below for details).
8
+ *
9
+ * Copyright (c) 2010-2020 Robert Kieffer and other contributors
10
+ *
11
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ * of this software and associated documentation files (the "Software"), to deal
13
+ * in the Software without restriction, including without limitation the rights
14
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ * copies of the Software, and to permit persons to whom the Software is
16
+ * furnished to do so, subject to the following conditions:
17
+ *
18
+ * The above copyright notice and this permission notice shall be included in
19
+ * all copies or substantial portions of the Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
+ * THE SOFTWARE.
28
+ */
29
+
30
+ import { createHash } from 'node:crypto';
31
+
32
+ /* eslint-disable no-bitwise,no-magic-numbers */
33
+ function parse(namespace: string) {
34
+ let value;
35
+ const intArray = new Uint8Array(16);
36
+
37
+ // Parse ########-....-....-....-............
38
+ intArray[0] = (value = Number.parseInt(namespace.slice(0, 8), 16)) >>> 24;
39
+ intArray[1] = (value >>> 16) & 0xff;
40
+ intArray[2] = (value >>> 8) & 0xff;
41
+ intArray[3] = value & 0xff;
42
+
43
+ // Parse ........-####-....-....-............
44
+ intArray[4] = (value = Number.parseInt(namespace.slice(9, 13), 16)) >>> 8;
45
+ intArray[5] = value & 0xff;
46
+
47
+ // Parse ........-....-####-....-............
48
+ intArray[6] = (value = Number.parseInt(namespace.slice(14, 18), 16)) >>> 8;
49
+ intArray[7] = value & 0xff;
50
+
51
+ // Parse ........-....-....-####-............
52
+ intArray[8] = (value = Number.parseInt(namespace.slice(19, 23), 16)) >>> 8;
53
+ intArray[9] = value & 0xff;
54
+
55
+ // Parse ........-....-....-....-############
56
+ // (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
57
+ intArray[10] = ((value = Number.parseInt(namespace.slice(24, 36), 16)) / 0x1_00_00_00_00_00) & 0xff;
58
+ intArray[11] = (value / 0x1_00_00_00_00) & 0xff;
59
+ intArray[12] = (value >>> 24) & 0xff;
60
+ intArray[13] = (value >>> 16) & 0xff;
61
+ intArray[14] = (value >>> 8) & 0xff;
62
+ intArray[15] = value & 0xff;
63
+
64
+ return intArray;
65
+ }
66
+
67
+ export default function (value: string, namespace: string): string {
68
+ const buffer = createHash('sha1').update(parse(namespace)).update(value).digest();
69
+ // note that undefined is treated as 0 by the & operator
70
+ buffer[6] = ((buffer[6] ?? 0) & 0x0f) | 0x50;
71
+ buffer[8] = ((buffer[8] ?? 0) & 0x3f) | 0x80;
72
+ const hex = buffer.toString('hex', 0, 16);
73
+ return [hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20, 32)]
74
+ .join('-')
75
+ .toLowerCase();
76
+ }
77
+ /* eslint-enable no-bitwise,no-magic-numbers */
package/dist/hash.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * @param value - a string to be hashed and converted into a derived uuid
3
- */
4
- export default function (value: string): string;
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { default as hash } from './hash';
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("crypto"),r=require("uuid");exports.hash=function(t){return r.v5(e.createHash("sha512").update(t).digest("base64"),"f25d4515-fea7-44c7-8baf-f3ca50865e66")};