-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasta.i
254 lines (211 loc) · 6.3 KB
/
pasta.i
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
178
179
180
181
182
183
184
185
186
187
188
189
190
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
249
250
251
252
253
254
%module(package="libpasta") pasta
%include "std_string.i"
#if defined(SWIGJAVA)
%include "enums.swg"
#endif
%ignore HashUpdate::HashUpdate;
%ignore PrimitiveWrapper::inner;
//
%{
namespace ffi {
#include "pasta.h"
}
using namespace ffi;
class HashUpdate {
public:
enum Tag {
Updated,
Ok,
Failed,
} tag;
char *updated = NULL;
HashUpdate(HashUpdateFfi *other) {
switch(other->tag) {
case HashUpdateFfi::Tag::Updated:
tag = HashUpdate::Updated;
updated = other->updated._0; break;
case HashUpdateFfi::Tag::Ok: tag = HashUpdate::Ok; break;
case HashUpdateFfi::Tag::Failed: tag = HashUpdate::Failed; break;
}
}
~HashUpdate() {
free_string(updated);
updated = NULL;
}
};
class PrimitiveWrapper {
public:
Primitive *self;
virtual Primitive *inner() =0;
virtual ~PrimitiveWrapper()=0;
};
PrimitiveWrapper::~PrimitiveWrapper() {}
class Argon2i: public PrimitiveWrapper {
public:
Argon2i() {
self = default_argon2i();
};
Argon2i(int passes, int lanes, int kib) {
self = new_argon2i(passes, lanes, kib);
};
~Argon2i() {
free_Primitive(self);
self = NULL;
}
protected:
Primitive *inner() {
return self;
};
};
class Bcrypt: public PrimitiveWrapper {
public:
Bcrypt() {
self = default_bcrypt();
};
Bcrypt(int cost) {
self = new_bcrypt(cost);
};
~Bcrypt() {
free_Primitive(self);
self = NULL;
}
protected:
Primitive *inner() {
return self;
};
};
class Scrypt: public PrimitiveWrapper {
public:
Scrypt() {
self = default_scrypt();
};
Scrypt(unsigned char log_n, unsigned int r, unsigned int p) {
self = new_scrypt(log_n, r, p);
};
~Scrypt() {
free_Primitive(self);
self = NULL;
}
protected:
Primitive *inner() {
return self;
};
};
namespace libpasta {
HashUpdate *migrate_hash(const char *hash) {
return new HashUpdate(ffi::migrate_hash(hash));
}
HashUpdate *verify_password_update_hash(const char *hash, const char *password) {
return new HashUpdate(ffi::verify_password_update_hash(hash, password));
}
}
%}
// For functions returning `char *`, allocate a new String and immediately
// call `free_string` on the pointer to clean up from Rust.
%typemap(newfree) char * "free_string($1);";
%newobject hash_password;
%newobject read_password;
%pragma(java) jniclassimports=%{
import org.scijava.nativelib.*;
%}
%pragma(java) jniclasscode=%{
static {
try {
NativeLoader.loadLibrary("pasta_jni");
} catch (Exception e) {
try {
NativeLibraryUtil.loadNativeLibrary(pastaJNI.class, "pasta_jni");
} catch (Exception e2) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
}
%}
%nodefaultctor;
%nodefaultdtor;
class PrimitiveWrapper {
public:
virtual Primitive *inner() =0;
};
class Argon2i: public PrimitiveWrapper {
public:
Argon2i();
Argon2i(int passes, int lanes, int kib);
~Argon2i();
protected:
Primitive *inner();
};
class Bcrypt: public PrimitiveWrapper {
public:
Bcrypt();
Bcrypt(int cost);
~Bcrypt();
protected:
Primitive *inner();
};
class Scrypt: public PrimitiveWrapper {
public:
Scrypt();
Scrypt(unsigned char log_n, unsigned int r, unsigned int p);
~Scrypt();
protected:
Primitive *inner();
};
class HashUpdate {
public:
enum Tag {
Updated,
Ok,
Failed,
} tag;
char *updated = NULL;
HashUpdate(HashUpdateFfi *other);
~HashUpdate();
};
typedef struct Config {
%extend {
static Config *with_primitive(PrimitiveWrapper *p) {
return config_with_primitive(p->inner());
}
char *hash_password(const char *password) {
return config_hash_password(self, password);
}
HashUpdate *migrate_hash(const char *hash) {
return new HashUpdate(config_migrate_hash(self, hash));
}
bool verify_password(const char *hash, const char *password) {
return config_verify_password(self, hash, password);
}
HashUpdate *verify_password_update_hash(const char *hash, const char *password) {
return new HashUpdate(config_verify_password_update_hash(self, hash, password));
}
}
} Config;
// We intentionally only pull in a subset of the exported functions
// %include "libpasta/libpasta-capi/include/pasta-bindings.h"
// Holds possible configuration options
// See the [module level documentation](index.html) for more information.
struct Config;
// Password hashing primitives
//
// Each variant is backed up by different implementation.
// Internally, primitives can either be static values, for example,
// the `lazy_static` generated value `DEFAULT_PRIM`, or dynamically allocated
// variables, which are `Arc<Box<...>>`.
//
// Most operations are expected to be performed using the static functions,
// since most use the default algorithms. However, the flexibilty to support
// arbitrary parameter sets is essential.
struct Primitive;
namespace libpasta {
%newobject migrate_hash;
%newobject verify_password_update_hash;
HashUpdate *migrate_hash(const char *hash);
HashUpdate *verify_password_update_hash(const char *hash, const char *password);
}
extern "C" {
char *hash_password(const char *password);
char *read_password(const char *prompt);
bool verify_password(const char *hash, const char *password);
}