@sd-jwt/crypto-browser 0.17.2-next.0 → 0.17.2-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
@@ -3,6 +3,54 @@ declare function digest(data: string | ArrayBuffer, algorithm?: string): Promise
3
3
  declare const getHasher: (algorithm?: string) => (data: string) => Promise<Uint8Array<ArrayBufferLike>>;
4
4
  declare const ES256: {
5
5
  alg: string;
6
+ _keyAlgorithm: {
7
+ name: string;
8
+ namedCurve: string;
9
+ };
10
+ _hashAlgorithm: {
11
+ name: string;
12
+ hash: {
13
+ name: string;
14
+ };
15
+ };
16
+ generateKeyPair(): Promise<{
17
+ publicKey: JsonWebKey;
18
+ privateKey: JsonWebKey;
19
+ }>;
20
+ getSigner(privateKeyJWK: object): Promise<(data: string) => Promise<string>>;
21
+ getVerifier(publicKeyJWK: object): Promise<(data: string, signatureBase64url: string) => Promise<boolean>>;
22
+ };
23
+ declare const ES384: {
24
+ alg: string;
25
+ _keyAlgorithm: {
26
+ name: string;
27
+ namedCurve: string;
28
+ };
29
+ _hashAlgorithm: {
30
+ name: string;
31
+ hash: {
32
+ name: string;
33
+ };
34
+ };
35
+ generateKeyPair(): Promise<{
36
+ publicKey: JsonWebKey;
37
+ privateKey: JsonWebKey;
38
+ }>;
39
+ getSigner(privateKeyJWK: object): Promise<(data: string) => Promise<string>>;
40
+ getVerifier(publicKeyJWK: object): Promise<(data: string, signatureBase64url: string) => Promise<boolean>>;
41
+ };
42
+ declare const ES512: {
43
+ alg: string;
44
+ _keyAlgorithm: {
45
+ name: string;
46
+ namedCurve: string;
47
+ };
48
+ _hashAlgorithm: {
49
+ name: string;
50
+ hash: {
51
+ name: string;
52
+ };
53
+ };
6
54
  generateKeyPair(): Promise<{
7
55
  publicKey: JsonWebKey;
8
56
  privateKey: JsonWebKey;
@@ -11,4 +59,4 @@ declare const ES256: {
11
59
  getVerifier(publicKeyJWK: object): Promise<(data: string, signatureBase64url: string) => Promise<boolean>>;
12
60
  };
13
61
 
14
- export { ES256, digest, generateSalt, getHasher };
62
+ export { ES256, ES384, ES512, digest, generateSalt, getHasher };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,54 @@ declare function digest(data: string | ArrayBuffer, algorithm?: string): Promise
3
3
  declare const getHasher: (algorithm?: string) => (data: string) => Promise<Uint8Array<ArrayBufferLike>>;
4
4
  declare const ES256: {
5
5
  alg: string;
6
+ _keyAlgorithm: {
7
+ name: string;
8
+ namedCurve: string;
9
+ };
10
+ _hashAlgorithm: {
11
+ name: string;
12
+ hash: {
13
+ name: string;
14
+ };
15
+ };
16
+ generateKeyPair(): Promise<{
17
+ publicKey: JsonWebKey;
18
+ privateKey: JsonWebKey;
19
+ }>;
20
+ getSigner(privateKeyJWK: object): Promise<(data: string) => Promise<string>>;
21
+ getVerifier(publicKeyJWK: object): Promise<(data: string, signatureBase64url: string) => Promise<boolean>>;
22
+ };
23
+ declare const ES384: {
24
+ alg: string;
25
+ _keyAlgorithm: {
26
+ name: string;
27
+ namedCurve: string;
28
+ };
29
+ _hashAlgorithm: {
30
+ name: string;
31
+ hash: {
32
+ name: string;
33
+ };
34
+ };
35
+ generateKeyPair(): Promise<{
36
+ publicKey: JsonWebKey;
37
+ privateKey: JsonWebKey;
38
+ }>;
39
+ getSigner(privateKeyJWK: object): Promise<(data: string) => Promise<string>>;
40
+ getVerifier(publicKeyJWK: object): Promise<(data: string, signatureBase64url: string) => Promise<boolean>>;
41
+ };
42
+ declare const ES512: {
43
+ alg: string;
44
+ _keyAlgorithm: {
45
+ name: string;
46
+ namedCurve: string;
47
+ };
48
+ _hashAlgorithm: {
49
+ name: string;
50
+ hash: {
51
+ name: string;
52
+ };
53
+ };
6
54
  generateKeyPair(): Promise<{
7
55
  publicKey: JsonWebKey;
8
56
  privateKey: JsonWebKey;
@@ -11,4 +59,4 @@ declare const ES256: {
11
59
  getVerifier(publicKeyJWK: object): Promise<(data: string, signatureBase64url: string) => Promise<boolean>>;
12
60
  };
13
61
 
14
- export { ES256, digest, generateSalt, getHasher };
62
+ export { ES256, ES384, ES512, digest, generateSalt, getHasher };
package/dist/index.js CHANGED
@@ -41,6 +41,8 @@ var __async = (__this, __arguments, generator) => {
41
41
  var index_exports = {};
42
42
  __export(index_exports, {
43
43
  ES256: () => ES256,
44
+ ES384: () => ES384,
45
+ ES512: () => ES512,
44
46
  digest: () => digest,
45
47
  generateSalt: () => generateSalt,
46
48
  getHasher: () => getHasher
@@ -53,7 +55,7 @@ var generateSalt = (length) => {
53
55
  return "";
54
56
  }
55
57
  const array = new Uint8Array(length / 2);
56
- window.crypto.getRandomValues(array);
58
+ globalThis.crypto.getRandomValues(array);
57
59
  const salt = Array.from(
58
60
  array,
59
61
  (byte) => byte.toString(16).padStart(2, "0")
@@ -63,7 +65,7 @@ var generateSalt = (length) => {
63
65
  function digest(data, algorithm = "sha-256") {
64
66
  return __async(this, null, function* () {
65
67
  const ec = new TextEncoder();
66
- const digest2 = yield window.crypto.subtle.digest(
68
+ const digest2 = yield globalThis.crypto.subtle.digest(
67
69
  algorithm,
68
70
  typeof data === "string" ? ec.encode(data) : data
69
71
  );
@@ -73,99 +75,180 @@ function digest(data, algorithm = "sha-256") {
73
75
  var getHasher = (algorithm = "sha-256") => {
74
76
  return (data) => digest(data, algorithm);
75
77
  };
78
+ function generateKeyPair(keyAlgorithm) {
79
+ return __async(this, null, function* () {
80
+ const keyPair = yield globalThis.crypto.subtle.generateKey(
81
+ keyAlgorithm,
82
+ true,
83
+ // whether the key is extractable (i.e., can be used in exportKey)
84
+ ["sign", "verify"]
85
+ // can be used to sign and verify signatures
86
+ );
87
+ const publicKeyJWK = yield globalThis.crypto.subtle.exportKey(
88
+ "jwk",
89
+ keyPair.publicKey
90
+ );
91
+ const privateKeyJWK = yield globalThis.crypto.subtle.exportKey(
92
+ "jwk",
93
+ keyPair.privateKey
94
+ );
95
+ return { publicKey: publicKeyJWK, privateKey: privateKeyJWK };
96
+ });
97
+ }
98
+ function getSigner(privateKeyJWK, keyAlgorithm, signAlgorithm) {
99
+ return __async(this, null, function* () {
100
+ const privateKey = yield globalThis.crypto.subtle.importKey(
101
+ "jwk",
102
+ privateKeyJWK,
103
+ keyAlgorithm,
104
+ true,
105
+ // whether the key is extractable (i.e., can be used in exportKey)
106
+ ["sign"]
107
+ );
108
+ return (data) => __async(null, null, function* () {
109
+ const encoder = new TextEncoder();
110
+ const signature = yield globalThis.crypto.subtle.sign(
111
+ signAlgorithm,
112
+ privateKey,
113
+ encoder.encode(data)
114
+ );
115
+ return btoa(String.fromCharCode(...new Uint8Array(signature))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
116
+ });
117
+ });
118
+ }
119
+ function getVerifier(publicKeyJWK, keyAlgorithm, verifyAlgorithm) {
120
+ return __async(this, null, function* () {
121
+ const publicKey = yield globalThis.crypto.subtle.importKey(
122
+ "jwk",
123
+ publicKeyJWK,
124
+ keyAlgorithm,
125
+ true,
126
+ // whether the key is extractable (i.e., can be used in exportKey)
127
+ ["verify"]
128
+ );
129
+ return (data, signatureBase64url) => __async(null, null, function* () {
130
+ const encoder = new TextEncoder();
131
+ const signature = Uint8Array.from(
132
+ atob(signatureBase64url.replace(/-/g, "+").replace(/_/g, "/")),
133
+ (c) => c.charCodeAt(0)
134
+ );
135
+ const isValid = yield globalThis.crypto.subtle.verify(
136
+ verifyAlgorithm,
137
+ publicKey,
138
+ signature,
139
+ encoder.encode(data)
140
+ );
141
+ return isValid;
142
+ });
143
+ });
144
+ }
76
145
  var ES256 = {
77
146
  alg: "ES256",
147
+ _keyAlgorithm: {
148
+ name: "ECDSA",
149
+ namedCurve: "P-256"
150
+ },
151
+ _hashAlgorithm: {
152
+ name: "ECDSA",
153
+ hash: { name: "sha-256" }
154
+ },
78
155
  generateKeyPair() {
79
156
  return __async(this, null, function* () {
80
- const keyPair = yield window.crypto.subtle.generateKey(
81
- {
82
- name: "ECDSA",
83
- namedCurve: "P-256"
84
- // ES256
85
- },
86
- true,
87
- // whether the key is extractable (i.e., can be used in exportKey)
88
- ["sign", "verify"]
89
- // can be used to sign and verify signatures
157
+ return yield generateKeyPair(ES256._keyAlgorithm);
158
+ });
159
+ },
160
+ getSigner(privateKeyJWK) {
161
+ return __async(this, null, function* () {
162
+ return yield getSigner(
163
+ privateKeyJWK,
164
+ ES256._keyAlgorithm,
165
+ ES256._hashAlgorithm
166
+ );
167
+ });
168
+ },
169
+ getVerifier(publicKeyJWK) {
170
+ return __async(this, null, function* () {
171
+ return yield getVerifier(
172
+ publicKeyJWK,
173
+ ES256._keyAlgorithm,
174
+ ES256._hashAlgorithm
90
175
  );
91
- const publicKeyJWK = yield window.crypto.subtle.exportKey(
92
- "jwk",
93
- keyPair.publicKey
176
+ });
177
+ }
178
+ };
179
+ var ES384 = {
180
+ alg: "ES384",
181
+ _keyAlgorithm: {
182
+ name: "ECDSA",
183
+ namedCurve: "P-384"
184
+ },
185
+ _hashAlgorithm: {
186
+ name: "ECDSA",
187
+ hash: { name: "sha-384" }
188
+ },
189
+ generateKeyPair() {
190
+ return __async(this, null, function* () {
191
+ return yield generateKeyPair(ES384._keyAlgorithm);
192
+ });
193
+ },
194
+ getSigner(privateKeyJWK) {
195
+ return __async(this, null, function* () {
196
+ return yield getSigner(
197
+ privateKeyJWK,
198
+ ES384._keyAlgorithm,
199
+ ES384._hashAlgorithm
94
200
  );
95
- const privateKeyJWK = yield window.crypto.subtle.exportKey(
96
- "jwk",
97
- keyPair.privateKey
201
+ });
202
+ },
203
+ getVerifier(publicKeyJWK) {
204
+ return __async(this, null, function* () {
205
+ return yield getVerifier(
206
+ publicKeyJWK,
207
+ ES384._keyAlgorithm,
208
+ ES384._hashAlgorithm
98
209
  );
99
- return { publicKey: publicKeyJWK, privateKey: privateKeyJWK };
210
+ });
211
+ }
212
+ };
213
+ var ES512 = {
214
+ alg: "ES512",
215
+ _keyAlgorithm: {
216
+ name: "ECDSA",
217
+ namedCurve: "P-521"
218
+ },
219
+ _hashAlgorithm: {
220
+ name: "ECDSA",
221
+ hash: { name: "sha-512" }
222
+ },
223
+ generateKeyPair() {
224
+ return __async(this, null, function* () {
225
+ return yield generateKeyPair(ES512._keyAlgorithm);
100
226
  });
101
227
  },
102
228
  getSigner(privateKeyJWK) {
103
229
  return __async(this, null, function* () {
104
- const privateKey = yield window.crypto.subtle.importKey(
105
- "jwk",
230
+ return yield getSigner(
106
231
  privateKeyJWK,
107
- {
108
- name: "ECDSA",
109
- namedCurve: "P-256"
110
- // Must match the curve used to generate the key
111
- },
112
- true,
113
- // whether the key is extractable (i.e., can be used in exportKey)
114
- ["sign"]
232
+ ES512._keyAlgorithm,
233
+ ES512._hashAlgorithm
115
234
  );
116
- return (data) => __async(null, null, function* () {
117
- const encoder = new TextEncoder();
118
- const signature = yield window.crypto.subtle.sign(
119
- {
120
- name: "ECDSA",
121
- hash: { name: "sha-256" }
122
- // Required for ES256
123
- },
124
- privateKey,
125
- encoder.encode(data)
126
- );
127
- return window.btoa(String.fromCharCode(...new Uint8Array(signature))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
128
- });
129
235
  });
130
236
  },
131
237
  getVerifier(publicKeyJWK) {
132
238
  return __async(this, null, function* () {
133
- const publicKey = yield window.crypto.subtle.importKey(
134
- "jwk",
239
+ return yield getVerifier(
135
240
  publicKeyJWK,
136
- {
137
- name: "ECDSA",
138
- namedCurve: "P-256"
139
- // Must match the curve used to generate the key
140
- },
141
- true,
142
- // whether the key is extractable (i.e., can be used in exportKey)
143
- ["verify"]
241
+ ES512._keyAlgorithm,
242
+ ES512._hashAlgorithm
144
243
  );
145
- return (data, signatureBase64url) => __async(null, null, function* () {
146
- const encoder = new TextEncoder();
147
- const signature = Uint8Array.from(
148
- atob(signatureBase64url.replace(/-/g, "+").replace(/_/g, "/")),
149
- (c) => c.charCodeAt(0)
150
- );
151
- const isValid = yield window.crypto.subtle.verify(
152
- {
153
- name: "ECDSA",
154
- hash: { name: "sha-256" }
155
- // Required for ES256
156
- },
157
- publicKey,
158
- signature,
159
- encoder.encode(data)
160
- );
161
- return isValid;
162
- });
163
244
  });
164
245
  }
165
246
  };
166
247
  // Annotate the CommonJS export names for ESM import in node:
167
248
  0 && (module.exports = {
168
249
  ES256,
250
+ ES384,
251
+ ES512,
169
252
  digest,
170
253
  generateSalt,
171
254
  getHasher
package/dist/index.mjs CHANGED
@@ -25,7 +25,7 @@ var generateSalt = (length) => {
25
25
  return "";
26
26
  }
27
27
  const array = new Uint8Array(length / 2);
28
- window.crypto.getRandomValues(array);
28
+ globalThis.crypto.getRandomValues(array);
29
29
  const salt = Array.from(
30
30
  array,
31
31
  (byte) => byte.toString(16).padStart(2, "0")
@@ -35,7 +35,7 @@ var generateSalt = (length) => {
35
35
  function digest(data, algorithm = "sha-256") {
36
36
  return __async(this, null, function* () {
37
37
  const ec = new TextEncoder();
38
- const digest2 = yield window.crypto.subtle.digest(
38
+ const digest2 = yield globalThis.crypto.subtle.digest(
39
39
  algorithm,
40
40
  typeof data === "string" ? ec.encode(data) : data
41
41
  );
@@ -45,98 +45,179 @@ function digest(data, algorithm = "sha-256") {
45
45
  var getHasher = (algorithm = "sha-256") => {
46
46
  return (data) => digest(data, algorithm);
47
47
  };
48
+ function generateKeyPair(keyAlgorithm) {
49
+ return __async(this, null, function* () {
50
+ const keyPair = yield globalThis.crypto.subtle.generateKey(
51
+ keyAlgorithm,
52
+ true,
53
+ // whether the key is extractable (i.e., can be used in exportKey)
54
+ ["sign", "verify"]
55
+ // can be used to sign and verify signatures
56
+ );
57
+ const publicKeyJWK = yield globalThis.crypto.subtle.exportKey(
58
+ "jwk",
59
+ keyPair.publicKey
60
+ );
61
+ const privateKeyJWK = yield globalThis.crypto.subtle.exportKey(
62
+ "jwk",
63
+ keyPair.privateKey
64
+ );
65
+ return { publicKey: publicKeyJWK, privateKey: privateKeyJWK };
66
+ });
67
+ }
68
+ function getSigner(privateKeyJWK, keyAlgorithm, signAlgorithm) {
69
+ return __async(this, null, function* () {
70
+ const privateKey = yield globalThis.crypto.subtle.importKey(
71
+ "jwk",
72
+ privateKeyJWK,
73
+ keyAlgorithm,
74
+ true,
75
+ // whether the key is extractable (i.e., can be used in exportKey)
76
+ ["sign"]
77
+ );
78
+ return (data) => __async(null, null, function* () {
79
+ const encoder = new TextEncoder();
80
+ const signature = yield globalThis.crypto.subtle.sign(
81
+ signAlgorithm,
82
+ privateKey,
83
+ encoder.encode(data)
84
+ );
85
+ return btoa(String.fromCharCode(...new Uint8Array(signature))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
86
+ });
87
+ });
88
+ }
89
+ function getVerifier(publicKeyJWK, keyAlgorithm, verifyAlgorithm) {
90
+ return __async(this, null, function* () {
91
+ const publicKey = yield globalThis.crypto.subtle.importKey(
92
+ "jwk",
93
+ publicKeyJWK,
94
+ keyAlgorithm,
95
+ true,
96
+ // whether the key is extractable (i.e., can be used in exportKey)
97
+ ["verify"]
98
+ );
99
+ return (data, signatureBase64url) => __async(null, null, function* () {
100
+ const encoder = new TextEncoder();
101
+ const signature = Uint8Array.from(
102
+ atob(signatureBase64url.replace(/-/g, "+").replace(/_/g, "/")),
103
+ (c) => c.charCodeAt(0)
104
+ );
105
+ const isValid = yield globalThis.crypto.subtle.verify(
106
+ verifyAlgorithm,
107
+ publicKey,
108
+ signature,
109
+ encoder.encode(data)
110
+ );
111
+ return isValid;
112
+ });
113
+ });
114
+ }
48
115
  var ES256 = {
49
116
  alg: "ES256",
117
+ _keyAlgorithm: {
118
+ name: "ECDSA",
119
+ namedCurve: "P-256"
120
+ },
121
+ _hashAlgorithm: {
122
+ name: "ECDSA",
123
+ hash: { name: "sha-256" }
124
+ },
50
125
  generateKeyPair() {
51
126
  return __async(this, null, function* () {
52
- const keyPair = yield window.crypto.subtle.generateKey(
53
- {
54
- name: "ECDSA",
55
- namedCurve: "P-256"
56
- // ES256
57
- },
58
- true,
59
- // whether the key is extractable (i.e., can be used in exportKey)
60
- ["sign", "verify"]
61
- // can be used to sign and verify signatures
127
+ return yield generateKeyPair(ES256._keyAlgorithm);
128
+ });
129
+ },
130
+ getSigner(privateKeyJWK) {
131
+ return __async(this, null, function* () {
132
+ return yield getSigner(
133
+ privateKeyJWK,
134
+ ES256._keyAlgorithm,
135
+ ES256._hashAlgorithm
136
+ );
137
+ });
138
+ },
139
+ getVerifier(publicKeyJWK) {
140
+ return __async(this, null, function* () {
141
+ return yield getVerifier(
142
+ publicKeyJWK,
143
+ ES256._keyAlgorithm,
144
+ ES256._hashAlgorithm
62
145
  );
63
- const publicKeyJWK = yield window.crypto.subtle.exportKey(
64
- "jwk",
65
- keyPair.publicKey
146
+ });
147
+ }
148
+ };
149
+ var ES384 = {
150
+ alg: "ES384",
151
+ _keyAlgorithm: {
152
+ name: "ECDSA",
153
+ namedCurve: "P-384"
154
+ },
155
+ _hashAlgorithm: {
156
+ name: "ECDSA",
157
+ hash: { name: "sha-384" }
158
+ },
159
+ generateKeyPair() {
160
+ return __async(this, null, function* () {
161
+ return yield generateKeyPair(ES384._keyAlgorithm);
162
+ });
163
+ },
164
+ getSigner(privateKeyJWK) {
165
+ return __async(this, null, function* () {
166
+ return yield getSigner(
167
+ privateKeyJWK,
168
+ ES384._keyAlgorithm,
169
+ ES384._hashAlgorithm
66
170
  );
67
- const privateKeyJWK = yield window.crypto.subtle.exportKey(
68
- "jwk",
69
- keyPair.privateKey
171
+ });
172
+ },
173
+ getVerifier(publicKeyJWK) {
174
+ return __async(this, null, function* () {
175
+ return yield getVerifier(
176
+ publicKeyJWK,
177
+ ES384._keyAlgorithm,
178
+ ES384._hashAlgorithm
70
179
  );
71
- return { publicKey: publicKeyJWK, privateKey: privateKeyJWK };
180
+ });
181
+ }
182
+ };
183
+ var ES512 = {
184
+ alg: "ES512",
185
+ _keyAlgorithm: {
186
+ name: "ECDSA",
187
+ namedCurve: "P-521"
188
+ },
189
+ _hashAlgorithm: {
190
+ name: "ECDSA",
191
+ hash: { name: "sha-512" }
192
+ },
193
+ generateKeyPair() {
194
+ return __async(this, null, function* () {
195
+ return yield generateKeyPair(ES512._keyAlgorithm);
72
196
  });
73
197
  },
74
198
  getSigner(privateKeyJWK) {
75
199
  return __async(this, null, function* () {
76
- const privateKey = yield window.crypto.subtle.importKey(
77
- "jwk",
200
+ return yield getSigner(
78
201
  privateKeyJWK,
79
- {
80
- name: "ECDSA",
81
- namedCurve: "P-256"
82
- // Must match the curve used to generate the key
83
- },
84
- true,
85
- // whether the key is extractable (i.e., can be used in exportKey)
86
- ["sign"]
202
+ ES512._keyAlgorithm,
203
+ ES512._hashAlgorithm
87
204
  );
88
- return (data) => __async(null, null, function* () {
89
- const encoder = new TextEncoder();
90
- const signature = yield window.crypto.subtle.sign(
91
- {
92
- name: "ECDSA",
93
- hash: { name: "sha-256" }
94
- // Required for ES256
95
- },
96
- privateKey,
97
- encoder.encode(data)
98
- );
99
- return window.btoa(String.fromCharCode(...new Uint8Array(signature))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
100
- });
101
205
  });
102
206
  },
103
207
  getVerifier(publicKeyJWK) {
104
208
  return __async(this, null, function* () {
105
- const publicKey = yield window.crypto.subtle.importKey(
106
- "jwk",
209
+ return yield getVerifier(
107
210
  publicKeyJWK,
108
- {
109
- name: "ECDSA",
110
- namedCurve: "P-256"
111
- // Must match the curve used to generate the key
112
- },
113
- true,
114
- // whether the key is extractable (i.e., can be used in exportKey)
115
- ["verify"]
211
+ ES512._keyAlgorithm,
212
+ ES512._hashAlgorithm
116
213
  );
117
- return (data, signatureBase64url) => __async(null, null, function* () {
118
- const encoder = new TextEncoder();
119
- const signature = Uint8Array.from(
120
- atob(signatureBase64url.replace(/-/g, "+").replace(/_/g, "/")),
121
- (c) => c.charCodeAt(0)
122
- );
123
- const isValid = yield window.crypto.subtle.verify(
124
- {
125
- name: "ECDSA",
126
- hash: { name: "sha-256" }
127
- // Required for ES256
128
- },
129
- publicKey,
130
- signature,
131
- encoder.encode(data)
132
- );
133
- return isValid;
134
- });
135
214
  });
136
215
  }
137
216
  };
138
217
  export {
139
218
  ES256,
219
+ ES384,
220
+ ES512,
140
221
  digest,
141
222
  generateSalt,
142
223
  getHasher
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sd-jwt/crypto-browser",
3
- "version": "0.17.2-next.0+1301a03",
3
+ "version": "0.17.2-next.2+4a4c1b0",
4
4
  "description": "sd-jwt draft 7 implementation in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -48,5 +48,5 @@
48
48
  "esm"
49
49
  ]
50
50
  },
51
- "gitHead": "1301a03484b285711d7afd7448457348bad96569"
51
+ "gitHead": "4a4c1b0c04615bcf0e455d51cd1b9234cbcd0d78"
52
52
  }
package/src/crypto.ts CHANGED
@@ -4,7 +4,7 @@ export const generateSalt = (length: number): string => {
4
4
  }
5
5
  // a hex is represented by 2 characters, so we split the length by 2
6
6
  const array = new Uint8Array(length / 2);
7
- window.crypto.getRandomValues(array);
7
+ globalThis.crypto.getRandomValues(array);
8
8
 
9
9
  const salt = Array.from(array, (byte) =>
10
10
  byte.toString(16).padStart(2, '0'),
@@ -18,7 +18,7 @@ export async function digest(
18
18
  algorithm = 'sha-256',
19
19
  ): Promise<Uint8Array> {
20
20
  const ec = new TextEncoder();
21
- const digest = await window.crypto.subtle.digest(
21
+ const digest = await globalThis.crypto.subtle.digest(
22
22
  algorithm,
23
23
  typeof data === 'string' ? ec.encode(data) : data,
24
24
  );
@@ -29,92 +29,193 @@ export const getHasher = (algorithm = 'sha-256') => {
29
29
  return (data: string) => digest(data, algorithm);
30
30
  };
31
31
 
32
+ // All derived from the subtle functions being called below
33
+ type GenerateKeyAlgorithm = RsaHashedKeyGenParams | EcKeyGenParams;
34
+ type ImportKeyAlgorithm =
35
+ | AlgorithmIdentifier
36
+ | RsaHashedImportParams
37
+ | EcKeyImportParams
38
+ | HmacImportParams
39
+ | AesKeyAlgorithm;
40
+ type SignAlgorithm = AlgorithmIdentifier | RsaPssParams | EcdsaParams;
41
+ type VerifyAlgorithm = AlgorithmIdentifier | RsaPssParams | EcdsaParams;
42
+
43
+ async function generateKeyPair(keyAlgorithm: GenerateKeyAlgorithm) {
44
+ const keyPair = await globalThis.crypto.subtle.generateKey(
45
+ keyAlgorithm,
46
+ true, // whether the key is extractable (i.e., can be used in exportKey)
47
+ ['sign', 'verify'], // can be used to sign and verify signatures
48
+ );
49
+
50
+ // Export the public and private keys in JWK format
51
+ const publicKeyJWK = await globalThis.crypto.subtle.exportKey(
52
+ 'jwk',
53
+ keyPair.publicKey,
54
+ );
55
+ const privateKeyJWK = await globalThis.crypto.subtle.exportKey(
56
+ 'jwk',
57
+ keyPair.privateKey,
58
+ );
59
+
60
+ return { publicKey: publicKeyJWK, privateKey: privateKeyJWK };
61
+ }
62
+
63
+ async function getSigner(
64
+ privateKeyJWK: object,
65
+ keyAlgorithm: ImportKeyAlgorithm,
66
+ signAlgorithm: SignAlgorithm,
67
+ ) {
68
+ const privateKey = await globalThis.crypto.subtle.importKey(
69
+ 'jwk',
70
+ privateKeyJWK,
71
+ keyAlgorithm,
72
+ true, // whether the key is extractable (i.e., can be used in exportKey)
73
+ ['sign'],
74
+ );
75
+
76
+ return async (data: string) => {
77
+ const encoder = new TextEncoder();
78
+ const signature = await globalThis.crypto.subtle.sign(
79
+ signAlgorithm,
80
+ privateKey,
81
+ encoder.encode(data),
82
+ );
83
+
84
+ return btoa(String.fromCharCode(...new Uint8Array(signature)))
85
+ .replace(/\+/g, '-')
86
+ .replace(/\//g, '_')
87
+ .replace(/=+$/, ''); // Convert to base64url format
88
+ };
89
+ }
90
+
91
+ async function getVerifier(
92
+ publicKeyJWK: object,
93
+ keyAlgorithm: ImportKeyAlgorithm,
94
+ verifyAlgorithm: VerifyAlgorithm,
95
+ ) {
96
+ const publicKey = await globalThis.crypto.subtle.importKey(
97
+ 'jwk',
98
+ publicKeyJWK,
99
+ keyAlgorithm,
100
+ true, // whether the key is extractable (i.e., can be used in exportKey)
101
+ ['verify'],
102
+ );
103
+
104
+ return async (data: string, signatureBase64url: string) => {
105
+ const encoder = new TextEncoder();
106
+ const signature = Uint8Array.from(
107
+ atob(signatureBase64url.replace(/-/g, '+').replace(/_/g, '/')),
108
+ (c) => c.charCodeAt(0),
109
+ );
110
+ const isValid = await globalThis.crypto.subtle.verify(
111
+ verifyAlgorithm,
112
+ publicKey,
113
+ signature,
114
+ encoder.encode(data),
115
+ );
116
+
117
+ return isValid;
118
+ };
119
+ }
120
+
32
121
  export const ES256 = {
33
122
  alg: 'ES256',
34
123
 
124
+ _keyAlgorithm: {
125
+ name: 'ECDSA',
126
+ namedCurve: 'P-256',
127
+ },
128
+
129
+ _hashAlgorithm: {
130
+ name: 'ECDSA',
131
+ hash: { name: 'sha-256' },
132
+ },
133
+
35
134
  async generateKeyPair() {
36
- const keyPair = await window.crypto.subtle.generateKey(
37
- {
38
- name: 'ECDSA',
39
- namedCurve: 'P-256', // ES256
40
- },
41
- true, // whether the key is extractable (i.e., can be used in exportKey)
42
- ['sign', 'verify'], // can be used to sign and verify signatures
43
- );
135
+ return await generateKeyPair(ES256._keyAlgorithm);
136
+ },
44
137
 
45
- // Export the public and private keys in JWK format
46
- const publicKeyJWK = await window.crypto.subtle.exportKey(
47
- 'jwk',
48
- keyPair.publicKey,
138
+ async getSigner(privateKeyJWK: object) {
139
+ return await getSigner(
140
+ privateKeyJWK,
141
+ ES256._keyAlgorithm,
142
+ ES256._hashAlgorithm,
49
143
  );
50
- const privateKeyJWK = await window.crypto.subtle.exportKey(
51
- 'jwk',
52
- keyPair.privateKey,
144
+ },
145
+
146
+ async getVerifier(publicKeyJWK: object) {
147
+ return await getVerifier(
148
+ publicKeyJWK,
149
+ ES256._keyAlgorithm,
150
+ ES256._hashAlgorithm,
53
151
  );
152
+ },
153
+ };
154
+
155
+ export const ES384 = {
156
+ alg: 'ES384',
157
+
158
+ _keyAlgorithm: {
159
+ name: 'ECDSA',
160
+ namedCurve: 'P-384',
161
+ },
54
162
 
55
- return { publicKey: publicKeyJWK, privateKey: privateKeyJWK };
163
+ _hashAlgorithm: {
164
+ name: 'ECDSA',
165
+ hash: { name: 'sha-384' },
166
+ },
167
+
168
+ async generateKeyPair() {
169
+ return await generateKeyPair(ES384._keyAlgorithm);
56
170
  },
57
171
 
58
172
  async getSigner(privateKeyJWK: object) {
59
- const privateKey = await window.crypto.subtle.importKey(
60
- 'jwk',
173
+ return await getSigner(
61
174
  privateKeyJWK,
62
- {
63
- name: 'ECDSA',
64
- namedCurve: 'P-256', // Must match the curve used to generate the key
65
- },
66
- true, // whether the key is extractable (i.e., can be used in exportKey)
67
- ['sign'],
175
+ ES384._keyAlgorithm,
176
+ ES384._hashAlgorithm,
68
177
  );
69
-
70
- return async (data: string) => {
71
- const encoder = new TextEncoder();
72
- const signature = await window.crypto.subtle.sign(
73
- {
74
- name: 'ECDSA',
75
- hash: { name: 'sha-256' }, // Required for ES256
76
- },
77
- privateKey,
78
- encoder.encode(data),
79
- );
80
-
81
- return window
82
- .btoa(String.fromCharCode(...new Uint8Array(signature)))
83
- .replace(/\+/g, '-')
84
- .replace(/\//g, '_')
85
- .replace(/=+$/, ''); // Convert to base64url format
86
- };
87
178
  },
88
179
 
89
180
  async getVerifier(publicKeyJWK: object) {
90
- const publicKey = await window.crypto.subtle.importKey(
91
- 'jwk',
181
+ return await getVerifier(
92
182
  publicKeyJWK,
93
- {
94
- name: 'ECDSA',
95
- namedCurve: 'P-256', // Must match the curve used to generate the key
96
- },
97
- true, // whether the key is extractable (i.e., can be used in exportKey)
98
- ['verify'],
183
+ ES384._keyAlgorithm,
184
+ ES384._hashAlgorithm,
185
+ );
186
+ },
187
+ };
188
+
189
+ export const ES512 = {
190
+ alg: 'ES512',
191
+
192
+ _keyAlgorithm: {
193
+ name: 'ECDSA',
194
+ namedCurve: 'P-521',
195
+ },
196
+
197
+ _hashAlgorithm: {
198
+ name: 'ECDSA',
199
+ hash: { name: 'sha-512' },
200
+ },
201
+
202
+ async generateKeyPair() {
203
+ return await generateKeyPair(ES512._keyAlgorithm);
204
+ },
205
+
206
+ async getSigner(privateKeyJWK: object) {
207
+ return await getSigner(
208
+ privateKeyJWK,
209
+ ES512._keyAlgorithm,
210
+ ES512._hashAlgorithm,
99
211
  );
212
+ },
100
213
 
101
- return async (data: string, signatureBase64url: string) => {
102
- const encoder = new TextEncoder();
103
- const signature = Uint8Array.from(
104
- atob(signatureBase64url.replace(/-/g, '+').replace(/_/g, '/')),
105
- (c) => c.charCodeAt(0),
106
- );
107
- const isValid = await window.crypto.subtle.verify(
108
- {
109
- name: 'ECDSA',
110
- hash: { name: 'sha-256' }, // Required for ES256
111
- },
112
- publicKey,
113
- signature,
114
- encoder.encode(data),
115
- );
116
-
117
- return isValid;
118
- };
214
+ async getVerifier(publicKeyJWK: object) {
215
+ return await getVerifier(
216
+ publicKeyJWK,
217
+ ES512._keyAlgorithm,
218
+ ES512._hashAlgorithm,
219
+ );
119
220
  },
120
221
  };
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'vitest';
2
- import { digest, ES256, generateSalt, getHasher } from '../index';
2
+ import { digest, ES256, ES384, ES512, generateSalt, getHasher } from '../index';
3
3
 
4
4
  // Extract the major version as a number
5
5
  const nodeVersionMajor = Number.parseInt(
@@ -42,24 +42,42 @@ describe('This file is for utility functions', () => {
42
42
  expect(hash).toBeDefined();
43
43
  });
44
44
 
45
- (nodeVersionMajor < 20 ? test.skip : test)('ES256 alg', () => {
46
- expect(ES256.alg).toBe('ES256');
47
- });
45
+ for (const algLib of [
46
+ {
47
+ name: 'ES256',
48
+ lib: ES256,
49
+ },
50
+ {
51
+ name: 'ES384',
52
+ lib: ES384,
53
+ },
54
+ {
55
+ name: 'ES512',
56
+ lib: ES512,
57
+ },
58
+ ]) {
59
+ (nodeVersionMajor < 20 ? test.skip : test)(`${algLib.name} alg`, () => {
60
+ expect(algLib.lib.alg).toBe(algLib.name);
61
+ });
48
62
 
49
- (nodeVersionMajor < 20 ? test.skip : test)('ES256', async () => {
50
- const { privateKey, publicKey } = await ES256.generateKeyPair();
51
- expect(privateKey).toBeDefined();
52
- expect(publicKey).toBeDefined();
53
- expect(typeof privateKey).toBe('object');
54
- expect(typeof publicKey).toBe('object');
63
+ (nodeVersionMajor < 20 ? test.skip : test)(algLib.name, async () => {
64
+ const { privateKey, publicKey } = await algLib.lib.generateKeyPair();
65
+ expect(privateKey).toBeDefined();
66
+ expect(publicKey).toBeDefined();
67
+ expect(typeof privateKey).toBe('object');
68
+ expect(typeof publicKey).toBe('object');
55
69
 
56
- const data =
57
- 'In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes data, a password or passphrase.';
58
- const signature = await (await ES256.getSigner(privateKey))(data);
59
- expect(signature).toBeDefined();
60
- expect(typeof signature).toBe('string');
70
+ const data =
71
+ 'In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes data, a password or passphrase.';
72
+ const signature = await (await algLib.lib.getSigner(privateKey))(data);
73
+ expect(signature).toBeDefined();
74
+ expect(typeof signature).toBe('string');
61
75
 
62
- const result = await (await ES256.getVerifier(publicKey))(data, signature);
63
- expect(result).toBe(true);
64
- });
76
+ const result = await (await algLib.lib.getVerifier(publicKey))(
77
+ data,
78
+ signature,
79
+ );
80
+ expect(result).toBe(true);
81
+ });
82
+ }
65
83
  });