enquo-core 0.7.0.2.gb37f667-arm64-darwin → 0.8.0-arm64-darwin

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.
data/ext/enquo/src/lib.rs CHANGED
@@ -1,320 +1,40 @@
1
- #[macro_use]
2
- extern crate rutie;
1
+ mod field;
2
+ mod root;
3
+ mod root_key;
3
4
 
4
- use enquo_core::{key_provider, Boolean, Date, Field, Root, Text, I64};
5
- use rutie::{
6
- AnyObject, Boolean as RBoolean, Class, Integer, Module, Object, RString, Symbol,
7
- VerifiedObject, VM,
8
- };
5
+ #[magnus::init]
6
+ fn init() -> Result<(), magnus::Error> {
7
+ let base_mod = magnus::define_module("Enquo")?;
9
8
 
10
- class!(EnquoRoot);
11
- class!(EnquoRootKeyStatic);
12
- class!(EnquoField);
9
+ root::init(&base_mod)?;
10
+ root_key::init(&base_mod)?;
11
+ field::init(&base_mod)?;
13
12
 
14
- wrappable_struct!(Root<'static>, RootWrapper, ROOT_WRAPPER);
15
- wrappable_struct!(
16
- key_provider::Static,
17
- StaticRootKeyWrapper,
18
- STATIC_ROOT_KEY_WRAPPER
19
- );
20
- wrappable_struct!(Field, FieldWrapper, FIELD_WRAPPER);
13
+ Ok(())
14
+ }
21
15
 
22
- fn maybe_raise<T, E: std::error::Error>(r: Result<T, E>, s: &str) -> T {
23
- r.map_err(|e| {
24
- VM::raise(
25
- Class::from_existing("Enquo").get_nested_class("Error"),
26
- &format!("{s}: {e}"),
16
+ fn enquo_exception() -> Result<magnus::ExceptionClass, magnus::Error> {
17
+ magnus::ExceptionClass::from_value(magnus::eval("::Enquo::Error")?).ok_or_else(|| {
18
+ magnus::Error::new(
19
+ magnus::exception::runtime_error(),
20
+ "failed to get RClass from Enquo::Error value".to_string(),
27
21
  )
28
22
  })
29
- .unwrap()
30
- }
31
-
32
- unsafe_methods!(
33
- EnquoRoot,
34
- _rbself,
35
- fn enquo_root_new_from_static_root_key(root_key_obj: EnquoRootKeyStatic) -> EnquoRoot {
36
- let rk = root_key_obj.get_data(&*STATIC_ROOT_KEY_WRAPPER);
37
- let root = maybe_raise(Root::new(rk), "Failed to create Enquo::Root");
38
-
39
- let klass = Module::from_existing("Enquo").get_nested_class("Root");
40
- klass.wrap_data(root, &*ROOT_WRAPPER)
41
- },
42
- fn enquo_root_field(relation_obj: RString, name_obj: RString) -> EnquoField {
43
- let relation = relation_obj.to_vec_u8_unchecked();
44
- let name = name_obj.to_vec_u8_unchecked();
45
-
46
- let root = _rbself.get_data(&*ROOT_WRAPPER);
47
-
48
- let field = maybe_raise(
49
- root.field(&relation, &name),
50
- "Failed to create Enquo::Field",
51
- );
52
-
53
- let klass = Module::from_existing("Enquo").get_nested_class("Field");
54
- klass.wrap_data(field, &*FIELD_WRAPPER)
55
- }
56
- );
57
-
58
- unsafe_methods!(
59
- EnquoRootKeyStatic,
60
- _rbself,
61
- fn enquo_root_key_static_new(root_key_obj: RString) -> EnquoRootKeyStatic {
62
- let root_key = root_key_obj.to_vec_u8_unchecked();
63
-
64
- let k = key_provider::Static::new(&root_key);
65
- let klass = Module::from_existing("Enquo")
66
- .get_nested_class("RootKey")
67
- .get_nested_class("Static");
68
- klass.wrap_data(k, &*STATIC_ROOT_KEY_WRAPPER)
69
- },
70
- );
71
-
72
- impl VerifiedObject for EnquoRootKeyStatic {
73
- fn is_correct_type<T: Object>(object: &T) -> bool {
74
- let klass = Module::from_existing("Enquo")
75
- .get_nested_class("RootKey")
76
- .get_nested_class("Static");
77
- klass.case_equals(object)
78
- }
79
-
80
- fn error_message() -> &'static str {
81
- "Provided object is not an Enquo::RootKey::Static instance"
82
- }
83
23
  }
84
24
 
85
- // rustfmt fucks this so it doesn't compile
86
- #[rustfmt::skip]
87
- unsafe_methods!(
88
- EnquoField,
89
- rbself,
90
- fn enquo_field_encrypt_boolean(b_obj: RBoolean, context_obj: RString, mode_obj: Symbol) -> RString {
91
- let b = b_obj.to_bool();
92
- let context = context_obj.to_vec_u8_unchecked();
93
- let mode = mode_obj.to_str();
94
-
95
- let field = rbself.get_data(&*FIELD_WRAPPER);
96
-
97
- let mut res = maybe_raise(
98
- if mode == "unsafe" {
99
- Boolean::new_with_unsafe_parts(b, &context, field)
100
- } else {
101
- Boolean::new(b, &context, field)
102
- },
103
- "Failed to create encrypted boolean",
104
- );
105
- if mode == "no_query" {
106
- res.make_unqueryable();
107
- }
108
-
109
- RString::new_utf8(&maybe_raise(serde_json::to_string(&res), "Failed to JSONify ciphertext"))
110
- },
111
- fn enquo_field_decrypt_boolean(ciphertext_obj: RString, context_obj: RString) -> RBoolean {
112
- let ct = ciphertext_obj.to_str_unchecked();
113
- let context = context_obj.to_vec_u8_unchecked();
114
-
115
- let field = rbself.get_data(&*FIELD_WRAPPER);
116
-
117
- let e_value: Boolean =
118
- maybe_raise(serde_json::from_str(ct), "Failed to deserialize ciphertext");
119
-
120
- let value = maybe_raise(
121
- e_value.decrypt(&context, field),
122
- "Failed to decrypt boolean value",
123
- );
124
- RBoolean::new(value)
125
- },
126
- fn enquo_field_encrypt_i64(i_obj: Integer, context_obj: RString, mode_obj: Symbol) -> RString {
127
- let i = i_obj.to_i64();
128
- let context = context_obj.to_vec_u8_unchecked();
129
- let mode = mode_obj.to_str();
130
-
131
- let field = rbself.get_data(&*FIELD_WRAPPER);
132
-
133
- let mut res = maybe_raise(
134
- if mode == "unsafe" {
135
- I64::new_with_unsafe_parts(i, &context, field)
136
- } else {
137
- I64::new(i, &context, field)
138
- },
139
- "Failed to create encrypted i64",
140
- );
141
- if mode == "no_query" {
142
- res.make_unqueryable();
143
- }
144
-
145
- RString::new_utf8(&maybe_raise(serde_json::to_string(&res), "Failed to JSONify ciphertext"))
146
- },
147
- fn enquo_field_decrypt_i64(ciphertext_obj: RString, context_obj: RString) -> Integer {
148
- let ct = ciphertext_obj.to_str_unchecked();
149
- let context = context_obj.to_vec_u8_unchecked();
25
+ fn maybe_raise<T, E: std::error::Error>(
26
+ r: Result<T, E>,
27
+ s: Option<&str>,
28
+ ) -> Result<T, magnus::Error> {
29
+ let ex_class = enquo_exception()?;
150
30
 
151
- let field = rbself.get_data(&*FIELD_WRAPPER);
152
-
153
- let e_value: I64 =
154
- maybe_raise(serde_json::from_str(ct), "Failed to deserialize ciphertext");
155
-
156
-
157
- let value = maybe_raise(
158
- e_value.decrypt(&context, field),
159
- "Failed to decrypt i64 value",
160
- );
161
- Integer::from(value)
162
- },
163
- fn enquo_field_encrypt_date(
164
- y_obj: Integer,
165
- m_obj: Integer,
166
- d_obj: Integer,
167
- context_obj: RString,
168
- mode_obj: Symbol
169
- ) -> RString {
170
- let y = y_obj.to_i32() as i16;
171
- let m = m_obj.to_i32() as u8;
172
- let d = d_obj.to_i32() as u8;
173
- let context = context_obj.to_vec_u8_unchecked();
174
- let mode = mode_obj.to_str();
175
-
176
- let field = rbself.get_data(&*FIELD_WRAPPER);
177
-
178
- let mut res = maybe_raise(
179
- if mode == "unsafe" {
180
- Date::new_with_unsafe_parts(
181
- (y, m, d),
182
- &context,
183
- field,
184
- )
185
- } else {
186
- Date::new((y, m, d), &context, field)
187
- },
188
- "Failed to create encrypted date",
189
- );
190
- if mode == "no_query" {
191
- res.make_unqueryable();
192
- }
193
-
194
- RString::new_utf8(&maybe_raise(serde_json::to_string(&res), "Failed to JSONify ciphertext"))
195
- },
196
- fn enquo_field_decrypt_date(ciphertext_obj: RString, context_obj: RString) -> AnyObject {
197
- let ct = ciphertext_obj.to_str_unchecked();
198
- let context = context_obj.to_vec_u8_unchecked();
199
-
200
- let field = rbself.get_data(&*FIELD_WRAPPER);
201
-
202
- let e_value: Date =
203
- maybe_raise(serde_json::from_str(ct), "Failed to deserialize ciphertext");
204
-
205
- let (y, m, d) = maybe_raise(
206
- e_value.decrypt(&context, field),
207
- "Failed to decrypt date value",
208
- );
209
- let klass = Class::from_existing("Date");
210
- let args: [AnyObject; 3] = [
211
- Integer::from(y as i32).into(),
212
- Integer::from(m as i32).into(),
213
- Integer::from(d as i32).into(),
214
- ];
215
- klass.protect_send("new", &args).unwrap()
216
- }
217
- fn enquo_field_encrypt_text(
218
- text_obj: RString,
219
- context_obj: RString,
220
- mode_obj: Symbol,
221
- order_code_len_obj: Integer
222
- ) -> RString {
223
- let text = text_obj.to_str();
224
- let context = context_obj.to_vec_u8_unchecked();
225
- let mode = mode_obj.to_str();
226
-
227
- let field = rbself.get_data(&*FIELD_WRAPPER);
228
-
229
- let mut res = maybe_raise(
230
- if mode == "unsafe" {
231
- Text::new_with_unsafe_parts(
232
- text,
233
- &context,
234
- field,
235
- None,
236
- )
237
- } else if mode == "orderable" {
238
- let order_code_len = order_code_len_obj.to_i64() as u8;
239
- Text::new_with_unsafe_parts(
240
- text,
241
- &context,
242
- field,
243
- Some(order_code_len),
244
- )
245
- } else {
246
- Text::new(text, &context, field)
31
+ r.map_err(|e| {
32
+ magnus::Error::new(
33
+ ex_class,
34
+ match &s {
35
+ None => e.to_string(),
36
+ Some(s) => format!("{s}: {e}"),
247
37
  },
248
- "Failed to create encrypted date",
249
- );
250
- if mode == "no_query" {
251
- maybe_raise(res.make_unqueryable(), "Failed to make ciphertext unqueryable");
252
- }
253
-
254
- RString::new_utf8(&maybe_raise(serde_json::to_string(&res), "Failed to JSONify ciphertext"))
255
- },
256
- fn enquo_field_decrypt_text(ciphertext_obj: RString, context_obj: RString) -> RString {
257
- let ct = ciphertext_obj.to_str_unchecked();
258
- let context = context_obj.to_vec_u8_unchecked();
259
-
260
- let field = rbself.get_data(&*FIELD_WRAPPER);
261
-
262
- let e_value: Text =
263
- maybe_raise(serde_json::from_str(ct), "Failed to deserialize ciphertext");
264
-
265
- let s = maybe_raise(e_value.decrypt(&context, field), "Failed to decrypt text value");
266
-
267
- RString::new_utf8(&s)
268
- },
269
- fn enquo_field_encrypt_text_length_query(
270
- len_obj: Integer
271
- ) -> RString {
272
- let len = len_obj.to_u32();
273
-
274
- let field = rbself.get_data(&*FIELD_WRAPPER);
275
-
276
- let v_set = maybe_raise(Text::query_length(len, field), "Failed to encrypt length as value set");
277
- RString::new_utf8(&maybe_raise(serde_json::to_string(&v_set), "Failed to JSONify value set"))
278
- }
279
- );
280
-
281
- #[allow(non_snake_case)]
282
- #[no_mangle]
283
- pub extern "C" fn Init_enquo() {
284
- Module::from_existing("Enquo").define(|topmod| {
285
- topmod
286
- .define_nested_class("Root", None)
287
- .define(|rootklass| {
288
- rootklass.singleton_class().def_private(
289
- "_new_from_static_root_key",
290
- enquo_root_new_from_static_root_key,
291
- );
292
- rootklass.def_private("_field", enquo_root_field);
293
- });
294
- topmod
295
- .define_nested_class("Field", None)
296
- .define(|fieldklass| {
297
- fieldklass.def_private("_encrypt_boolean", enquo_field_encrypt_boolean);
298
- fieldklass.def_private("_decrypt_boolean", enquo_field_decrypt_boolean);
299
- fieldklass.def_private("_encrypt_i64", enquo_field_encrypt_i64);
300
- fieldklass.def_private("_decrypt_i64", enquo_field_decrypt_i64);
301
- fieldklass.def_private("_encrypt_date", enquo_field_encrypt_date);
302
- fieldklass.def_private("_decrypt_date", enquo_field_decrypt_date);
303
- fieldklass.def_private("_encrypt_text", enquo_field_encrypt_text);
304
- fieldklass.def_private("_decrypt_text", enquo_field_decrypt_text);
305
- fieldklass.def_private(
306
- "_encrypt_text_length_query",
307
- enquo_field_encrypt_text_length_query,
308
- );
309
- });
310
- topmod.define_nested_module("RootKey").define(|rkmod| {
311
- rkmod
312
- .define_nested_class("Static", None)
313
- .define(|statickeyklass| {
314
- statickeyklass
315
- .singleton_class()
316
- .def_private("_new", enquo_root_key_static_new);
317
- });
318
- });
319
- });
38
+ )
39
+ })
320
40
  }
@@ -0,0 +1,39 @@
1
+ use magnus::{class, function, method, prelude::*, RModule};
2
+ use std::ops::Deref;
3
+
4
+ use crate::{field::Field, maybe_raise, root_key::RootKey};
5
+
6
+ #[magnus::wrap(class = "Enquo::Root")]
7
+ struct Root(enquo_core::Root);
8
+
9
+ impl Deref for Root {
10
+ type Target = enquo_core::Root;
11
+
12
+ fn deref(&self) -> &Self::Target {
13
+ &self.0
14
+ }
15
+ }
16
+
17
+ impl Root {
18
+ fn new(k: &RootKey) -> Result<Self, magnus::Error> {
19
+ Ok(Self(maybe_raise(
20
+ enquo_core::Root::new(k.deref().clone()),
21
+ None,
22
+ )?))
23
+ }
24
+
25
+ fn field(&self, relation: String, name: String) -> Result<Field, magnus::Error> {
26
+ Ok(Field(maybe_raise(
27
+ self.0.field(relation.as_bytes(), name.as_bytes()),
28
+ None,
29
+ )?))
30
+ }
31
+ }
32
+
33
+ pub fn init(base: &RModule) -> Result<(), magnus::Error> {
34
+ let class = base.define_class("Root", class::object())?;
35
+
36
+ class.define_singleton_method("new", function!(Root::new, 1))?;
37
+ class.define_method("field", method!(Root::field, 2))?;
38
+ Ok(())
39
+ }
@@ -0,0 +1,67 @@
1
+ use enquo_core::{key_provider, key_provider::KeyProvider};
2
+ use magnus::{class, encoding, exception, function, prelude::*, RModule};
3
+ use std::ops::Deref;
4
+ use std::sync::Arc;
5
+
6
+ #[magnus::wrap(class = "Enquo::RootKey")]
7
+ pub struct RootKey(Arc<dyn KeyProvider>);
8
+
9
+ impl Deref for RootKey {
10
+ type Target = Arc<dyn KeyProvider>;
11
+
12
+ fn deref(&self) -> &Self::Target {
13
+ &self.0
14
+ }
15
+ }
16
+
17
+ fn new_static_root_key(k_str: magnus::RString) -> Result<RootKey, magnus::Error> {
18
+ let encindex = k_str.enc_get();
19
+
20
+ let k: &[u8] = &if encindex == encoding::Index::ascii8bit() {
21
+ if k_str.len() == 32 {
22
+ Ok(unsafe { (*k_str.as_slice()).to_vec() })
23
+ } else {
24
+ Err(magnus::Error::new(
25
+ exception::arg_error(),
26
+ "binary key string must be exactly 32 bytes long".to_string(),
27
+ ))
28
+ }
29
+ } else if encindex == encoding::Index::utf8() || encindex == encoding::Index::usascii() {
30
+ if k_str.len() == 64 {
31
+ Ok(unsafe {
32
+ hex::decode(k_str.as_slice()).map_err(|e| {
33
+ magnus::Error::new(
34
+ exception::arg_error(),
35
+ format!("hex key must only contain valid hex characters: {e}"),
36
+ )
37
+ })?
38
+ })
39
+ } else {
40
+ Err(magnus::Error::new(
41
+ exception::arg_error(),
42
+ format!(
43
+ "hex key string must be exactly 64 characters long (got {} characters)",
44
+ k_str.len()
45
+ ),
46
+ ))
47
+ }
48
+ } else {
49
+ Err(magnus::Error::new(
50
+ exception::encoding_error(),
51
+ "key string must be encoded as BINARY or UTF-8".to_string(),
52
+ ))
53
+ }?;
54
+
55
+ Ok(RootKey(Arc::new(key_provider::Static::new(k))))
56
+ }
57
+
58
+ pub fn init(base: &RModule) -> Result<(), magnus::Error> {
59
+ let base_class = base.define_class("RootKey", class::object())?;
60
+
61
+ {
62
+ let class = base_class.define_class("Static", base_class)?;
63
+ class.define_singleton_method("new", function!(new_static_root_key, 1))?;
64
+ }
65
+
66
+ Ok(())
67
+ }
data/lib/2.7/enquo.bundle CHANGED
Binary file
data/lib/3.0/enquo.bundle CHANGED
Binary file
data/lib/3.1/enquo.bundle CHANGED
Binary file
data/lib/3.2/enquo.bundle CHANGED
Binary file
data/lib/enquo.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "date"
2
+
1
3
  module Enquo
2
4
  class Error < StandardError; end
3
5
  end
@@ -12,7 +14,3 @@ rescue LoadError
12
14
  raise LoadError, "Failed to load enquo.#{RbConfig::CONFIG["DLEXT"]}; either it hasn't been built, or was built incorrectly for your system"
13
15
  end
14
16
  end
15
-
16
- require_relative "./enquo/root"
17
- require_relative "./enquo/root_key"
18
- require_relative "./enquo/field"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enquo-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.2.gb37f667
4
+ version: 0.8.0
5
5
  platform: arm64-darwin
6
6
  authors:
7
7
  - Matt Palmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-14 00:00:00.000000000 Z
11
+ date: 2023-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -170,17 +170,16 @@ files:
170
170
  - ext/enquo/Cargo.lock
171
171
  - ext/enquo/Cargo.toml
172
172
  - ext/enquo/extconf.rb
173
+ - ext/enquo/src/field.rs
173
174
  - ext/enquo/src/lib.rs
175
+ - ext/enquo/src/root.rs
176
+ - ext/enquo/src/root_key.rs
174
177
  - lib/2.7/enquo.bundle
175
178
  - lib/3.0/enquo.bundle
176
179
  - lib/3.1/enquo.bundle
177
180
  - lib/3.2/enquo.bundle
178
181
  - lib/enquo-core.rb
179
182
  - lib/enquo.rb
180
- - lib/enquo/field.rb
181
- - lib/enquo/root.rb
182
- - lib/enquo/root_key.rb
183
- - lib/enquo/root_key/static.rb
184
183
  - lib/enquo_core.rb
185
184
  homepage: https://enquo.org/active_enquo
186
185
  licenses: []
@@ -203,9 +202,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
203
202
  version: 3.3.dev
204
203
  required_rubygems_version: !ruby/object:Gem::Requirement
205
204
  requirements:
206
- - - ">"
205
+ - - ">="
207
206
  - !ruby/object:Gem::Version
208
- version: 1.3.1
207
+ version: '0'
209
208
  requirements: []
210
209
  rubygems_version: 3.4.4
211
210
  signing_key: