@sd-jwt/hash 0.12.1-next.0 → 0.12.1-next.2

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/dist/index.d.mts CHANGED
@@ -1,4 +1,7 @@
1
- declare const sha256: (text: string) => Uint8Array;
2
- declare const hasher: (data: string, algorithm: string) => Uint8Array;
1
+ declare const sha256: (text: string | ArrayBuffer) => Uint8Array;
2
+ declare const sha384: (text: string | ArrayBuffer) => Uint8Array;
3
+ declare const sha512: (text: string | ArrayBuffer) => Uint8Array;
4
+ type HasherAlgorithm = 'sha256' | 'sha384' | 'sha512' | (string & {});
5
+ declare const hasher: (data: string | ArrayBuffer, algorithm?: HasherAlgorithm) => Uint8Array;
3
6
 
4
- export { hasher, sha256 };
7
+ export { hasher, sha256, sha384, sha512 };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- declare const sha256: (text: string) => Uint8Array;
2
- declare const hasher: (data: string, algorithm: string) => Uint8Array;
1
+ declare const sha256: (text: string | ArrayBuffer) => Uint8Array;
2
+ declare const sha384: (text: string | ArrayBuffer) => Uint8Array;
3
+ declare const sha512: (text: string | ArrayBuffer) => Uint8Array;
4
+ type HasherAlgorithm = 'sha256' | 'sha384' | 'sha512' | (string & {});
5
+ declare const hasher: (data: string | ArrayBuffer, algorithm?: HasherAlgorithm) => Uint8Array;
3
6
 
4
- export { hasher, sha256 };
7
+ export { hasher, sha256, sha384, sha512 };
package/dist/index.js CHANGED
@@ -21,23 +21,43 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  hasher: () => hasher,
24
- sha256: () => sha256
24
+ sha256: () => sha256,
25
+ sha384: () => sha384,
26
+ sha512: () => sha512
25
27
  });
26
28
  module.exports = __toCommonJS(index_exports);
27
29
 
28
- // src/sha256.ts
29
- var import_sha256 = require("@noble/hashes/sha256");
30
+ // src/sha.ts
31
+ var import_sha2 = require("@noble/hashes/sha2.js");
30
32
  var import_utils = require("@sd-jwt/utils");
31
33
  var sha256 = (text) => {
32
- const uint8Array = toUTF8Array(text);
33
- const hashBytes = (0, import_sha256.sha256)(uint8Array);
34
+ const uint8Array = typeof text === "string" ? toUTF8Array(text) : new Uint8Array(text);
35
+ const hashBytes = (0, import_sha2.sha256)(uint8Array);
34
36
  return hashBytes;
35
37
  };
36
- var hasher = (data, algorithm) => {
37
- if (toCryptoAlg(algorithm) !== "sha256") {
38
- throw new import_utils.SDJWTException("Not implemented");
38
+ var sha384 = (text) => {
39
+ const uint8Array = typeof text === "string" ? toUTF8Array(text) : new Uint8Array(text);
40
+ const hashBytes = (0, import_sha2.sha384)(uint8Array);
41
+ return hashBytes;
42
+ };
43
+ var sha512 = (text) => {
44
+ const uint8Array = typeof text === "string" ? toUTF8Array(text) : new Uint8Array(text);
45
+ const hashBytes = (0, import_sha2.sha512)(uint8Array);
46
+ return hashBytes;
47
+ };
48
+ var hasher = (data, algorithm = "sha256") => {
49
+ const msg = typeof data === "string" ? toUTF8Array(data) : new Uint8Array(data);
50
+ const alg = toCryptoAlg(algorithm);
51
+ switch (alg) {
52
+ case "sha256":
53
+ return sha256(msg);
54
+ case "sha384":
55
+ return sha384(msg);
56
+ case "sha512":
57
+ return sha512(msg);
58
+ default:
59
+ throw new import_utils.SDJWTException(`Unsupported algorithm: ${algorithm}`);
39
60
  }
40
- return sha256(data);
41
61
  };
42
62
  var toCryptoAlg = (hashAlg) => (
43
63
  // To cover sha-256, sha256, SHA-256, SHA256
@@ -72,5 +92,7 @@ function toUTF8Array(str) {
72
92
  // Annotate the CommonJS export names for ESM import in node:
73
93
  0 && (module.exports = {
74
94
  hasher,
75
- sha256
95
+ sha256,
96
+ sha384,
97
+ sha512
76
98
  });
package/dist/index.mjs CHANGED
@@ -1,16 +1,38 @@
1
- // src/sha256.ts
2
- import { sha256 as nobleSha256 } from "@noble/hashes/sha256";
1
+ // src/sha.ts
2
+ import {
3
+ sha256 as nobleSha256,
4
+ sha384 as nobleSha384,
5
+ sha512 as nobleSha512
6
+ } from "@noble/hashes/sha2.js";
3
7
  import { SDJWTException } from "@sd-jwt/utils";
4
8
  var sha256 = (text) => {
5
- const uint8Array = toUTF8Array(text);
9
+ const uint8Array = typeof text === "string" ? toUTF8Array(text) : new Uint8Array(text);
6
10
  const hashBytes = nobleSha256(uint8Array);
7
11
  return hashBytes;
8
12
  };
9
- var hasher = (data, algorithm) => {
10
- if (toCryptoAlg(algorithm) !== "sha256") {
11
- throw new SDJWTException("Not implemented");
13
+ var sha384 = (text) => {
14
+ const uint8Array = typeof text === "string" ? toUTF8Array(text) : new Uint8Array(text);
15
+ const hashBytes = nobleSha384(uint8Array);
16
+ return hashBytes;
17
+ };
18
+ var sha512 = (text) => {
19
+ const uint8Array = typeof text === "string" ? toUTF8Array(text) : new Uint8Array(text);
20
+ const hashBytes = nobleSha512(uint8Array);
21
+ return hashBytes;
22
+ };
23
+ var hasher = (data, algorithm = "sha256") => {
24
+ const msg = typeof data === "string" ? toUTF8Array(data) : new Uint8Array(data);
25
+ const alg = toCryptoAlg(algorithm);
26
+ switch (alg) {
27
+ case "sha256":
28
+ return sha256(msg);
29
+ case "sha384":
30
+ return sha384(msg);
31
+ case "sha512":
32
+ return sha512(msg);
33
+ default:
34
+ throw new SDJWTException(`Unsupported algorithm: ${algorithm}`);
12
35
  }
13
- return sha256(data);
14
36
  };
15
37
  var toCryptoAlg = (hashAlg) => (
16
38
  // To cover sha-256, sha256, SHA-256, SHA256
@@ -44,5 +66,7 @@ function toUTF8Array(str) {
44
66
  }
45
67
  export {
46
68
  hasher,
47
- sha256
69
+ sha256,
70
+ sha384,
71
+ sha512
48
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sd-jwt/hash",
3
- "version": "0.12.1-next.0+36e0b22",
3
+ "version": "0.12.1-next.2+1eefb26",
4
4
  "description": "sd-jwt draft 7 implementation in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -38,11 +38,11 @@
38
38
  },
39
39
  "license": "Apache-2.0",
40
40
  "devDependencies": {
41
- "@sd-jwt/crypto-nodejs": "0.12.1-next.0+36e0b22"
41
+ "@sd-jwt/crypto-nodejs": "0.12.1-next.2+1eefb26"
42
42
  },
43
43
  "dependencies": {
44
- "@noble/hashes": "1.0.0",
45
- "@sd-jwt/utils": "0.12.1-next.0+36e0b22"
44
+ "@noble/hashes": "^1.8.0",
45
+ "@sd-jwt/utils": "0.12.1-next.2+1eefb26"
46
46
  },
47
47
  "publishConfig": {
48
48
  "access": "public"
@@ -60,5 +60,5 @@
60
60
  "esm"
61
61
  ]
62
62
  },
63
- "gitHead": "36e0b22eb619f3ca9ae8522a26617e50c3680526"
63
+ "gitHead": "1eefb262c40ea23e999cdef6e75222e5b4df1e2c"
64
64
  }
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export * from './sha256';
1
+ export * from './sha';
package/src/sha.ts ADDED
@@ -0,0 +1,89 @@
1
+ import {
2
+ sha256 as nobleSha256,
3
+ sha384 as nobleSha384,
4
+ sha512 as nobleSha512,
5
+ } from '@noble/hashes/sha2.js';
6
+ import { SDJWTException } from '@sd-jwt/utils';
7
+
8
+ export const sha256 = (text: string | ArrayBuffer): Uint8Array => {
9
+ const uint8Array =
10
+ typeof text === 'string' ? toUTF8Array(text) : new Uint8Array(text);
11
+ const hashBytes = nobleSha256(uint8Array);
12
+ return hashBytes;
13
+ };
14
+
15
+ export const sha384 = (text: string | ArrayBuffer): Uint8Array => {
16
+ const uint8Array =
17
+ typeof text === 'string' ? toUTF8Array(text) : new Uint8Array(text);
18
+ const hashBytes = nobleSha384(uint8Array);
19
+ return hashBytes;
20
+ };
21
+
22
+ export const sha512 = (text: string | ArrayBuffer): Uint8Array => {
23
+ const uint8Array =
24
+ typeof text === 'string' ? toUTF8Array(text) : new Uint8Array(text);
25
+ const hashBytes = nobleSha512(uint8Array);
26
+ return hashBytes;
27
+ };
28
+
29
+ type HasherAlgorithm = 'sha256' | 'sha384' | 'sha512' | (string & {});
30
+
31
+ export const hasher = (
32
+ data: string | ArrayBuffer,
33
+ algorithm: HasherAlgorithm = 'sha256',
34
+ ) => {
35
+ const msg =
36
+ typeof data === 'string' ? toUTF8Array(data) : new Uint8Array(data);
37
+
38
+ const alg = toCryptoAlg(algorithm);
39
+
40
+ switch (alg) {
41
+ case 'sha256':
42
+ return sha256(msg);
43
+ case 'sha384':
44
+ return sha384(msg);
45
+ case 'sha512':
46
+ return sha512(msg);
47
+ default:
48
+ throw new SDJWTException(`Unsupported algorithm: ${algorithm}`);
49
+ }
50
+ };
51
+
52
+ const toCryptoAlg = (hashAlg: HasherAlgorithm): string =>
53
+ // To cover sha-256, sha256, SHA-256, SHA256
54
+ hashAlg
55
+ .replace('-', '')
56
+ .toLowerCase();
57
+
58
+ function toUTF8Array(str: string) {
59
+ const utf8: Array<number> = [];
60
+ for (let i = 0; i < str.length; i++) {
61
+ let charcode = str.charCodeAt(i);
62
+ if (charcode < 0x80) utf8.push(charcode);
63
+ else if (charcode < 0x800) {
64
+ utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f));
65
+ } else if (charcode < 0xd800 || charcode >= 0xe000) {
66
+ utf8.push(
67
+ 0xe0 | (charcode >> 12),
68
+ 0x80 | ((charcode >> 6) & 0x3f),
69
+ 0x80 | (charcode & 0x3f),
70
+ );
71
+ }
72
+ // surrogate pair
73
+ else {
74
+ i++;
75
+ // UTF-16 encodes 0x10000-0x10FFFF by
76
+ // subtracting 0x10000 and splitting the
77
+ // 20 bits of 0x0-0xFFFFF into two halves
78
+ charcode =
79
+ 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
80
+ utf8.push(
81
+ 0xf0 | (charcode >> 18),
82
+ 0x80 | ((charcode >> 12) & 0x3f),
83
+ 0x80 | ((charcode >> 6) & 0x3f),
84
+ 0x80 | (charcode & 0x3f),
85
+ );
86
+ }
87
+ }
88
+ return new Uint8Array(utf8);
89
+ }
@@ -2,8 +2,9 @@ import { digest } from '@sd-jwt/crypto-nodejs';
2
2
  import { bytesToHex } from '@noble/hashes/utils';
3
3
  import { hasher, sha256 } from '../index';
4
4
  import { describe, expect, test } from 'vitest';
5
+ import { createHash } from 'node:crypto';
5
6
 
6
- describe('SHA-256 tests', () => {
7
+ describe('hashing tests', () => {
7
8
  test('test#1', async () => {
8
9
  const payload = 'test1';
9
10
  const s1 = bytesToHex(await digest(payload));
@@ -123,4 +124,66 @@ describe('SHA-256 tests', () => {
123
124
  expect(e).toBeInstanceOf(Error);
124
125
  }
125
126
  });
127
+
128
+ describe('Hash', () => {
129
+ (process.env.npm_lifecycle_event === 'test:browser' ? test.skip : test)(
130
+ 'sha256 - string',
131
+ () => {
132
+ const data = 'test';
133
+ const hashdata = hasher(data, 'sha-256');
134
+ const hashdata2 = createHash('sha256').update(data).digest();
135
+ expect(bytesToHex(hashdata)).toEqual(bytesToHex(hashdata2));
136
+ },
137
+ );
138
+
139
+ (process.env.npm_lifecycle_event === 'test:browser' ? test.skip : test)(
140
+ 'sha256 - arraybuffer',
141
+ () => {
142
+ const data = new TextEncoder().encode('test');
143
+ const hashdata = hasher(data, 'sha-256');
144
+ const hashdata2 = createHash('sha256').update(data).digest();
145
+ expect(bytesToHex(hashdata)).toEqual(bytesToHex(hashdata2));
146
+ },
147
+ );
148
+
149
+ (process.env.npm_lifecycle_event === 'test:browser' ? test.skip : test)(
150
+ 'sha-384 - string',
151
+ () => {
152
+ const data = 'test';
153
+ const hashdata = hasher(data, 'sha-384');
154
+ const hashdata2 = createHash('sha384').update(data).digest();
155
+ expect(bytesToHex(hashdata)).toEqual(bytesToHex(hashdata2));
156
+ },
157
+ );
158
+
159
+ (process.env.npm_lifecycle_event === 'test:browser' ? test.skip : test)(
160
+ 'sha-384 - arraybuffer',
161
+ () => {
162
+ const data = new TextEncoder().encode('test');
163
+ const hashdata = hasher(data, 'sha-384');
164
+ const hashdata2 = createHash('sha384').update(data).digest();
165
+ expect(bytesToHex(hashdata)).toEqual(bytesToHex(hashdata2));
166
+ },
167
+ );
168
+
169
+ (process.env.npm_lifecycle_event === 'test:browser' ? test.skip : test)(
170
+ 'sha-512 - string',
171
+ () => {
172
+ const data = 'test';
173
+ const hashdata = hasher(data, 'sha-512');
174
+ const hashdata2 = createHash('sha512').update(data).digest();
175
+ expect(bytesToHex(hashdata)).toEqual(bytesToHex(hashdata2));
176
+ },
177
+ );
178
+
179
+ (process.env.npm_lifecycle_event === 'test:browser' ? test.skip : test)(
180
+ 'sha-512 - arraybuffer',
181
+ () => {
182
+ const data = new TextEncoder().encode('test');
183
+ const hashdata = hasher(data, 'sha-512');
184
+ const hashdata2 = createHash('sha512').update(data).digest();
185
+ expect(bytesToHex(hashdata)).toEqual(bytesToHex(hashdata2));
186
+ },
187
+ );
188
+ });
126
189
  });
package/src/sha256.ts DELETED
@@ -1,54 +0,0 @@
1
- import { sha256 as nobleSha256 } from '@noble/hashes/sha256';
2
- import { SDJWTException } from '@sd-jwt/utils';
3
-
4
- export const sha256 = (text: string): Uint8Array => {
5
- const uint8Array = toUTF8Array(text);
6
- const hashBytes = nobleSha256(uint8Array);
7
- return hashBytes;
8
- };
9
-
10
- export const hasher = (data: string, algorithm: string) => {
11
- if (toCryptoAlg(algorithm) !== 'sha256') {
12
- throw new SDJWTException('Not implemented');
13
- }
14
- return sha256(data);
15
- };
16
-
17
- const toCryptoAlg = (hashAlg: string): string =>
18
- // To cover sha-256, sha256, SHA-256, SHA256
19
- hashAlg
20
- .replace('-', '')
21
- .toLowerCase();
22
-
23
- function toUTF8Array(str: string) {
24
- const utf8: Array<number> = [];
25
- for (let i = 0; i < str.length; i++) {
26
- let charcode = str.charCodeAt(i);
27
- if (charcode < 0x80) utf8.push(charcode);
28
- else if (charcode < 0x800) {
29
- utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f));
30
- } else if (charcode < 0xd800 || charcode >= 0xe000) {
31
- utf8.push(
32
- 0xe0 | (charcode >> 12),
33
- 0x80 | ((charcode >> 6) & 0x3f),
34
- 0x80 | (charcode & 0x3f),
35
- );
36
- }
37
- // surrogate pair
38
- else {
39
- i++;
40
- // UTF-16 encodes 0x10000-0x10FFFF by
41
- // subtracting 0x10000 and splitting the
42
- // 20 bits of 0x0-0xFFFFF into two halves
43
- charcode =
44
- 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
45
- utf8.push(
46
- 0xf0 | (charcode >> 18),
47
- 0x80 | ((charcode >> 12) & 0x3f),
48
- 0x80 | ((charcode >> 6) & 0x3f),
49
- 0x80 | (charcode & 0x3f),
50
- );
51
- }
52
- }
53
- return new Uint8Array(utf8);
54
- }