-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpdfcrypt.js
91 lines (84 loc) · 2.85 KB
/
cpdfcrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
//Provides: camlpdf_caml_aes_cook_encrypt_key
//Requires: camlpdf_caml_aes_cook_decrypt_key
function camlpdf_caml_aes_cook_encrypt_key(s)
{
//console.log('camlpdf_caml_aes_cook_encrypt_key');
return camlpdf_caml_aes_cook_decrypt_key(s);
}
//The aes cipher. We set it up in camlpdf_caml_aes_cook_decrypt_key, but store
//it here. The "cooked key" handed back to OCaml is junk and will be ignored
//when sent to camlpdf_caml_aes_decrypt.
var cpdf_aes;
//Provides: camlpdf_caml_aes_cook_decrypt_key
//Requires: caml_array_of_bytes
//Requires: caml_bytes_of_array
function camlpdf_caml_aes_cook_decrypt_key(s)
{
//console.log('camlpdf_caml_aes_cook_decrypt_key');
var s2 = caml_array_of_bytes(s);
var key = cpdf_sjcl.codec.bytes.toBits(s2);
cpdf_aes = new cpdf_sjcl.cipher.aes(key);
var ba2 = Uint8Array.from(s2);
return caml_bytes_of_array(ba2);
}
//Provides: camlpdf_caml_aes_encrypt
//Requires: caml_array_of_bytes
function camlpdf_caml_aes_encrypt(ckey, src, src_ofs, dst, dst_ofs)
{
//console.log('camlpdf_caml_aes_encrypt');
var srcbytes = caml_array_of_bytes(src);
var srcbits = cpdf_sjcl.codec.bytes.toBits(srcbytes);
var encrypted = cpdf_aes.encrypt(srcbits);
dst.t = 4;
dst.c = cpdf_sjcl.codec.bytes.fromBits(encrypted);
dst.l = src.l;
}
//Provides: camlpdf_caml_aes_decrypt
//Requires: caml_array_of_bytes
//ckey is irrelevant here, since stored in global cpdf_aes above
function camlpdf_caml_aes_decrypt(ckey, src, src_ofs, dst, dst_ofs)
{
//console.log('camlpdf_caml_aes_decrypt');
var srcbytes = caml_array_of_bytes(src);
var srcbits = cpdf_sjcl.codec.bytes.toBits(srcbytes);
var plaintext = cpdf_aes.decrypt(srcbits);
dst.t = 4;
dst.c = cpdf_sjcl.codec.bytes.fromBits(plaintext);
dst.l = src.l;
}
//Provides: camlpdf_caml_sha256
//Requires: caml_bytes_of_array
//Requires: caml_array_of_bytes
function camlpdf_caml_sha256(src)
{
//console.log('camlpdf_caml_sha256');
var hash = cpdf_crypto.createHash('sha256').update(caml_array_of_bytes(src));
var hashedbytes = hash.digest().buffer;
var arr = new Uint8Array(hashedbytes);
var r = caml_bytes_of_array(arr);
return r;
}
//Provides: camlpdf_caml_sha384
//Requires: caml_bytes_of_array
//Requires: caml_array_of_bytes
function camlpdf_caml_sha384(src)
{
//console.log('camlpdf_caml_sha384');
var hash = cpdf_crypto.createHash('sha384').update(caml_array_of_bytes(src));
var hashedbytes = hash.digest().buffer;
var arr = new Uint8Array(hashedbytes);
var r = caml_bytes_of_array(arr);
return r;
}
//Provides: camlpdf_caml_sha512
//Requires: caml_bytes_of_array
//Requires: caml_array_of_bytes
function camlpdf_caml_sha512(src)
{
//console.log('camlpdf_caml_sha512');
var hash = cpdf_crypto.createHash('sha512').update(caml_array_of_bytes(src));
var hashedbytes = hash.digest().buffer;
var arr = new Uint8Array(hashedbytes);
var r = caml_bytes_of_array(arr);
return r;
}