react-native-quick-crypto 0.7.3 → 0.7.5

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.
Files changed (179) hide show
  1. package/README.md +13 -46
  2. package/android/build.gradle +1 -1
  3. package/android/gradle.properties +1 -1
  4. package/lib/commonjs/Cipher.js +114 -112
  5. package/lib/commonjs/Cipher.js.map +1 -1
  6. package/lib/commonjs/Hash.js +8 -5
  7. package/lib/commonjs/Hash.js.map +1 -1
  8. package/lib/commonjs/Hashnames.js +7 -1
  9. package/lib/commonjs/Hashnames.js.map +1 -1
  10. package/lib/commonjs/Hmac.js +5 -3
  11. package/lib/commonjs/Hmac.js.map +1 -1
  12. package/lib/commonjs/NativeQuickCrypto/Cipher.js +7 -7
  13. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  14. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -4
  15. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  16. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  17. package/lib/commonjs/Utils.js +98 -63
  18. package/lib/commonjs/Utils.js.map +1 -1
  19. package/lib/commonjs/aes.js +9 -8
  20. package/lib/commonjs/aes.js.map +1 -1
  21. package/lib/commonjs/ec.js +2 -3
  22. package/lib/commonjs/ec.js.map +1 -1
  23. package/lib/commonjs/index.js +1 -1
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/keygen.js +4 -2
  26. package/lib/commonjs/keygen.js.map +1 -1
  27. package/lib/commonjs/keys.js +7 -2
  28. package/lib/commonjs/keys.js.map +1 -1
  29. package/lib/commonjs/pbkdf2.js +13 -21
  30. package/lib/commonjs/pbkdf2.js.map +1 -1
  31. package/lib/commonjs/random.js +6 -8
  32. package/lib/commonjs/random.js.map +1 -1
  33. package/lib/commonjs/rsa.js.map +1 -1
  34. package/lib/commonjs/sig.js +1 -2
  35. package/lib/commonjs/sig.js.map +1 -1
  36. package/lib/commonjs/subtle.js +4 -3
  37. package/lib/commonjs/subtle.js.map +1 -1
  38. package/lib/module/Cipher.js +113 -111
  39. package/lib/module/Cipher.js.map +1 -1
  40. package/lib/module/Hash.js +7 -4
  41. package/lib/module/Hash.js.map +1 -1
  42. package/lib/module/Hashnames.js +7 -1
  43. package/lib/module/Hashnames.js.map +1 -1
  44. package/lib/module/Hmac.js +4 -2
  45. package/lib/module/Hmac.js.map +1 -1
  46. package/lib/module/NativeQuickCrypto/Cipher.js +7 -7
  47. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  48. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +11 -4
  49. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  50. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  51. package/lib/module/Utils.js +95 -62
  52. package/lib/module/Utils.js.map +1 -1
  53. package/lib/module/aes.js +9 -8
  54. package/lib/module/aes.js.map +1 -1
  55. package/lib/module/ec.js +2 -3
  56. package/lib/module/ec.js.map +1 -1
  57. package/lib/module/keygen.js +4 -2
  58. package/lib/module/keygen.js.map +1 -1
  59. package/lib/module/keys.js +7 -2
  60. package/lib/module/keys.js.map +1 -1
  61. package/lib/module/pbkdf2.js +13 -21
  62. package/lib/module/pbkdf2.js.map +1 -1
  63. package/lib/module/random.js +6 -8
  64. package/lib/module/random.js.map +1 -1
  65. package/lib/module/rsa.js.map +1 -1
  66. package/lib/module/sig.js +0 -1
  67. package/lib/module/sig.js.map +1 -1
  68. package/lib/module/subtle.js +4 -3
  69. package/lib/module/subtle.js.map +1 -1
  70. package/lib/typescript/{Cipher.d.ts → src/Cipher.d.ts} +37 -33
  71. package/lib/typescript/src/Cipher.d.ts.map +1 -0
  72. package/lib/typescript/src/Hash.d.ts.map +1 -0
  73. package/lib/typescript/{Hashnames.d.ts → src/Hashnames.d.ts} +2 -2
  74. package/lib/typescript/src/Hashnames.d.ts.map +1 -0
  75. package/lib/typescript/src/Hmac.d.ts.map +1 -0
  76. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/Cipher.d.ts +5 -5
  77. package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
  78. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +1 -1
  79. package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
  80. package/lib/typescript/src/NativeQuickCrypto/aes.d.ts.map +1 -0
  81. package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
  82. package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
  83. package/lib/typescript/src/NativeQuickCrypto/keygen.d.ts.map +1 -0
  84. package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
  85. package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
  86. package/lib/typescript/src/NativeQuickCrypto/rsa.d.ts.map +1 -0
  87. package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
  88. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/webcrypto.d.ts +3 -2
  89. package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
  90. package/lib/typescript/{Utils.d.ts → src/Utils.d.ts} +18 -8
  91. package/lib/typescript/src/Utils.d.ts.map +1 -0
  92. package/lib/typescript/src/aes.d.ts.map +1 -0
  93. package/lib/typescript/src/constants.d.ts.map +1 -0
  94. package/lib/typescript/src/ec.d.ts.map +1 -0
  95. package/lib/typescript/{index.d.ts → src/index.d.ts} +9 -34
  96. package/lib/typescript/src/index.d.ts.map +1 -0
  97. package/lib/typescript/src/keygen.d.ts.map +1 -0
  98. package/lib/typescript/{keys.d.ts → src/keys.d.ts} +30 -28
  99. package/lib/typescript/src/keys.d.ts.map +1 -0
  100. package/lib/typescript/{pbkdf2.d.ts → src/pbkdf2.d.ts} +3 -4
  101. package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
  102. package/lib/typescript/{random.d.ts → src/random.d.ts} +5 -5
  103. package/lib/typescript/src/random.d.ts.map +1 -0
  104. package/lib/typescript/src/rsa.d.ts.map +1 -0
  105. package/lib/typescript/{sig.d.ts → src/sig.d.ts} +5 -5
  106. package/lib/typescript/src/sig.d.ts.map +1 -0
  107. package/lib/typescript/src/subtle.d.ts.map +1 -0
  108. package/lib/typescript/src/webcrypto.d.ts.map +1 -0
  109. package/package.json +14 -41
  110. package/src/Cipher.ts +272 -214
  111. package/src/Hash.ts +14 -7
  112. package/src/Hashnames.ts +10 -4
  113. package/src/Hmac.ts +6 -5
  114. package/src/NativeQuickCrypto/Cipher.ts +21 -20
  115. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +15 -11
  116. package/src/NativeQuickCrypto/aes.ts +1 -1
  117. package/src/NativeQuickCrypto/hash.ts +1 -1
  118. package/src/NativeQuickCrypto/hmac.ts +1 -1
  119. package/src/NativeQuickCrypto/keygen.ts +1 -1
  120. package/src/NativeQuickCrypto/pbkdf2.ts +2 -2
  121. package/src/NativeQuickCrypto/random.ts +2 -2
  122. package/src/NativeQuickCrypto/rsa.ts +1 -1
  123. package/src/NativeQuickCrypto/sig.ts +3 -1
  124. package/src/NativeQuickCrypto/webcrypto.ts +6 -5
  125. package/src/Utils.ts +181 -108
  126. package/src/aes.ts +29 -29
  127. package/src/ec.ts +18 -19
  128. package/src/keygen.ts +7 -7
  129. package/src/keys.ts +73 -67
  130. package/src/pbkdf2.ts +24 -48
  131. package/src/random.ts +37 -43
  132. package/src/rsa.ts +25 -23
  133. package/src/sig.ts +12 -13
  134. package/src/subtle.ts +50 -45
  135. package/lib/typescript/Cipher.d.ts.map +0 -1
  136. package/lib/typescript/Hash.d.ts.map +0 -1
  137. package/lib/typescript/Hashnames.d.ts.map +0 -1
  138. package/lib/typescript/Hmac.d.ts.map +0 -1
  139. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  140. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  141. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  142. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  143. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  144. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  145. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  146. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  147. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  148. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  149. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  150. package/lib/typescript/Utils.d.ts.map +0 -1
  151. package/lib/typescript/aes.d.ts.map +0 -1
  152. package/lib/typescript/constants.d.ts.map +0 -1
  153. package/lib/typescript/ec.d.ts.map +0 -1
  154. package/lib/typescript/index.d.ts.map +0 -1
  155. package/lib/typescript/keygen.d.ts.map +0 -1
  156. package/lib/typescript/keys.d.ts.map +0 -1
  157. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  158. package/lib/typescript/random.d.ts.map +0 -1
  159. package/lib/typescript/rsa.d.ts.map +0 -1
  160. package/lib/typescript/sig.d.ts.map +0 -1
  161. package/lib/typescript/subtle.d.ts.map +0 -1
  162. package/lib/typescript/webcrypto.d.ts.map +0 -1
  163. /package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +0 -0
  164. /package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +0 -0
  165. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/aes.d.ts +0 -0
  166. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/hash.d.ts +0 -0
  167. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/hmac.d.ts +0 -0
  168. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/keygen.d.ts +0 -0
  169. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +0 -0
  170. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +0 -0
  171. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/rsa.d.ts +0 -0
  172. /package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +0 -0
  173. /package/lib/typescript/{aes.d.ts → src/aes.d.ts} +0 -0
  174. /package/lib/typescript/{constants.d.ts → src/constants.d.ts} +0 -0
  175. /package/lib/typescript/{ec.d.ts → src/ec.d.ts} +0 -0
  176. /package/lib/typescript/{keygen.d.ts → src/keygen.d.ts} +0 -0
  177. /package/lib/typescript/{rsa.d.ts → src/rsa.d.ts} +0 -0
  178. /package/lib/typescript/{subtle.d.ts → src/subtle.d.ts} +0 -0
  179. /package/lib/typescript/{webcrypto.d.ts → src/webcrypto.d.ts} +0 -0
package/src/Utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Buffer } from '@craftzdog/react-native-buffer';
2
+ import { Buffer as SBuffer } from 'safe-buffer';
2
3
  import type {
3
4
  AnyAlgorithm,
4
5
  DeriveBitsAlgorithm,
@@ -13,8 +14,14 @@ import type {
13
14
  } from './keys';
14
15
  import { type CipherKey } from 'crypto'; // @types/node
15
16
 
16
- export type BufferLike = ArrayBuffer | Buffer | ArrayBufferView;
17
- export type BinaryLike = string | ArrayBuffer | Buffer | TypedArray;
17
+ export type BufferLike = ArrayBuffer | Buffer | SBuffer | ArrayBufferView;
18
+ export type BinaryLike =
19
+ | string
20
+ | ArrayBuffer
21
+ | Buffer
22
+ | SBuffer
23
+ | TypedArray
24
+ | DataView;
18
25
  export type BinaryLikeNode = CipherKey | BinaryLike;
19
26
 
20
27
  export type BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary';
@@ -28,6 +35,44 @@ export type Encoding =
28
35
  // TODO(osp) should buffer be part of the Encoding type?
29
36
  export type CipherEncoding = Encoding | 'buffer';
30
37
 
38
+ // These are for shortcomings in @types/node
39
+ // Here we use "*Type" instead of "*Types" like node does.
40
+ export type CipherType =
41
+ | 'aes128'
42
+ | 'aes192'
43
+ | 'aes256'
44
+ | CipherCBCType
45
+ | CipherCFBType
46
+ | CipherCTRType
47
+ | CipherDESType
48
+ | CipherECBType
49
+ | CipherGCMType
50
+ | CipherOFBType;
51
+ export type CipherCBCType = 'aes-128-cbc' | 'aes-192-cbc' | 'aes-256-cbc';
52
+ export type CipherCFBType =
53
+ | 'aes-128-cfb'
54
+ | 'aes-192-cfb'
55
+ | 'aes-256-cfb'
56
+ | 'aes-128-cfb1'
57
+ | 'aes-192-cfb1'
58
+ | 'aes-256-cfb1'
59
+ | 'aes-128-cfb8'
60
+ | 'aes-192-cfb8'
61
+ | 'aes-256-cfb8';
62
+ export type CipherCTRType = 'aes-128-ctr' | 'aes-192-ctr' | 'aes-256-ctr';
63
+ export type CipherDESType =
64
+ | 'des'
65
+ | 'des3'
66
+ | 'des-cbc'
67
+ | 'des-ecb'
68
+ | 'des-ede'
69
+ | 'des-ede-cbc'
70
+ | 'des-ede3'
71
+ | 'des-ede3-cbc';
72
+ export type CipherECBType = 'aes-128-ecb' | 'aes-192-ecb' | 'aes-256-ecb';
73
+ export type CipherGCMType = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm';
74
+ export type CipherOFBType = 'aes-128-ofb' | 'aes-192-ofb' | 'aes-256-ofb';
75
+
31
76
  export type TypedArray =
32
77
  | Uint8Array
33
78
  | Uint8ClampedArray
@@ -43,7 +88,7 @@ type DOMName =
43
88
  | string
44
89
  | {
45
90
  name: string;
46
- cause: any;
91
+ cause: unknown;
47
92
  };
48
93
 
49
94
  // Mimics node behavior for default global encoding
@@ -130,34 +175,40 @@ export const kEmptyObject = Object.freeze(Object.create(null));
130
175
  // return slowCases(enc);
131
176
  // }
132
177
 
133
- export function toArrayBuffer(buf: Buffer): ArrayBuffer {
134
- if (buf?.buffer?.slice) {
178
+ export function toArrayBuffer(buf: Buffer | SBuffer): ArrayBuffer {
179
+ if (Buffer.isBuffer(buf) && buf?.buffer?.slice) {
135
180
  return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
136
181
  }
137
182
  const ab = new ArrayBuffer(buf.length);
138
183
  const view = new Uint8Array(ab);
139
184
  for (let i = 0; i < buf.length; ++i) {
140
- view[i] = buf[i]!;
185
+ view[i] = SBuffer.isBuffer(buf) ? buf.readUInt8(i) : buf[i]!;
141
186
  }
142
187
  return ab;
143
188
  }
144
189
 
145
190
  export function bufferLikeToArrayBuffer(buf: BufferLike): ArrayBuffer {
146
- return Buffer.isBuffer(buf)
147
- ? buf.buffer
148
- : ArrayBuffer.isView(buf)
149
- ? buf.buffer
150
- : buf;
191
+ if (Buffer.isBuffer(buf)) {
192
+ return buf.buffer;
193
+ }
194
+ if (SBuffer.isBuffer(buf)) {
195
+ return toArrayBuffer(buf);
196
+ }
197
+ if (ArrayBuffer.isView(buf)) {
198
+ return buf.buffer;
199
+ }
200
+ return buf;
151
201
  }
152
202
 
153
203
  export function binaryLikeToArrayBuffer(
154
204
  input: BinaryLikeNode, // CipherKey adds compat with node types
155
- encoding: string = 'utf-8'
205
+ encoding: string = 'utf-8',
156
206
  ): ArrayBuffer {
207
+ // string
157
208
  if (typeof input === 'string') {
158
209
  if (encoding === 'buffer') {
159
210
  throw new Error(
160
- 'Cannot create a buffer from a string with a buffer encoding'
211
+ 'Cannot create a buffer from a string with a buffer encoding',
161
212
  );
162
213
  }
163
214
 
@@ -165,43 +216,51 @@ export function binaryLikeToArrayBuffer(
165
216
 
166
217
  return buffer.buffer.slice(
167
218
  buffer.byteOffset,
168
- buffer.byteOffset + buffer.byteLength
219
+ buffer.byteOffset + buffer.byteLength,
169
220
  );
170
221
  }
171
222
 
223
+ // Buffer
172
224
  if (Buffer.isBuffer(input)) {
173
225
  return toArrayBuffer(input);
174
226
  }
175
227
 
228
+ // ArrayBufferView
176
229
  // TODO add further binary types to BinaryLike, UInt8Array and so for have this array as property
177
230
  if (ArrayBuffer.isView(input)) {
178
231
  return input.buffer;
179
232
  }
180
233
 
181
- if (!(input instanceof ArrayBuffer)) {
182
- try {
183
- // this is a strange fallback case and input is unknown at this point
184
- // @ts-expect-error
185
- const buffer = Buffer.from(input);
186
- return buffer.buffer.slice(
187
- buffer.byteOffset,
188
- buffer.byteOffset + buffer.byteLength
189
- );
190
- } catch {
191
- throw 'error';
192
- }
234
+ // ArrayBuffer
235
+ if (input instanceof ArrayBuffer) {
236
+ return input;
193
237
  }
194
238
 
239
+ // if (!(input instanceof ArrayBuffer)) {
240
+ // try {
241
+ // // this is a strange fallback case and input is unknown at this point
242
+ // const buffer = Buffer.from(input as unknown as string);
243
+ // return buffer.buffer.slice(
244
+ // buffer.byteOffset,
245
+ // buffer.byteOffset + buffer.byteLength
246
+ // );
247
+ // } catch(e: unknown) {
248
+ // console.log('throwing 1');
249
+ // const err = e as Error;
250
+ // throw new Error(err.message);
251
+ // }
252
+ // }
253
+
195
254
  // TODO: handle if input is KeyObject?
196
255
 
197
- return input;
256
+ throw new Error('input could not be converted to ArrayBuffer');
198
257
  }
199
258
 
200
259
  export function ab2str(buf: ArrayBuffer, encoding: string = 'hex') {
201
260
  return Buffer.from(buf).toString(encoding);
202
261
  }
203
262
 
204
- export function validateString(str: any, name?: string): str is string {
263
+ export function validateString(str: unknown, name?: string): str is string {
205
264
  const isString = typeof str === 'string';
206
265
  if (!isString) {
207
266
  throw new Error(`${name} is not a string`);
@@ -209,11 +268,11 @@ export function validateString(str: any, name?: string): str is string {
209
268
  return isString;
210
269
  }
211
270
 
212
- export function validateFunction(f: any): boolean {
271
+ export function validateFunction(f: unknown): boolean {
213
272
  return f !== null && typeof f === 'function';
214
273
  }
215
274
 
216
- export function isStringOrBuffer(val: any): val is string | ArrayBuffer {
275
+ export function isStringOrBuffer(val: unknown): val is string | ArrayBuffer {
217
276
  return (
218
277
  typeof val === 'string' ||
219
278
  ArrayBuffer.isView(val) ||
@@ -222,13 +281,13 @@ export function isStringOrBuffer(val: any): val is string | ArrayBuffer {
222
281
  }
223
282
 
224
283
  export function validateObject<T>(
225
- value: any,
284
+ value: unknown,
226
285
  name: string,
227
286
  options?: {
228
287
  allowArray: boolean;
229
288
  allowFunction: boolean;
230
289
  nullable: boolean;
231
- } | null
290
+ } | null,
232
291
  ): value is T {
233
292
  const useDefaultOptions = options == null;
234
293
  const allowArray = useDefaultOptions ? false : options.allowArray;
@@ -246,10 +305,11 @@ export function validateObject<T>(
246
305
  }
247
306
 
248
307
  export function validateInt32(
308
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
249
309
  value: any,
250
310
  name: string,
251
311
  min = -2147483648,
252
- max = 2147483647
312
+ max = 2147483647,
253
313
  ) {
254
314
  // The defaults for min and max correspond to the limits of 32-bit integers.
255
315
  if (typeof value !== 'number') {
@@ -257,12 +317,12 @@ export function validateInt32(
257
317
  }
258
318
  if (!Number.isInteger(value)) {
259
319
  throw new Error(
260
- `Argument out of range - ${name} out of integer range: ${value}`
320
+ `Argument out of range - ${name} out of integer range: ${value}`,
261
321
  );
262
322
  }
263
323
  if (value < min || value > max) {
264
324
  throw new Error(
265
- `Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`
325
+ `Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`,
266
326
  );
267
327
  }
268
328
  }
@@ -270,7 +330,7 @@ export function validateInt32(
270
330
  export function validateUint32(
271
331
  value: number,
272
332
  name: string,
273
- positive?: boolean
333
+ positive?: boolean,
274
334
  ) {
275
335
  if (typeof value !== 'number') {
276
336
  // throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
@@ -279,7 +339,7 @@ export function validateUint32(
279
339
  if (!Number.isInteger(value)) {
280
340
  // throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
281
341
  throw new Error(
282
- `Argument out of range - ${name} out of integer range: ${value}`
342
+ `Argument out of range - ${name} out of integer range: ${value}`,
283
343
  );
284
344
  }
285
345
  const min = positive ? 1 : 0;
@@ -288,7 +348,7 @@ export function validateUint32(
288
348
  if (value < min || value > max) {
289
349
  // throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
290
350
  throw new Error(
291
- `Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`
351
+ `Invalid argument - ${name} out of range >= ${min} && <= ${max}: ${value}`,
292
352
  );
293
353
  }
294
354
  }
@@ -334,19 +394,19 @@ type SupportedAlgorithm<Type extends string> = {
334
394
  };
335
395
 
336
396
  type SupportedAlgorithms = {
337
- 'digest': SupportedAlgorithm<DigestAlgorithm>;
338
- 'generateKey': SupportedAlgorithm<KeyPairAlgorithm | SecretKeyAlgorithm>;
339
- 'sign': SupportedAlgorithm<SignVerifyAlgorithm>;
340
- 'verify': SupportedAlgorithm<SignVerifyAlgorithm>;
341
- 'importKey': SupportedAlgorithm<
397
+ digest: SupportedAlgorithm<DigestAlgorithm>;
398
+ generateKey: SupportedAlgorithm<KeyPairAlgorithm | SecretKeyAlgorithm>;
399
+ sign: SupportedAlgorithm<SignVerifyAlgorithm>;
400
+ verify: SupportedAlgorithm<SignVerifyAlgorithm>;
401
+ importKey: SupportedAlgorithm<
342
402
  KeyPairAlgorithm | 'PBKDF2' | SecretKeyAlgorithm | 'HKDF'
343
403
  >;
344
- 'deriveBits': SupportedAlgorithm<DeriveBitsAlgorithm>;
345
- 'encrypt': SupportedAlgorithm<EncryptDecryptAlgorithm>;
346
- 'decrypt': SupportedAlgorithm<EncryptDecryptAlgorithm>;
404
+ deriveBits: SupportedAlgorithm<DeriveBitsAlgorithm>;
405
+ encrypt: SupportedAlgorithm<EncryptDecryptAlgorithm>;
406
+ decrypt: SupportedAlgorithm<EncryptDecryptAlgorithm>;
347
407
  'get key length': SupportedAlgorithm<SecretKeyAlgorithm | 'PBKDF2' | 'HKDF'>;
348
- 'wrapKey': SupportedAlgorithm<'AES-KW'>;
349
- 'unwrapKey': SupportedAlgorithm<'AES-KW'>;
408
+ wrapKey: SupportedAlgorithm<'AES-KW'>;
409
+ unwrapKey: SupportedAlgorithm<'AES-KW'>;
350
410
  };
351
411
 
352
412
  export type Operation =
@@ -363,76 +423,76 @@ export type Operation =
363
423
  | 'unwrapKey';
364
424
 
365
425
  const kSupportedAlgorithms: SupportedAlgorithms = {
366
- 'digest': {
426
+ digest: {
367
427
  'SHA-1': null,
368
428
  'SHA-256': null,
369
429
  'SHA-384': null,
370
430
  'SHA-512': null,
371
431
  },
372
- 'generateKey': {
432
+ generateKey: {
373
433
  'RSASSA-PKCS1-v1_5': 'RsaHashedKeyGenParams',
374
434
  'RSA-PSS': 'RsaHashedKeyGenParams',
375
435
  'RSA-OAEP': 'RsaHashedKeyGenParams',
376
- 'ECDSA': 'EcKeyGenParams',
377
- 'ECDH': 'EcKeyGenParams',
436
+ ECDSA: 'EcKeyGenParams',
437
+ ECDH: 'EcKeyGenParams',
378
438
  'AES-CTR': 'AesKeyGenParams',
379
439
  'AES-CBC': 'AesKeyGenParams',
380
440
  'AES-GCM': 'AesKeyGenParams',
381
441
  'AES-KW': 'AesKeyGenParams',
382
- 'HMAC': 'HmacKeyGenParams',
383
- 'X25519': null,
384
- 'Ed25519': null,
385
- 'X448': null,
386
- 'Ed448': null,
442
+ HMAC: 'HmacKeyGenParams',
443
+ X25519: null,
444
+ Ed25519: null,
445
+ X448: null,
446
+ Ed448: null,
387
447
  },
388
- 'sign': {
448
+ sign: {
389
449
  'RSASSA-PKCS1-v1_5': null,
390
450
  'RSA-PSS': 'RsaPssParams',
391
- 'ECDSA': 'EcdsaParams',
392
- 'HMAC': null,
393
- 'Ed25519': null,
394
- 'Ed448': 'Ed448Params',
451
+ ECDSA: 'EcdsaParams',
452
+ HMAC: null,
453
+ Ed25519: null,
454
+ Ed448: 'Ed448Params',
395
455
  },
396
- 'verify': {
456
+ verify: {
397
457
  'RSASSA-PKCS1-v1_5': null,
398
458
  'RSA-PSS': 'RsaPssParams',
399
- 'ECDSA': 'EcdsaParams',
400
- 'HMAC': null,
401
- 'Ed25519': null,
402
- 'Ed448': 'Ed448Params',
459
+ ECDSA: 'EcdsaParams',
460
+ HMAC: null,
461
+ Ed25519: null,
462
+ Ed448: 'Ed448Params',
403
463
  },
404
- 'importKey': {
464
+ importKey: {
405
465
  'RSASSA-PKCS1-v1_5': 'RsaHashedImportParams',
406
466
  'RSA-PSS': 'RsaHashedImportParams',
407
467
  'RSA-OAEP': 'RsaHashedImportParams',
408
- 'ECDSA': 'EcKeyImportParams',
409
- 'ECDH': 'EcKeyImportParams',
410
- 'HMAC': 'HmacImportParams',
411
- 'HKDF': null,
412
- 'PBKDF2': null,
468
+ ECDSA: 'EcKeyImportParams',
469
+ ECDH: 'EcKeyImportParams',
470
+ HMAC: 'HmacImportParams',
471
+ HKDF: null,
472
+ PBKDF2: null,
413
473
  'AES-CTR': null,
414
474
  'AES-CBC': null,
415
475
  'AES-GCM': null,
416
476
  'AES-KW': null,
417
- 'Ed25519': null,
418
- 'X25519': null,
419
- 'Ed448': null,
420
- 'X448': null,
477
+ Ed25519: null,
478
+ X25519: null,
479
+ Ed448: null,
480
+ X448: null,
421
481
  },
422
- 'deriveBits': {
482
+ deriveBits: {
423
483
  HKDF: 'HkdfParams',
424
484
  PBKDF2: 'Pbkdf2Params',
425
485
  ECDH: 'EcdhKeyDeriveParams',
426
486
  X25519: 'EcdhKeyDeriveParams',
427
487
  X448: 'EcdhKeyDeriveParams',
428
488
  },
429
- 'encrypt': {
489
+ encrypt: {
430
490
  'RSA-OAEP': 'RsaOaepParams',
431
491
  'AES-CBC': 'AesCbcParams',
432
492
  'AES-GCM': 'AesGcmParams',
433
493
  'AES-CTR': 'AesCtrParams',
434
494
  },
435
- 'decrypt': {
495
+ decrypt: {
436
496
  'RSA-OAEP': 'RsaOaepParams',
437
497
  'AES-CBC': 'AesCbcParams',
438
498
  'AES-GCM': 'AesGcmParams',
@@ -443,20 +503,20 @@ const kSupportedAlgorithms: SupportedAlgorithms = {
443
503
  'AES-CTR': 'AesDerivedKeyParams',
444
504
  'AES-GCM': 'AesDerivedKeyParams',
445
505
  'AES-KW': 'AesDerivedKeyParams',
446
- 'HMAC': 'HmacImportParams',
447
- 'HKDF': null,
448
- 'PBKDF2': null,
506
+ HMAC: 'HmacImportParams',
507
+ HKDF: null,
508
+ PBKDF2: null,
449
509
  },
450
- 'wrapKey': {
510
+ wrapKey: {
451
511
  'AES-KW': null,
452
512
  },
453
- 'unwrapKey': {
513
+ unwrapKey: {
454
514
  'AES-KW': null,
455
515
  },
456
516
  };
457
517
 
458
518
  type AlgorithmDictionaries = {
459
- [key in string]: Object;
519
+ [key in string]: object;
460
520
  };
461
521
 
462
522
  const simpleAlgorithmDictionaries: AlgorithmDictionaries = {
@@ -481,13 +541,16 @@ const simpleAlgorithmDictionaries: AlgorithmDictionaries = {
481
541
 
482
542
  export const validateMaxBufferLength = (
483
543
  data: BinaryLike | BufferLike,
484
- name: string
544
+ name: string,
485
545
  ): void => {
486
- const length = typeof data === 'string' ? data.length : data.byteLength;
546
+ const length =
547
+ typeof data === 'string' || data instanceof SBuffer
548
+ ? data.length
549
+ : data.byteLength;
487
550
  if (length > kMaxBufferLength) {
488
551
  throw lazyDOMException(
489
552
  `${name} must be less than ${kMaxBufferLength + 1} bits`,
490
- 'OperationError'
553
+ 'OperationError',
491
554
  );
492
555
  }
493
556
  };
@@ -497,7 +560,7 @@ export const validateMaxBufferLength = (
497
560
  // https://github.com/denoland/deno/blob/v1.29.1/ext/crypto/00_crypto.js#L195
498
561
  export const normalizeAlgorithm = (
499
562
  algorithm: SubtleAlgorithm | EncryptDecryptParams | AnyAlgorithm,
500
- op: Operation
563
+ op: Operation,
501
564
  ): SubtleAlgorithm | EncryptDecryptParams => {
502
565
  if (typeof algorithm === 'string') {
503
566
  return normalizeAlgorithm({ name: algorithm }, op);
@@ -514,19 +577,19 @@ export const normalizeAlgorithm = (
514
577
 
515
578
  // 4.
516
579
  let algName = algorithm.name;
517
- // @ts-expect-error
518
- if (algName === undefined) return { name: undefined };
580
+ if (algName === undefined) return { name: 'unknown' };
519
581
 
520
582
  // 5.
521
583
  let desiredType: string | null | undefined;
522
584
  for (const key in registeredAlgorithms) {
523
- if (!registeredAlgorithms.hasOwnProperty(key)) {
585
+ if (!Object.prototype.hasOwnProperty.call(registeredAlgorithms, key)) {
524
586
  continue;
525
587
  }
526
588
  if (key.toUpperCase() === algName.toUpperCase()) {
527
589
  algName = key as AnyAlgorithm;
528
- // @ts-ignore
529
- desiredType = registeredAlgorithms[algName];
590
+ desiredType = (
591
+ registeredAlgorithms as Record<string, typeof desiredType>
592
+ )[algName];
530
593
  }
531
594
  }
532
595
  if (desiredType === undefined)
@@ -551,7 +614,7 @@ export const normalizeAlgorithm = (
551
614
  const dictKeys = dict ? Object.keys(dict) : [];
552
615
  for (let i = 0; i < dictKeys.length; i++) {
553
616
  const member = dictKeys[i] || '';
554
- if (!dict?.hasOwnProperty(member)) continue;
617
+ if (!Object.prototype.hasOwnProperty.call(dict, member)) continue;
555
618
  // TODO: implement this? Maybe via typescript?
556
619
  // const idlType = dict[member];
557
620
  // const idlValue = normalizedAlgorithm[member];
@@ -581,7 +644,7 @@ export const normalizeAlgorithm = (
581
644
  export const validateBitLength = (
582
645
  length: number,
583
646
  name: string,
584
- required: boolean = false
647
+ required: boolean = false,
585
648
  ) => {
586
649
  if (length !== undefined || required) {
587
650
  // validateNumber(length, name);
@@ -589,7 +652,7 @@ export const validateBitLength = (
589
652
  if (length % 8) {
590
653
  throw lazyDOMException(
591
654
  `${name}'s length (${length}) must be a multiple of 8`,
592
- 'InvalidArgument'
655
+ 'InvalidArgument',
593
656
  );
594
657
  }
595
658
  }
@@ -598,12 +661,15 @@ export const validateBitLength = (
598
661
  export const validateByteLength = (
599
662
  buf: BufferLike,
600
663
  name: string,
601
- target: number
664
+ target: number,
602
665
  ) => {
603
- if (buf.byteLength !== target) {
666
+ if (
667
+ (SBuffer.isBuffer(buf) && buf.length !== target) ||
668
+ (buf as Buffer | ArrayBuffer | ArrayBufferView).byteLength !== target
669
+ ) {
604
670
  throw lazyDOMException(
605
671
  `${name} must contain exactly ${target} bytes`,
606
- 'OperationError'
672
+ 'OperationError',
607
673
  );
608
674
  }
609
675
  };
@@ -633,7 +699,7 @@ const kKeyOps: {
633
699
 
634
700
  export const validateKeyOps = (
635
701
  keyOps: KeyUsage[] | undefined,
636
- usagesSet: KeyUsage[]
702
+ usagesSet: KeyUsage[],
637
703
  ) => {
638
704
  if (keyOps === undefined) return;
639
705
  if (!Array.isArray(keyOps)) {
@@ -646,10 +712,8 @@ export const validateKeyOps = (
646
712
  // Skipping unknown key ops
647
713
  if (op_flag === undefined) continue;
648
714
  // Have we seen it already? if so, error
649
- // eslint-disable-next-line no-bitwise
650
715
  if (flags & (1 << op_flag))
651
716
  throw lazyDOMException('Duplicate key operation', 'DataError');
652
- // eslint-disable-next-line no-bitwise
653
717
  flags |= 1 << op_flag;
654
718
 
655
719
  // TODO(@jasnell): RFC7517 section 4.3 strong recommends validating
@@ -662,7 +726,7 @@ export const validateKeyOps = (
662
726
  if (!keyOps.includes(use)) {
663
727
  throw lazyDOMException(
664
728
  'Key operations and usage mismatch',
665
- 'DataError'
729
+ 'DataError',
666
730
  );
667
731
  }
668
732
  }
@@ -677,21 +741,30 @@ export const validateKeyOps = (
677
741
  // https://github.com/chromium/chromium/blob/HEAD/third_party/blink/public/platform/web_crypto_algorithm_params.h, but ported to JavaScript
678
742
  // Returns undefined if the conversion was unsuccessful.
679
743
  export const bigIntArrayToUnsignedInt = (
680
- input: Uint8Array
744
+ input: Uint8Array,
681
745
  ): number | undefined => {
682
746
  let result = 0;
683
747
 
684
748
  for (let n = 0; n < input.length; ++n) {
685
749
  const n_reversed = input.length - n - 1;
686
750
  if (n_reversed >= 4 && input[n]) return; // Too large
687
- // @ts-ignore
688
- // eslint-disable-next-line no-bitwise
751
+ // @ts-expect-error - input[n] is possibly undefined
689
752
  result |= input[n] << (8 * n_reversed);
690
753
  }
691
754
 
692
755
  return result;
693
756
  };
694
757
 
758
+ export function abvToArrayBuffer(buffer: ArrayBufferView): ArrayBuffer {
759
+ if (Buffer.isBuffer(buffer)) {
760
+ return buffer.buffer;
761
+ }
762
+ if (ArrayBuffer.isView(buffer)) {
763
+ return buffer.buffer;
764
+ }
765
+ return buffer;
766
+ }
767
+
695
768
  // TODO: these used to be shipped by crypto-browserify in quickcrypto v0.6
696
769
  // could instead fetch from OpenSSL if needed and handle breaking changes
697
770
  export const getHashes = () => [