-
Notifications
You must be signed in to change notification settings - Fork 4
/
crypto_module.js
178 lines (146 loc) · 5.11 KB
/
crypto_module.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
92
93
94
95
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* tslint:disable */
import * as wasm from './crypto_module_bg';
let cachegetUint8Memory = null;
function getUint8Memory() {
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory;
}
function passArray8ToWasm(arg) {
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
getUint8Memory().set(arg, ptr / 1);
return [ptr, arg.length];
}
const TextDecoder = typeof self === 'object' && self.TextDecoder
? self.TextDecoder
: require('util').TextDecoder;
let cachedDecoder = new TextDecoder('utf-8');
function getStringFromWasm(ptr, len) {
return cachedDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}
return cachedGlobalArgumentPtr;
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
const TextEncoder = typeof self === 'object' && self.TextEncoder
? self.TextEncoder
: require('util').TextEncoder;
let cachedEncoder = new TextEncoder('utf-8');
function passStringToWasm(arg) {
const buf = cachedEncoder.encode(arg);
const ptr = wasm.__wbindgen_malloc(buf.length);
getUint8Memory().set(buf, ptr);
return [ptr, buf.length];
}
/**
* given a public key (e, n), encrypts message m for this public key using RSA.
* @param {string} arg0
* @param {string} arg1
* @returns {string}
*/
export function encrypt(arg0, arg1) {
const [ptr0, len0] = passStringToWasm(arg0);
const [ptr1, len1] = passStringToWasm(arg1);
const retptr = globalArgumentPtr();
try {
wasm.encrypt(retptr, ptr0, len0, ptr1, len1);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getStringFromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
}
}
/**
* stores the information for a given RSA keypair.
*/
export class Keypair {
static __construct(ptr) {
return new Keypair(ptr);
}
constructor(ptr) {
this.ptr = ptr;
}
free() {
const ptr = this.ptr;
this.ptr = 0;
wasm.__wbg_keypair_free(ptr);
}
/**
* randomly generates a new keypair based on two seeds.
* @param {Uint8Array} arg0
* @param {Uint8Array} arg1
* @returns {Keypair}
*/
static new(arg0, arg1) {
const [ptr0, len0] = passArray8ToWasm(arg0);
const [ptr1, len1] = passArray8ToWasm(arg1);
try {
return Keypair.__construct(wasm.keypair_new(ptr0, len0, ptr1, len1));
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
wasm.__wbindgen_free(ptr1, len1 * 1);
}
}
/**
* nicely outputs a formatted public key for use in the javascript code.
* improved since 0.2.0. Now outputs just n as a radix 32 string similar
* to how it is done here: http://gauss.ececs.uc.edu/Courses/c653/project/radix_32.html
* @returns {string}
*/
public_key_display_wasm() {
if (this.ptr === 0) {
throw new Error('Attempt to use a moved value');
}
const retptr = globalArgumentPtr();
wasm.keypair_public_key_display_wasm(retptr, this.ptr);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getStringFromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
}
/**
* given a ciphertext, attempts to decrypt based on the private key and modulo from this keypair. Performs
* simple decryption based on RSA algorithm.
* @param {string} arg0
* @returns {string}
*/
decrypt(arg0) {
if (this.ptr === 0) {
throw new Error('Attempt to use a moved value');
}
const [ptr0, len0] = passStringToWasm(arg0);
const retptr = globalArgumentPtr();
try {
wasm.keypair_decrypt(retptr, this.ptr, ptr0, len0);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getStringFromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
} finally {
wasm.__wbindgen_free(ptr0, len0 * 1);
}
}
}
export function __wbindgen_throw(ptr, len) {
throw new Error(getStringFromWasm(ptr, len));
}