genanki-js 2.0.0 → 2.1.0
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.md +22 -22
- package/README.md +179 -89
- package/dist/genanki.cjs +1277 -0
- package/dist/genanki.js +1270 -536
- package/dist/genanki.mjs +1277 -0
- package/genanki/anki_hash.js +31 -28
- package/genanki/apkg_col.js +107 -0
- package/genanki/apkg_schema.js +83 -83
- package/genanki/deck.js +12 -12
- package/genanki/default.js +135 -135
- package/genanki/license.js +15 -15
- package/genanki/model.js +61 -58
- package/genanki/note.js +62 -59
- package/genanki/package.js +239 -134
- package/genanki/sha256.js +518 -0
- package/package.json +55 -36
- package/Changelog.md +0 -13
- package/dist/genanki-min.js +0 -1
- package/genanki/import.js +0 -4
- package/gulpfile.js +0 -28
package/dist/genanki.js
CHANGED
|
@@ -1,536 +1,1270 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* [mkanki]{@link https://github.com/nornagon/mkanki}
|
|
3
|
-
* @copyright Copyright (c) 2018 Jeremy Apthorp
|
|
4
|
-
* @license AGPL-3.0 License
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* [genanki]{@link https://github.com/kerrickstaley/genanki}
|
|
8
|
-
* @copyright Copyright (c) Kerrick Staley 2021
|
|
9
|
-
* @license The MIT License
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* [genanki-js]{@link https://github.com/krmanik/genanki-js}
|
|
13
|
-
* @copyright Copyright (c) 2021 Mani
|
|
14
|
-
* @license AGPL-3.0 License
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
font-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
color:
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
\\
|
|
115
|
-
\\
|
|
116
|
-
\\
|
|
117
|
-
\\
|
|
118
|
-
\\
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
'
|
|
170
|
-
'
|
|
171
|
-
'
|
|
172
|
-
'
|
|
173
|
-
'
|
|
174
|
-
|
|
175
|
-
'
|
|
176
|
-
|
|
177
|
-
'
|
|
178
|
-
'
|
|
179
|
-
'
|
|
180
|
-
'
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
'
|
|
196
|
-
'
|
|
197
|
-
'
|
|
198
|
-
'
|
|
199
|
-
|
|
200
|
-
'
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
'
|
|
206
|
-
'
|
|
207
|
-
|
|
208
|
-
'
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
'
|
|
214
|
-
'
|
|
215
|
-
'
|
|
216
|
-
'
|
|
217
|
-
|
|
218
|
-
'
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
'
|
|
224
|
-
'
|
|
225
|
-
'
|
|
226
|
-
'
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
this.
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
this._guid
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
get
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
let
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
const
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
)
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
)
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
)
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
1
|
+
/**
|
|
2
|
+
* [mkanki]{@link https://github.com/nornagon/mkanki}
|
|
3
|
+
* @copyright Copyright (c) 2018 Jeremy Apthorp
|
|
4
|
+
* @license AGPL-3.0 License
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* [genanki]{@link https://github.com/kerrickstaley/genanki}
|
|
8
|
+
* @copyright Copyright (c) Kerrick Staley 2021
|
|
9
|
+
* @license The MIT License
|
|
10
|
+
*
|
|
11
|
+
*
|
|
12
|
+
* [genanki-js]{@link https://github.com/krmanik/genanki-js}
|
|
13
|
+
* @copyright Copyright (c) 2021 Mani
|
|
14
|
+
* @license AGPL-3.0 License
|
|
15
|
+
*/
|
|
16
|
+
const BASE91_TABLE = [
|
|
17
|
+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
|
|
18
|
+
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
|
|
19
|
+
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
|
|
20
|
+
'5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', ':',
|
|
21
|
+
';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~']
|
|
22
|
+
|
|
23
|
+
function ankiHash(fields) {
|
|
24
|
+
const str = fields.join('__')
|
|
25
|
+
const h = sha256.create();
|
|
26
|
+
h.update(str)
|
|
27
|
+
const hex = h.digest()
|
|
28
|
+
|
|
29
|
+
// The first 8 bytes of the digest form a big-endian unsigned 64-bit integer,
|
|
30
|
+
// which is converted to the weird base91 format that Anki uses.
|
|
31
|
+
// Long division over the byte array is used instead of BigInt, because
|
|
32
|
+
// BigInt (and the `0n` literal syntax) is not supported on older Safari / iOS.
|
|
33
|
+
const digits = hex.slice(0, 8)
|
|
34
|
+
let rv_reversed = []
|
|
35
|
+
while (digits.some(d => d !== 0)) {
|
|
36
|
+
let remainder = 0
|
|
37
|
+
for (let i = 0; i < digits.length; i++) {
|
|
38
|
+
const acc = remainder * 256 + digits[i]
|
|
39
|
+
digits[i] = Math.floor(acc / 91)
|
|
40
|
+
remainder = acc % 91
|
|
41
|
+
}
|
|
42
|
+
rv_reversed.push(BASE91_TABLE[remainder])
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return rv_reversed.reverse().join('')
|
|
46
|
+
}
|
|
47
|
+
const MODEL_STD = 0
|
|
48
|
+
const MODEL_CLOZE = 1
|
|
49
|
+
|
|
50
|
+
class Model {
|
|
51
|
+
constructor(props) {
|
|
52
|
+
this.props = {
|
|
53
|
+
...defaultModel,
|
|
54
|
+
...props,
|
|
55
|
+
flds: props.flds.map((f, i) => ({ ...defaultField, ord: i, ...f })),
|
|
56
|
+
tmpls: props.tmpls.map((t, i) => ({ ...defaultTemplate, ord: i, name: `Card ${i + 1}`, ...t })),
|
|
57
|
+
mod: new Date().getTime()
|
|
58
|
+
}
|
|
59
|
+
this.fieldNameToOrd = {}
|
|
60
|
+
this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
note(fields, tags, guid = null) {
|
|
64
|
+
if (Array.isArray(fields)) {
|
|
65
|
+
if (fields.length !== this.props.flds.length) {
|
|
66
|
+
throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
|
|
67
|
+
}
|
|
68
|
+
return new Note(this, fields, tags, guid)
|
|
69
|
+
} else {
|
|
70
|
+
const field_names = Object.keys(fields)
|
|
71
|
+
const fields_list = []
|
|
72
|
+
field_names.forEach(field_name => {
|
|
73
|
+
const ord = this.fieldNameToOrd[field_name]
|
|
74
|
+
if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
|
|
75
|
+
fields_list[ord] = fields[field_name]
|
|
76
|
+
})
|
|
77
|
+
return new Note(this, fields_list, tags, guid)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
class ClozeModel extends Model {
|
|
83
|
+
constructor(props) {
|
|
84
|
+
// `tmpl` is only used to build the single cloze template,
|
|
85
|
+
// it must not end up in the note type json
|
|
86
|
+
const { tmpl, ...rest } = props
|
|
87
|
+
super({
|
|
88
|
+
type: MODEL_CLOZE,
|
|
89
|
+
css: `
|
|
90
|
+
.card {
|
|
91
|
+
font-family: arial;
|
|
92
|
+
font-size: 20px;
|
|
93
|
+
text-align: center;
|
|
94
|
+
color: black;
|
|
95
|
+
background-color: white;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.cloze {
|
|
99
|
+
font-weight: bold;
|
|
100
|
+
color: blue;
|
|
101
|
+
}
|
|
102
|
+
`,
|
|
103
|
+
tmpls: [{ name: "Cloze", ...tmpl }],
|
|
104
|
+
...rest
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const defaultModel = {
|
|
109
|
+
sortf: 0, // sort field
|
|
110
|
+
did: 1, // deck id
|
|
111
|
+
latexPre: `\\documentclass[12pt]{article}
|
|
112
|
+
\\special{papersize=3in,5in}
|
|
113
|
+
\\usepackage[utf8]{inputenc}
|
|
114
|
+
\\usepackage{amssymb,amsmath}
|
|
115
|
+
\\pagestyle{empty}
|
|
116
|
+
\\setlength{\\parindent}{0in}
|
|
117
|
+
\\begin{document}`,
|
|
118
|
+
latexPost: "\\end{document}",
|
|
119
|
+
mod: 0, // modification time
|
|
120
|
+
usn: 0, // unsure, something to do with sync?
|
|
121
|
+
vers: [], // seems to be unused
|
|
122
|
+
type: MODEL_STD,
|
|
123
|
+
css: `.card {
|
|
124
|
+
font-family: arial;
|
|
125
|
+
font-size: 20px;
|
|
126
|
+
text-align: center;
|
|
127
|
+
color: black;
|
|
128
|
+
background-color: white;
|
|
129
|
+
}`,
|
|
130
|
+
/* also:
|
|
131
|
+
name: string,
|
|
132
|
+
flds: [Field],
|
|
133
|
+
tmpls: [Template],
|
|
134
|
+
tags: [??],
|
|
135
|
+
id: string
|
|
136
|
+
*/
|
|
137
|
+
tags: [],
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const defaultField = {
|
|
141
|
+
name: "",
|
|
142
|
+
ord: null,
|
|
143
|
+
sticky: false,
|
|
144
|
+
rtl: false,
|
|
145
|
+
font: "Arial",
|
|
146
|
+
size: 20,
|
|
147
|
+
media: [],
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const defaultTemplate = {
|
|
151
|
+
name: "",
|
|
152
|
+
ord: null,
|
|
153
|
+
qfmt: "",
|
|
154
|
+
afmt: "",
|
|
155
|
+
did: null,
|
|
156
|
+
bqfmt: "",
|
|
157
|
+
bafmt: "",
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// whether new cards should be mixed with reviews, or shown first or last
|
|
161
|
+
const NEW_CARDS_DISTRIBUTE = 0
|
|
162
|
+
const NEW_CARDS_LAST = 1
|
|
163
|
+
const NEW_CARDS_FIRST = 2
|
|
164
|
+
|
|
165
|
+
const defaultConf = {
|
|
166
|
+
// review options
|
|
167
|
+
'activeDecks': [1],
|
|
168
|
+
'curDeck': 1,
|
|
169
|
+
'newSpread': NEW_CARDS_DISTRIBUTE,
|
|
170
|
+
'collapseTime': 1200,
|
|
171
|
+
'timeLim': 0,
|
|
172
|
+
'estTimes': true,
|
|
173
|
+
'dueCounts': true,
|
|
174
|
+
// other config
|
|
175
|
+
'curModel': null,
|
|
176
|
+
'nextPos': 1,
|
|
177
|
+
'sortType': "noteFld",
|
|
178
|
+
'sortBackwards': false,
|
|
179
|
+
'addToCur': true, // add new to currently selected deck?
|
|
180
|
+
'dayLearnFirst': false,
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
// new card insertion order
|
|
185
|
+
const NEW_CARDS_RANDOM = 0
|
|
186
|
+
const NEW_CARDS_DUE = 1
|
|
187
|
+
|
|
188
|
+
const STARTING_FACTOR = 2500
|
|
189
|
+
|
|
190
|
+
const defaultDeckConf = {
|
|
191
|
+
'name': "Default",
|
|
192
|
+
'new': {
|
|
193
|
+
'delays': [1, 10],
|
|
194
|
+
'ints': [1, 4, 7], // 7 is not currently used
|
|
195
|
+
'initialFactor': STARTING_FACTOR,
|
|
196
|
+
'separate': true,
|
|
197
|
+
'order': NEW_CARDS_DUE,
|
|
198
|
+
'perDay': 20,
|
|
199
|
+
// may not be set on old decks
|
|
200
|
+
'bury': false,
|
|
201
|
+
},
|
|
202
|
+
'lapse': {
|
|
203
|
+
'delays': [10],
|
|
204
|
+
'mult': 0,
|
|
205
|
+
'minInt': 1,
|
|
206
|
+
'leechFails': 8,
|
|
207
|
+
// type 0=suspend, 1=tagonly
|
|
208
|
+
'leechAction': 0,
|
|
209
|
+
},
|
|
210
|
+
'rev': {
|
|
211
|
+
'perDay': 200,
|
|
212
|
+
'ease4': 1.3,
|
|
213
|
+
'fuzz': 0.05,
|
|
214
|
+
'minSpace': 1, // not currently used
|
|
215
|
+
'ivlFct': 1,
|
|
216
|
+
'maxIvl': 36500,
|
|
217
|
+
// may not be set on old decks
|
|
218
|
+
'bury': false,
|
|
219
|
+
'hardFactor': 1.2,
|
|
220
|
+
},
|
|
221
|
+
'maxTaken': 60,
|
|
222
|
+
'timer': 0,
|
|
223
|
+
'autoplay': true,
|
|
224
|
+
'replayq': true,
|
|
225
|
+
'mod': 0,
|
|
226
|
+
'usn': 0,
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const defaultDeck = {
|
|
230
|
+
newToday: [0, 0], // currentDay, count
|
|
231
|
+
revToday: [0, 0],
|
|
232
|
+
lrnToday: [0, 0],
|
|
233
|
+
timeToday: [0, 0], // time in ms
|
|
234
|
+
conf: 1,
|
|
235
|
+
usn: 0,
|
|
236
|
+
desc: "",
|
|
237
|
+
dyn: 0, // anki uses int/bool interchangably here
|
|
238
|
+
collapsed: false,
|
|
239
|
+
// added in beta11
|
|
240
|
+
extendNew: 10,
|
|
241
|
+
extendRev: 50,
|
|
242
|
+
}
|
|
243
|
+
class Deck {
|
|
244
|
+
constructor(id, name, desc="") {
|
|
245
|
+
this.id = id
|
|
246
|
+
this.name = name
|
|
247
|
+
this.desc = desc
|
|
248
|
+
this.notes = []
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
addNote(note) {
|
|
252
|
+
this.notes.push(note)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
class Note {
|
|
256
|
+
constructor(model, fields, tags = null, guid = null) {
|
|
257
|
+
this.model = model
|
|
258
|
+
this.fields = fields
|
|
259
|
+
this.tags = tags
|
|
260
|
+
this._guid = guid
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
get guid() {
|
|
264
|
+
return this._guid ? this._guid : ankiHash(this.fields);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
get cards() {
|
|
268
|
+
if (this.model.props.type === MODEL_STD) {
|
|
269
|
+
const isEmpty = f => {
|
|
270
|
+
return !f || f.toString().trim().length === 0
|
|
271
|
+
}
|
|
272
|
+
const rv = []
|
|
273
|
+
for (const [card_ord, any_or_all, required_field_ords] of this.model.props.req) {
|
|
274
|
+
const op = any_or_all === "any" ? "some" : "every"
|
|
275
|
+
if (required_field_ords[op](f => !isEmpty(this.fields[f]))) {
|
|
276
|
+
rv.push(card_ord)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return rv
|
|
280
|
+
} else {
|
|
281
|
+
// the below logic is copied from anki's ModelManager._availClozeOrds
|
|
282
|
+
const ords = new Set()
|
|
283
|
+
const matches = []
|
|
284
|
+
const curliesRe = /{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g
|
|
285
|
+
const percentRe = /<%cloze:(.+?)%>/g
|
|
286
|
+
const { qfmt } = this.model.props.tmpls[0] // cloze models only have 1 template
|
|
287
|
+
let m;
|
|
288
|
+
while (m = curliesRe.exec(qfmt))
|
|
289
|
+
matches.push(m[1])
|
|
290
|
+
while (m = percentRe.exec(qfmt))
|
|
291
|
+
matches.push(m[1])
|
|
292
|
+
const map = {}
|
|
293
|
+
this.model.props.flds.forEach((fld, i) => {
|
|
294
|
+
map[fld.name] = [i, fld]
|
|
295
|
+
})
|
|
296
|
+
for (const fname of matches) {
|
|
297
|
+
if (!(fname in map)) continue
|
|
298
|
+
const ord = map[fname][0]
|
|
299
|
+
// matches {{c1::text}} and {{c1,2::text}} (one deletion shown on several cards)
|
|
300
|
+
const re = /{{c(\d+(?:,\d+)*)::[\s\S]+?}}/g
|
|
301
|
+
while (m = re.exec(this.fields[ord])) {
|
|
302
|
+
for (const n of m[1].split(',')) {
|
|
303
|
+
const i = parseInt(n)
|
|
304
|
+
if (i > 0)
|
|
305
|
+
ords.add(i - 1)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (ords.size === 0) {
|
|
310
|
+
// empty clozes use first ord
|
|
311
|
+
return [0]
|
|
312
|
+
}
|
|
313
|
+
return Array.from(ords)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// Dependencies used to create the package.
|
|
318
|
+
// When genanki.js is loaded with a <script> tag, the global `SQL` (or `initSqlJs`),
|
|
319
|
+
// `JSZip` and `saveAs` (FileSaver.js, optional) are used.
|
|
320
|
+
// The npm builds (ES module / CommonJS) set these from the installed packages.
|
|
321
|
+
const genankiDeps = {
|
|
322
|
+
SQL: null, // initialized sql.js, see setSqlJs()
|
|
323
|
+
sqlPromise: null, // Promise<SQL> while sql.js is being loaded
|
|
324
|
+
loadSqlJs: null, // () => Promise<SQL>
|
|
325
|
+
JSZip: null,
|
|
326
|
+
saveAs: null,
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Use an initialized sql.js instance for all packages.
|
|
331
|
+
* `SQL` is the value `initSqlJs()` resolves to.
|
|
332
|
+
*/
|
|
333
|
+
function setSqlJs(SQL) {
|
|
334
|
+
genankiDeps.SQL = SQL
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function resolveSqlJs(packageSQL) {
|
|
338
|
+
if (packageSQL) return Promise.resolve(packageSQL)
|
|
339
|
+
if (genankiDeps.SQL) return Promise.resolve(genankiDeps.SQL)
|
|
340
|
+
if (typeof SQL !== "undefined" && SQL) return Promise.resolve(SQL)
|
|
341
|
+
|
|
342
|
+
if (!genankiDeps.sqlPromise) {
|
|
343
|
+
let load = genankiDeps.loadSqlJs
|
|
344
|
+
if (!load && typeof initSqlJs === "function") {
|
|
345
|
+
load = () => initSqlJs()
|
|
346
|
+
}
|
|
347
|
+
if (!load) {
|
|
348
|
+
return Promise.reject(new Error("sql.js is not loaded. Add sql.js to the page and assign the result " +
|
|
349
|
+
"of initSqlJs() to a global `SQL` variable, or pass it with setSqlJs(SQL)."))
|
|
350
|
+
}
|
|
351
|
+
genankiDeps.sqlPromise = load().then(sql => {
|
|
352
|
+
genankiDeps.SQL = sql
|
|
353
|
+
return sql
|
|
354
|
+
}, err => {
|
|
355
|
+
genankiDeps.sqlPromise = null
|
|
356
|
+
throw err
|
|
357
|
+
})
|
|
358
|
+
}
|
|
359
|
+
return genankiDeps.sqlPromise
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function resolveJSZip() {
|
|
363
|
+
if (genankiDeps.JSZip) return genankiDeps.JSZip
|
|
364
|
+
if (typeof JSZip !== "undefined" && JSZip) return JSZip
|
|
365
|
+
throw new Error("JSZip is not loaded. Add jszip.min.js to the page.")
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function saveFile(content, filename) {
|
|
369
|
+
const saver = genankiDeps.saveAs || (typeof saveAs === "function" ? saveAs : null)
|
|
370
|
+
if (saver) {
|
|
371
|
+
saver(content, filename)
|
|
372
|
+
return
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// FileSaver.js is optional, download with a temporary link instead
|
|
376
|
+
if (typeof document !== "undefined" && typeof URL !== "undefined" && URL.createObjectURL) {
|
|
377
|
+
const url = URL.createObjectURL(content)
|
|
378
|
+
const a = document.createElement("a")
|
|
379
|
+
a.href = url
|
|
380
|
+
a.download = filename
|
|
381
|
+
a.style.display = "none"
|
|
382
|
+
document.body.appendChild(a)
|
|
383
|
+
a.click()
|
|
384
|
+
document.body.removeChild(a)
|
|
385
|
+
setTimeout(() => URL.revokeObjectURL(url), 40000)
|
|
386
|
+
return
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
throw new Error("writeToFile() can only download files in a browser. " +
|
|
390
|
+
"Use package.generate('nodebuffer') or package.generate('uint8array') and write the result to a file.")
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
class Package {
|
|
394
|
+
constructor() {
|
|
395
|
+
this.decks = []
|
|
396
|
+
this.media = []
|
|
397
|
+
this.SQL = null
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Use an initialized sql.js instance for this package.
|
|
402
|
+
*/
|
|
403
|
+
setSqlJs(SQL) {
|
|
404
|
+
this.SQL = SQL
|
|
405
|
+
return this
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
addDeck(deck) {
|
|
409
|
+
// adding the same deck twice would duplicate all of its notes
|
|
410
|
+
if (!this.decks.includes(deck)) {
|
|
411
|
+
this.decks.push(deck)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
addMedia(data, name) {
|
|
416
|
+
this.media.push({ name, data })
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
addMediaFile(filename, name = null) {
|
|
420
|
+
this.media.push({ name: name || filename, filename })
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Create the .apkg file content.
|
|
425
|
+
* `type` is any JSZip output type: "blob" (default), "uint8array", "arraybuffer", "base64",
|
|
426
|
+
* or "nodebuffer" (Node.js only).
|
|
427
|
+
* Returns a Promise.
|
|
428
|
+
*/
|
|
429
|
+
generate(type = "blob") {
|
|
430
|
+
return resolveSqlJs(this.SQL).then(SQL => {
|
|
431
|
+
const ZipClass = resolveJSZip()
|
|
432
|
+
|
|
433
|
+
const db = new SQL.Database()
|
|
434
|
+
let data
|
|
435
|
+
try {
|
|
436
|
+
db.run(APKG_SCHEMA)
|
|
437
|
+
this.write(db)
|
|
438
|
+
data = db.export()
|
|
439
|
+
} finally {
|
|
440
|
+
db.close()
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const zip = new ZipClass()
|
|
444
|
+
zip.file("collection.anki2", data)
|
|
445
|
+
|
|
446
|
+
const media_info = {}
|
|
447
|
+
|
|
448
|
+
this.media.forEach((m, i) => {
|
|
449
|
+
if (m.filename != null) {
|
|
450
|
+
zip.file(i.toString(), m.filename)
|
|
451
|
+
} else {
|
|
452
|
+
zip.file(i.toString(), m.data)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
media_info[i] = m.name
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
zip.file('media', JSON.stringify(media_info))
|
|
459
|
+
|
|
460
|
+
return zip.generateAsync({ type, mimeType: "application/apkg" })
|
|
461
|
+
})
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Download the package as `filename` (browser only).
|
|
466
|
+
* Returns a Promise that resolves with the generated Blob.
|
|
467
|
+
*/
|
|
468
|
+
writeToFile(filename) {
|
|
469
|
+
return this.generate("blob").then(content => {
|
|
470
|
+
saveFile(content, filename)
|
|
471
|
+
return content
|
|
472
|
+
})
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
write(db) {
|
|
477
|
+
const now = new Date
|
|
478
|
+
const models = {}
|
|
479
|
+
const decks = {}
|
|
480
|
+
|
|
481
|
+
// AnkiDroid failed to import subdeck, So add a Default deck
|
|
482
|
+
decks["1"] = {...defaultDeck, id: 1, name: "Default"}
|
|
483
|
+
|
|
484
|
+
this.decks.forEach(d => {
|
|
485
|
+
d.notes.forEach(n => models[n.model.props.id] = n.model.props)
|
|
486
|
+
decks[d.id] = {
|
|
487
|
+
...defaultDeck,
|
|
488
|
+
id: d.id,
|
|
489
|
+
name: d.name,
|
|
490
|
+
desc: d.desc,
|
|
491
|
+
}
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
const col = [
|
|
495
|
+
null, // id
|
|
496
|
+
(+now / 1000) | 0, // crt
|
|
497
|
+
+now, // mod
|
|
498
|
+
+now, // scm
|
|
499
|
+
11, // ver
|
|
500
|
+
0, // dty
|
|
501
|
+
0, // usn
|
|
502
|
+
0, // ls
|
|
503
|
+
JSON.stringify(defaultConf), // conf
|
|
504
|
+
JSON.stringify(models), // models
|
|
505
|
+
JSON.stringify(decks), // decks
|
|
506
|
+
JSON.stringify({ 1: { id: 1, ...defaultDeckConf } }), // dconf
|
|
507
|
+
JSON.stringify({}), // tags
|
|
508
|
+
]
|
|
509
|
+
|
|
510
|
+
db.prepare(`INSERT INTO col
|
|
511
|
+
(id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
|
|
512
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(col)
|
|
513
|
+
|
|
514
|
+
const insert_notes = db.prepare(`INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
|
|
515
|
+
VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`)
|
|
516
|
+
|
|
517
|
+
const insert_cards = db.prepare(`INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
|
|
518
|
+
VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')`)
|
|
519
|
+
|
|
520
|
+
for (const deck of this.decks) {
|
|
521
|
+
for (const note of deck.notes) {
|
|
522
|
+
var tags = note.tags == null ? '' : note.tags.join(' ')
|
|
523
|
+
insert_notes.run(
|
|
524
|
+
[
|
|
525
|
+
note.guid, // guid
|
|
526
|
+
note.model.props.id, // mid
|
|
527
|
+
(+now / 1000) | 0, // mod
|
|
528
|
+
-1, // usn
|
|
529
|
+
tags, // tags
|
|
530
|
+
note.fields.join('\x1f'), // flds
|
|
531
|
+
0, // sfld
|
|
532
|
+
])
|
|
533
|
+
|
|
534
|
+
var rowID = db.exec("select last_insert_rowid();")
|
|
535
|
+
var note_id = rowID[0]['values'][0][0];
|
|
536
|
+
|
|
537
|
+
for (const card_ord of note.cards) {
|
|
538
|
+
insert_cards.run(
|
|
539
|
+
[
|
|
540
|
+
note_id, // nid
|
|
541
|
+
deck.id, // did
|
|
542
|
+
card_ord, // ord
|
|
543
|
+
(+now / 1000) | 0, // mod
|
|
544
|
+
-1, // usn
|
|
545
|
+
0, // type 0=new, 1=learning, 2=due
|
|
546
|
+
0, // queue -1 for suspended
|
|
547
|
+
])
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
insert_notes.free()
|
|
553
|
+
insert_cards.free()
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
var sha256 = (function () {
|
|
557
|
+
var module = { exports: {} }, define, process;
|
|
558
|
+
/**
|
|
559
|
+
* [js-sha256]{@link https://github.com/emn178/js-sha256}
|
|
560
|
+
*
|
|
561
|
+
* @version 0.9.0
|
|
562
|
+
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
|
563
|
+
* @copyright Chen, Yi-Cyuan 2014-2017
|
|
564
|
+
* @license MIT
|
|
565
|
+
*/
|
|
566
|
+
/*jslint bitwise: true */
|
|
567
|
+
(function () {
|
|
568
|
+
'use strict';
|
|
569
|
+
|
|
570
|
+
var ERROR = 'input is invalid type';
|
|
571
|
+
var WINDOW = typeof window === 'object';
|
|
572
|
+
var root = WINDOW ? window : {};
|
|
573
|
+
if (root.JS_SHA256_NO_WINDOW) {
|
|
574
|
+
WINDOW = false;
|
|
575
|
+
}
|
|
576
|
+
var WEB_WORKER = !WINDOW && typeof self === 'object';
|
|
577
|
+
var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
|
|
578
|
+
if (NODE_JS) {
|
|
579
|
+
root = global;
|
|
580
|
+
} else if (WEB_WORKER) {
|
|
581
|
+
root = self;
|
|
582
|
+
}
|
|
583
|
+
var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
|
|
584
|
+
var AMD = typeof define === 'function' && define.amd;
|
|
585
|
+
var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
|
|
586
|
+
var HEX_CHARS = '0123456789abcdef'.split('');
|
|
587
|
+
var EXTRA = [-2147483648, 8388608, 32768, 128];
|
|
588
|
+
var SHIFT = [24, 16, 8, 0];
|
|
589
|
+
var K = [
|
|
590
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
591
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
592
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
593
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
594
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
595
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
596
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
597
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
598
|
+
];
|
|
599
|
+
var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
|
|
600
|
+
|
|
601
|
+
var blocks = [];
|
|
602
|
+
|
|
603
|
+
if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
|
|
604
|
+
Array.isArray = function (obj) {
|
|
605
|
+
return Object.prototype.toString.call(obj) === '[object Array]';
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
|
|
610
|
+
ArrayBuffer.isView = function (obj) {
|
|
611
|
+
return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
var createOutputMethod = function (outputType, is224) {
|
|
616
|
+
return function (message) {
|
|
617
|
+
return new Sha256(is224, true).update(message)[outputType]();
|
|
618
|
+
};
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
var createMethod = function (is224) {
|
|
622
|
+
var method = createOutputMethod('hex', is224);
|
|
623
|
+
if (NODE_JS) {
|
|
624
|
+
method = nodeWrap(method, is224);
|
|
625
|
+
}
|
|
626
|
+
method.create = function () {
|
|
627
|
+
return new Sha256(is224);
|
|
628
|
+
};
|
|
629
|
+
method.update = function (message) {
|
|
630
|
+
return method.create().update(message);
|
|
631
|
+
};
|
|
632
|
+
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
|
|
633
|
+
var type = OUTPUT_TYPES[i];
|
|
634
|
+
method[type] = createOutputMethod(type, is224);
|
|
635
|
+
}
|
|
636
|
+
return method;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
var nodeWrap = function (method, is224) {
|
|
640
|
+
var crypto = eval("require('crypto')");
|
|
641
|
+
var Buffer = eval("require('buffer').Buffer");
|
|
642
|
+
var algorithm = is224 ? 'sha224' : 'sha256';
|
|
643
|
+
var nodeMethod = function (message) {
|
|
644
|
+
if (typeof message === 'string') {
|
|
645
|
+
return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
|
|
646
|
+
} else {
|
|
647
|
+
if (message === null || message === undefined) {
|
|
648
|
+
throw new Error(ERROR);
|
|
649
|
+
} else if (message.constructor === ArrayBuffer) {
|
|
650
|
+
message = new Uint8Array(message);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (Array.isArray(message) || ArrayBuffer.isView(message) ||
|
|
654
|
+
message.constructor === Buffer) {
|
|
655
|
+
return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
|
|
656
|
+
} else {
|
|
657
|
+
return method(message);
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
return nodeMethod;
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
var createHmacOutputMethod = function (outputType, is224) {
|
|
664
|
+
return function (key, message) {
|
|
665
|
+
return new HmacSha256(key, is224, true).update(message)[outputType]();
|
|
666
|
+
};
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
var createHmacMethod = function (is224) {
|
|
670
|
+
var method = createHmacOutputMethod('hex', is224);
|
|
671
|
+
method.create = function (key) {
|
|
672
|
+
return new HmacSha256(key, is224);
|
|
673
|
+
};
|
|
674
|
+
method.update = function (key, message) {
|
|
675
|
+
return method.create(key).update(message);
|
|
676
|
+
};
|
|
677
|
+
for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
|
|
678
|
+
var type = OUTPUT_TYPES[i];
|
|
679
|
+
method[type] = createHmacOutputMethod(type, is224);
|
|
680
|
+
}
|
|
681
|
+
return method;
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
function Sha256(is224, sharedMemory) {
|
|
685
|
+
if (sharedMemory) {
|
|
686
|
+
blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
|
687
|
+
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
|
688
|
+
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
|
689
|
+
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
|
690
|
+
this.blocks = blocks;
|
|
691
|
+
} else {
|
|
692
|
+
this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (is224) {
|
|
696
|
+
this.h0 = 0xc1059ed8;
|
|
697
|
+
this.h1 = 0x367cd507;
|
|
698
|
+
this.h2 = 0x3070dd17;
|
|
699
|
+
this.h3 = 0xf70e5939;
|
|
700
|
+
this.h4 = 0xffc00b31;
|
|
701
|
+
this.h5 = 0x68581511;
|
|
702
|
+
this.h6 = 0x64f98fa7;
|
|
703
|
+
this.h7 = 0xbefa4fa4;
|
|
704
|
+
} else { // 256
|
|
705
|
+
this.h0 = 0x6a09e667;
|
|
706
|
+
this.h1 = 0xbb67ae85;
|
|
707
|
+
this.h2 = 0x3c6ef372;
|
|
708
|
+
this.h3 = 0xa54ff53a;
|
|
709
|
+
this.h4 = 0x510e527f;
|
|
710
|
+
this.h5 = 0x9b05688c;
|
|
711
|
+
this.h6 = 0x1f83d9ab;
|
|
712
|
+
this.h7 = 0x5be0cd19;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
this.block = this.start = this.bytes = this.hBytes = 0;
|
|
716
|
+
this.finalized = this.hashed = false;
|
|
717
|
+
this.first = true;
|
|
718
|
+
this.is224 = is224;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
Sha256.prototype.update = function (message) {
|
|
722
|
+
if (this.finalized) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
var notString, type = typeof message;
|
|
726
|
+
if (type !== 'string') {
|
|
727
|
+
if (type === 'object') {
|
|
728
|
+
if (message === null) {
|
|
729
|
+
throw new Error(ERROR);
|
|
730
|
+
} else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
|
|
731
|
+
message = new Uint8Array(message);
|
|
732
|
+
} else if (!Array.isArray(message)) {
|
|
733
|
+
if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
|
|
734
|
+
throw new Error(ERROR);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
} else {
|
|
738
|
+
throw new Error(ERROR);
|
|
739
|
+
}
|
|
740
|
+
notString = true;
|
|
741
|
+
}
|
|
742
|
+
var code, index = 0, i, length = message.length, blocks = this.blocks;
|
|
743
|
+
|
|
744
|
+
while (index < length) {
|
|
745
|
+
if (this.hashed) {
|
|
746
|
+
this.hashed = false;
|
|
747
|
+
blocks[0] = this.block;
|
|
748
|
+
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
|
749
|
+
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
|
750
|
+
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
|
751
|
+
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
if (notString) {
|
|
755
|
+
for (i = this.start; index < length && i < 64; ++index) {
|
|
756
|
+
blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
|
|
757
|
+
}
|
|
758
|
+
} else {
|
|
759
|
+
for (i = this.start; index < length && i < 64; ++index) {
|
|
760
|
+
code = message.charCodeAt(index);
|
|
761
|
+
if (code < 0x80) {
|
|
762
|
+
blocks[i >> 2] |= code << SHIFT[i++ & 3];
|
|
763
|
+
} else if (code < 0x800) {
|
|
764
|
+
blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
|
|
765
|
+
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
|
766
|
+
} else if (code < 0xd800 || code >= 0xe000) {
|
|
767
|
+
blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
|
|
768
|
+
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
|
|
769
|
+
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
|
770
|
+
} else {
|
|
771
|
+
code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
|
|
772
|
+
blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
|
|
773
|
+
blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
|
|
774
|
+
blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
|
|
775
|
+
blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
this.lastByteIndex = i;
|
|
781
|
+
this.bytes += i - this.start;
|
|
782
|
+
if (i >= 64) {
|
|
783
|
+
this.block = blocks[16];
|
|
784
|
+
this.start = i - 64;
|
|
785
|
+
this.hash();
|
|
786
|
+
this.hashed = true;
|
|
787
|
+
} else {
|
|
788
|
+
this.start = i;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
if (this.bytes > 4294967295) {
|
|
792
|
+
this.hBytes += this.bytes / 4294967296 << 0;
|
|
793
|
+
this.bytes = this.bytes % 4294967296;
|
|
794
|
+
}
|
|
795
|
+
return this;
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
Sha256.prototype.finalize = function () {
|
|
799
|
+
if (this.finalized) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
this.finalized = true;
|
|
803
|
+
var blocks = this.blocks, i = this.lastByteIndex;
|
|
804
|
+
blocks[16] = this.block;
|
|
805
|
+
blocks[i >> 2] |= EXTRA[i & 3];
|
|
806
|
+
this.block = blocks[16];
|
|
807
|
+
if (i >= 56) {
|
|
808
|
+
if (!this.hashed) {
|
|
809
|
+
this.hash();
|
|
810
|
+
}
|
|
811
|
+
blocks[0] = this.block;
|
|
812
|
+
blocks[16] = blocks[1] = blocks[2] = blocks[3] =
|
|
813
|
+
blocks[4] = blocks[5] = blocks[6] = blocks[7] =
|
|
814
|
+
blocks[8] = blocks[9] = blocks[10] = blocks[11] =
|
|
815
|
+
blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
|
|
816
|
+
}
|
|
817
|
+
blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
|
|
818
|
+
blocks[15] = this.bytes << 3;
|
|
819
|
+
this.hash();
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
Sha256.prototype.hash = function () {
|
|
823
|
+
var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
|
|
824
|
+
h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
|
|
825
|
+
|
|
826
|
+
for (j = 16; j < 64; ++j) {
|
|
827
|
+
// rightrotate
|
|
828
|
+
t1 = blocks[j - 15];
|
|
829
|
+
s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
|
|
830
|
+
t1 = blocks[j - 2];
|
|
831
|
+
s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
|
|
832
|
+
blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
bc = b & c;
|
|
836
|
+
for (j = 0; j < 64; j += 4) {
|
|
837
|
+
if (this.first) {
|
|
838
|
+
if (this.is224) {
|
|
839
|
+
ab = 300032;
|
|
840
|
+
t1 = blocks[0] - 1413257819;
|
|
841
|
+
h = t1 - 150054599 << 0;
|
|
842
|
+
d = t1 + 24177077 << 0;
|
|
843
|
+
} else {
|
|
844
|
+
ab = 704751109;
|
|
845
|
+
t1 = blocks[0] - 210244248;
|
|
846
|
+
h = t1 - 1521486534 << 0;
|
|
847
|
+
d = t1 + 143694565 << 0;
|
|
848
|
+
}
|
|
849
|
+
this.first = false;
|
|
850
|
+
} else {
|
|
851
|
+
s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
852
|
+
s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
853
|
+
ab = a & b;
|
|
854
|
+
maj = ab ^ (a & c) ^ bc;
|
|
855
|
+
ch = (e & f) ^ (~e & g);
|
|
856
|
+
t1 = h + s1 + ch + K[j] + blocks[j];
|
|
857
|
+
t2 = s0 + maj;
|
|
858
|
+
h = d + t1 << 0;
|
|
859
|
+
d = t1 + t2 << 0;
|
|
860
|
+
}
|
|
861
|
+
s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
|
|
862
|
+
s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
|
|
863
|
+
da = d & a;
|
|
864
|
+
maj = da ^ (d & b) ^ ab;
|
|
865
|
+
ch = (h & e) ^ (~h & f);
|
|
866
|
+
t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
|
|
867
|
+
t2 = s0 + maj;
|
|
868
|
+
g = c + t1 << 0;
|
|
869
|
+
c = t1 + t2 << 0;
|
|
870
|
+
s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
|
|
871
|
+
s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
|
|
872
|
+
cd = c & d;
|
|
873
|
+
maj = cd ^ (c & a) ^ da;
|
|
874
|
+
ch = (g & h) ^ (~g & e);
|
|
875
|
+
t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
|
|
876
|
+
t2 = s0 + maj;
|
|
877
|
+
f = b + t1 << 0;
|
|
878
|
+
b = t1 + t2 << 0;
|
|
879
|
+
s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
|
|
880
|
+
s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
|
|
881
|
+
bc = b & c;
|
|
882
|
+
maj = bc ^ (b & d) ^ cd;
|
|
883
|
+
ch = (f & g) ^ (~f & h);
|
|
884
|
+
t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
|
|
885
|
+
t2 = s0 + maj;
|
|
886
|
+
e = a + t1 << 0;
|
|
887
|
+
a = t1 + t2 << 0;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
this.h0 = this.h0 + a << 0;
|
|
891
|
+
this.h1 = this.h1 + b << 0;
|
|
892
|
+
this.h2 = this.h2 + c << 0;
|
|
893
|
+
this.h3 = this.h3 + d << 0;
|
|
894
|
+
this.h4 = this.h4 + e << 0;
|
|
895
|
+
this.h5 = this.h5 + f << 0;
|
|
896
|
+
this.h6 = this.h6 + g << 0;
|
|
897
|
+
this.h7 = this.h7 + h << 0;
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
Sha256.prototype.hex = function () {
|
|
901
|
+
this.finalize();
|
|
902
|
+
|
|
903
|
+
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
|
|
904
|
+
h6 = this.h6, h7 = this.h7;
|
|
905
|
+
|
|
906
|
+
var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
|
|
907
|
+
HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
|
|
908
|
+
HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
|
|
909
|
+
HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
|
|
910
|
+
HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
|
|
911
|
+
HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
|
|
912
|
+
HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
|
|
913
|
+
HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
|
|
914
|
+
HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
|
|
915
|
+
HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
|
|
916
|
+
HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
|
|
917
|
+
HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
|
|
918
|
+
HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
|
|
919
|
+
HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
|
|
920
|
+
HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
|
|
921
|
+
HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
|
|
922
|
+
HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
|
|
923
|
+
HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
|
|
924
|
+
HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
|
|
925
|
+
HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
|
|
926
|
+
HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
|
|
927
|
+
HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
|
|
928
|
+
HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
|
|
929
|
+
HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
|
|
930
|
+
HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
|
|
931
|
+
HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
|
|
932
|
+
HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
|
|
933
|
+
HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
|
|
934
|
+
if (!this.is224) {
|
|
935
|
+
hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
|
|
936
|
+
HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
|
|
937
|
+
HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
|
|
938
|
+
HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
|
|
939
|
+
}
|
|
940
|
+
return hex;
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
Sha256.prototype.toString = Sha256.prototype.hex;
|
|
944
|
+
|
|
945
|
+
Sha256.prototype.digest = function () {
|
|
946
|
+
this.finalize();
|
|
947
|
+
|
|
948
|
+
var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
|
|
949
|
+
h6 = this.h6, h7 = this.h7;
|
|
950
|
+
|
|
951
|
+
var arr = [
|
|
952
|
+
(h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
|
|
953
|
+
(h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
|
|
954
|
+
(h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
|
|
955
|
+
(h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
|
|
956
|
+
(h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
|
|
957
|
+
(h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
|
|
958
|
+
(h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
|
|
959
|
+
];
|
|
960
|
+
if (!this.is224) {
|
|
961
|
+
arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
|
|
962
|
+
}
|
|
963
|
+
return arr;
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
Sha256.prototype.array = Sha256.prototype.digest;
|
|
967
|
+
|
|
968
|
+
Sha256.prototype.arrayBuffer = function () {
|
|
969
|
+
this.finalize();
|
|
970
|
+
|
|
971
|
+
var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
|
|
972
|
+
var dataView = new DataView(buffer);
|
|
973
|
+
dataView.setUint32(0, this.h0);
|
|
974
|
+
dataView.setUint32(4, this.h1);
|
|
975
|
+
dataView.setUint32(8, this.h2);
|
|
976
|
+
dataView.setUint32(12, this.h3);
|
|
977
|
+
dataView.setUint32(16, this.h4);
|
|
978
|
+
dataView.setUint32(20, this.h5);
|
|
979
|
+
dataView.setUint32(24, this.h6);
|
|
980
|
+
if (!this.is224) {
|
|
981
|
+
dataView.setUint32(28, this.h7);
|
|
982
|
+
}
|
|
983
|
+
return buffer;
|
|
984
|
+
};
|
|
985
|
+
|
|
986
|
+
function HmacSha256(key, is224, sharedMemory) {
|
|
987
|
+
var i, type = typeof key;
|
|
988
|
+
if (type === 'string') {
|
|
989
|
+
var bytes = [], length = key.length, index = 0, code;
|
|
990
|
+
for (i = 0; i < length; ++i) {
|
|
991
|
+
code = key.charCodeAt(i);
|
|
992
|
+
if (code < 0x80) {
|
|
993
|
+
bytes[index++] = code;
|
|
994
|
+
} else if (code < 0x800) {
|
|
995
|
+
bytes[index++] = (0xc0 | (code >> 6));
|
|
996
|
+
bytes[index++] = (0x80 | (code & 0x3f));
|
|
997
|
+
} else if (code < 0xd800 || code >= 0xe000) {
|
|
998
|
+
bytes[index++] = (0xe0 | (code >> 12));
|
|
999
|
+
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
|
|
1000
|
+
bytes[index++] = (0x80 | (code & 0x3f));
|
|
1001
|
+
} else {
|
|
1002
|
+
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
|
|
1003
|
+
bytes[index++] = (0xf0 | (code >> 18));
|
|
1004
|
+
bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
|
|
1005
|
+
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
|
|
1006
|
+
bytes[index++] = (0x80 | (code & 0x3f));
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
key = bytes;
|
|
1010
|
+
} else {
|
|
1011
|
+
if (type === 'object') {
|
|
1012
|
+
if (key === null) {
|
|
1013
|
+
throw new Error(ERROR);
|
|
1014
|
+
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
|
|
1015
|
+
key = new Uint8Array(key);
|
|
1016
|
+
} else if (!Array.isArray(key)) {
|
|
1017
|
+
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
|
|
1018
|
+
throw new Error(ERROR);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
} else {
|
|
1022
|
+
throw new Error(ERROR);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
if (key.length > 64) {
|
|
1027
|
+
key = (new Sha256(is224, true)).update(key).array();
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
var oKeyPad = [], iKeyPad = [];
|
|
1031
|
+
for (i = 0; i < 64; ++i) {
|
|
1032
|
+
var b = key[i] || 0;
|
|
1033
|
+
oKeyPad[i] = 0x5c ^ b;
|
|
1034
|
+
iKeyPad[i] = 0x36 ^ b;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
Sha256.call(this, is224, sharedMemory);
|
|
1038
|
+
|
|
1039
|
+
this.update(iKeyPad);
|
|
1040
|
+
this.oKeyPad = oKeyPad;
|
|
1041
|
+
this.inner = true;
|
|
1042
|
+
this.sharedMemory = sharedMemory;
|
|
1043
|
+
}
|
|
1044
|
+
HmacSha256.prototype = new Sha256();
|
|
1045
|
+
|
|
1046
|
+
HmacSha256.prototype.finalize = function () {
|
|
1047
|
+
Sha256.prototype.finalize.call(this);
|
|
1048
|
+
if (this.inner) {
|
|
1049
|
+
this.inner = false;
|
|
1050
|
+
var innerHash = this.array();
|
|
1051
|
+
Sha256.call(this, this.is224, this.sharedMemory);
|
|
1052
|
+
this.update(this.oKeyPad);
|
|
1053
|
+
this.update(innerHash);
|
|
1054
|
+
Sha256.prototype.finalize.call(this);
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1058
|
+
var exports = createMethod();
|
|
1059
|
+
exports.sha256 = exports;
|
|
1060
|
+
exports.sha224 = createMethod(true);
|
|
1061
|
+
exports.sha256.hmac = createHmacMethod();
|
|
1062
|
+
exports.sha224.hmac = createHmacMethod(true);
|
|
1063
|
+
|
|
1064
|
+
if (COMMON_JS) {
|
|
1065
|
+
module.exports = exports;
|
|
1066
|
+
} else {
|
|
1067
|
+
root.sha256 = exports.sha256;
|
|
1068
|
+
root.sha224 = exports.sha224;
|
|
1069
|
+
if (AMD) {
|
|
1070
|
+
define(function () {
|
|
1071
|
+
return exports;
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
})();
|
|
1076
|
+
return module.exports;
|
|
1077
|
+
})();
|
|
1078
|
+
/*
|
|
1079
|
+
* [genanki]{@link https://github.com/kerrickstaley/genanki}
|
|
1080
|
+
* @copyright Copyright (c) Kerrick Staley 2021
|
|
1081
|
+
* @license The MIT License
|
|
1082
|
+
*/
|
|
1083
|
+
|
|
1084
|
+
const APKG_SCHEMA = `
|
|
1085
|
+
PRAGMA foreign_keys=OFF;
|
|
1086
|
+
BEGIN TRANSACTION;
|
|
1087
|
+
|
|
1088
|
+
CREATE TABLE col (
|
|
1089
|
+
id integer primary key,
|
|
1090
|
+
crt integer not null,
|
|
1091
|
+
mod integer not null,
|
|
1092
|
+
scm integer not null,
|
|
1093
|
+
ver integer not null,
|
|
1094
|
+
dty integer not null,
|
|
1095
|
+
usn integer not null,
|
|
1096
|
+
ls integer not null,
|
|
1097
|
+
conf text not null,
|
|
1098
|
+
models text not null,
|
|
1099
|
+
decks text not null,
|
|
1100
|
+
dconf text not null,
|
|
1101
|
+
tags text not null
|
|
1102
|
+
);
|
|
1103
|
+
CREATE TABLE notes (
|
|
1104
|
+
id integer primary key, /* 0 */
|
|
1105
|
+
guid text not null, /* 1 */
|
|
1106
|
+
mid integer not null, /* 2 */
|
|
1107
|
+
mod integer not null, /* 3 */
|
|
1108
|
+
usn integer not null, /* 4 */
|
|
1109
|
+
tags text not null, /* 5 */
|
|
1110
|
+
flds text not null, /* 6 */
|
|
1111
|
+
sfld integer not null, /* 7 */
|
|
1112
|
+
csum integer not null, /* 8 */
|
|
1113
|
+
flags integer not null, /* 9 */
|
|
1114
|
+
data text not null /* 10 */
|
|
1115
|
+
);
|
|
1116
|
+
CREATE TABLE cards (
|
|
1117
|
+
id integer primary key, /* 0 */
|
|
1118
|
+
nid integer not null, /* 1 */
|
|
1119
|
+
did integer not null, /* 2 */
|
|
1120
|
+
ord integer not null, /* 3 */
|
|
1121
|
+
mod integer not null, /* 4 */
|
|
1122
|
+
usn integer not null, /* 5 */
|
|
1123
|
+
type integer not null, /* 6 */
|
|
1124
|
+
queue integer not null, /* 7 */
|
|
1125
|
+
due integer not null, /* 8 */
|
|
1126
|
+
ivl integer not null, /* 9 */
|
|
1127
|
+
factor integer not null, /* 10 */
|
|
1128
|
+
reps integer not null, /* 11 */
|
|
1129
|
+
lapses integer not null, /* 12 */
|
|
1130
|
+
left integer not null, /* 13 */
|
|
1131
|
+
odue integer not null, /* 14 */
|
|
1132
|
+
odid integer not null, /* 15 */
|
|
1133
|
+
flags integer not null, /* 16 */
|
|
1134
|
+
data text not null /* 17 */
|
|
1135
|
+
);
|
|
1136
|
+
CREATE TABLE revlog (
|
|
1137
|
+
id integer primary key,
|
|
1138
|
+
cid integer not null,
|
|
1139
|
+
usn integer not null,
|
|
1140
|
+
ease integer not null,
|
|
1141
|
+
ivl integer not null,
|
|
1142
|
+
lastIvl integer not null,
|
|
1143
|
+
factor integer not null,
|
|
1144
|
+
time integer not null,
|
|
1145
|
+
type integer not null
|
|
1146
|
+
);
|
|
1147
|
+
CREATE TABLE graves (
|
|
1148
|
+
usn integer not null,
|
|
1149
|
+
oid integer not null,
|
|
1150
|
+
type integer not null
|
|
1151
|
+
);
|
|
1152
|
+
CREATE INDEX ix_notes_usn on notes (usn);
|
|
1153
|
+
CREATE INDEX ix_cards_usn on cards (usn);
|
|
1154
|
+
CREATE INDEX ix_revlog_usn on revlog (usn);
|
|
1155
|
+
CREATE INDEX ix_cards_nid on cards (nid);
|
|
1156
|
+
CREATE INDEX ix_cards_sched on cards (did, queue, due);
|
|
1157
|
+
CREATE INDEX ix_revlog_cid on revlog (cid);
|
|
1158
|
+
CREATE INDEX ix_notes_csum on notes (csum);
|
|
1159
|
+
COMMIT;
|
|
1160
|
+
`;
|
|
1161
|
+
var APKG_COL = `
|
|
1162
|
+
INSERT INTO col VALUES(
|
|
1163
|
+
null,
|
|
1164
|
+
1411124400,
|
|
1165
|
+
1425279151694,
|
|
1166
|
+
1425279151690,
|
|
1167
|
+
11,
|
|
1168
|
+
0,
|
|
1169
|
+
0,
|
|
1170
|
+
0,
|
|
1171
|
+
'{
|
|
1172
|
+
"activeDecks": [
|
|
1173
|
+
1
|
|
1174
|
+
],
|
|
1175
|
+
"addToCur": true,
|
|
1176
|
+
"collapseTime": 1200,
|
|
1177
|
+
"curDeck": 1,
|
|
1178
|
+
"curModel": "1425279151691",
|
|
1179
|
+
"dueCounts": true,
|
|
1180
|
+
"estTimes": true,
|
|
1181
|
+
"newBury": true,
|
|
1182
|
+
"newSpread": 0,
|
|
1183
|
+
"nextPos": 1,
|
|
1184
|
+
"sortBackwards": false,
|
|
1185
|
+
"sortType": "noteFld",
|
|
1186
|
+
"timeLim": 0
|
|
1187
|
+
}',
|
|
1188
|
+
'{}',
|
|
1189
|
+
'{
|
|
1190
|
+
"1": {
|
|
1191
|
+
"collapsed": false,
|
|
1192
|
+
"conf": 1,
|
|
1193
|
+
"desc": "",
|
|
1194
|
+
"dyn": 0,
|
|
1195
|
+
"extendNew": 10,
|
|
1196
|
+
"extendRev": 50,
|
|
1197
|
+
"id": 1,
|
|
1198
|
+
"lrnToday": [
|
|
1199
|
+
0,
|
|
1200
|
+
0
|
|
1201
|
+
],
|
|
1202
|
+
"mod": 1425279151,
|
|
1203
|
+
"name": "Default",
|
|
1204
|
+
"newToday": [
|
|
1205
|
+
0,
|
|
1206
|
+
0
|
|
1207
|
+
],
|
|
1208
|
+
"revToday": [
|
|
1209
|
+
0,
|
|
1210
|
+
0
|
|
1211
|
+
],
|
|
1212
|
+
"timeToday": [
|
|
1213
|
+
0,
|
|
1214
|
+
0
|
|
1215
|
+
],
|
|
1216
|
+
"usn": 0
|
|
1217
|
+
}
|
|
1218
|
+
}',
|
|
1219
|
+
'{
|
|
1220
|
+
"1": {
|
|
1221
|
+
"autoplay": true,
|
|
1222
|
+
"id": 1,
|
|
1223
|
+
"lapse": {
|
|
1224
|
+
"delays": [
|
|
1225
|
+
10
|
|
1226
|
+
],
|
|
1227
|
+
"leechAction": 0,
|
|
1228
|
+
"leechFails": 8,
|
|
1229
|
+
"minInt": 1,
|
|
1230
|
+
"mult": 0
|
|
1231
|
+
},
|
|
1232
|
+
"maxTaken": 60,
|
|
1233
|
+
"mod": 0,
|
|
1234
|
+
"name": "Default",
|
|
1235
|
+
"new": {
|
|
1236
|
+
"bury": true,
|
|
1237
|
+
"delays": [
|
|
1238
|
+
1,
|
|
1239
|
+
10
|
|
1240
|
+
],
|
|
1241
|
+
"initialFactor": 2500,
|
|
1242
|
+
"ints": [
|
|
1243
|
+
1,
|
|
1244
|
+
4,
|
|
1245
|
+
7
|
|
1246
|
+
],
|
|
1247
|
+
"order": 1,
|
|
1248
|
+
"perDay": 20,
|
|
1249
|
+
"separate": true
|
|
1250
|
+
},
|
|
1251
|
+
"replayq": true,
|
|
1252
|
+
"rev": {
|
|
1253
|
+
"bury": true,
|
|
1254
|
+
"ease4": 1.3,
|
|
1255
|
+
"fuzz": 0.05,
|
|
1256
|
+
"ivlFct": 1,
|
|
1257
|
+
"maxIvl": 36500,
|
|
1258
|
+
"minSpace": 1,
|
|
1259
|
+
"perDay": 100
|
|
1260
|
+
},
|
|
1261
|
+
"timer": 0,
|
|
1262
|
+
"usn": 0
|
|
1263
|
+
}
|
|
1264
|
+
}',
|
|
1265
|
+
'{}'
|
|
1266
|
+
);
|
|
1267
|
+
`;
|
|
1268
|
+
|
|
1269
|
+
// namespace for the <script> build, e.g. new genanki.Model({...})
|
|
1270
|
+
var genanki = { Model, ClozeModel, Deck, Note, Package, setSqlJs, ankiHash, sha256, MODEL_STD, MODEL_CLOZE }
|