Class: SelfCrypto::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/self_crypto/account.rb,
ext/self_crypto/account.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
# File 'ext/self_crypto/account.c', line 13

static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE pickle, password, seed, opts;
    size_t size;
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    (void)rb_scan_args(argc, argv, "0:", &opts);

    opts = (opts == Qnil) ? rb_hash_new(): opts;

    pickle = rb_hash_aref(opts, ID2SYM(rb_intern("pickle")));
    password = rb_hash_aref(opts, ID2SYM(rb_intern("password")));
    seed = rb_hash_aref(opts, ID2SYM(rb_intern("seed")));

    if(pickle != Qnil){

        if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){

            rb_raise(rb_eTypeError, "pickle must be kind of String");
        }
    }

    if(password != Qnil){

        if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){

            rb_raise(rb_eTypeError, "password must be kind of String");
        }
    }
    else{

        password = rb_str_new2("");
    }

    if(pickle != Qnil){

        if(olm_unpickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == olm_error()){

            raise_olm_error(olm_account_last_error(this));
        }
    }
    if(seed != Qnil){
        if(olm_create_account_derrived_keys(this, RSTRING_PTR(seed), RSTRING_LEN(seed)) == olm_error()){

            raise_olm_error(olm_account_last_error(this));
        }
    }
    else{

        size = olm_create_account_random_length(this);

        if(olm_create_account(this, RSTRING_PTR(get_random(size)), size) == olm_error()){

            raise_olm_error(olm_account_last_error(this));
        }
    }

    return self;
}

Class Method Details

.from_pickle(pickle, password = "") ⇒ Account

Parameters:

  • pickle (String)

    pickled state

  • password (String) (defaults to: "")

    password used to encrypt pickled state

Returns:



10
11
12
# File 'lib/self_crypto/account.rb', line 10

def self.from_pickle(pickle, password="")
  Account.new(pickle: pickle, password: password)
end

.from_seed(seed) ⇒ Object



14
15
16
# File 'lib/self_crypto/account.rb', line 14

def self.from_seed(seed)
  Account.new(seed: Base64.decode64(seed))
end

Instance Method Details

#gen_otk(number = 1) ⇒ Object



18
19
20
# File 'lib/self_crypto/account.rb', line 18

def gen_otk(number=1)
  generate_one_time_keys(number)
end

#generate_one_time_keys(number) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/self_crypto/account.c', line 91

static VALUE generate_one_time_keys(VALUE self, VALUE number)
{
    size_t size;
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    size = olm_account_generate_one_time_keys_random_length(this, NUM2SIZET(number));

    if(olm_account_generate_one_time_keys(this, NUM2SIZET(number), RSTRING_PTR(get_random(size)), size) == olm_error()){

        raise_olm_error(olm_account_last_error(this));
    }

    return self;
}

#identity_keysObject Also known as: ik



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/self_crypto/account.c', line 74

static VALUE identity_keys(VALUE self)
{
    OlmAccount *this;
    size_t size;
    Data_Get_Struct(self, OlmAccount, this);

    size = olm_account_identity_keys_length(this);
    uint8_t buf[size];

    if(olm_account_identity_keys(this, buf, size) != size){

        raise_olm_error(olm_account_last_error(this));
    }

    return rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new((char *)buf, size));
}

#inbound_session(*args) ⇒ Object



211
212
213
214
215
216
217
218
# File 'ext/self_crypto/account.c', line 211

static VALUE inbound_session(int argc, VALUE *argv, VALUE self)
{
    VALUE args[] = {self, Qnil, Qnil};

    (void)rb_scan_args(argc, argv, "11", &args[1], &args[2]);

    return rb_class_new_instance(sizeof(args)/sizeof(*args), args, rb_eval_string("SelfCrypto::InboundSession"));
}

#last_errorObject



5
6
7
8
9
10
11
# File 'ext/self_crypto/account.c', line 5

static VALUE last_error(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    return rb_funcall(rb_eval_string("SelfCrypto::OlmError"), rb_intern("from_string"), 1, rb_str_new2(olm_account_last_error(this)));
}

#mark_keys_as_publishedObject Also known as: mark_otk



172
173
174
175
176
177
178
179
180
# File 'ext/self_crypto/account.c', line 172

static VALUE mark_keys_as_published(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    (void)olm_account_mark_keys_as_published(this);

    return self;
}

#max_number_of_one_time_keysObject Also known as: max_otk



182
183
184
185
186
187
188
# File 'ext/self_crypto/account.c', line 182

static VALUE max_number_of_one_time_keys(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    return SIZET2NUM(olm_account_max_number_of_one_time_keys(this));
}

#one_time_keysObject Also known as: otk



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
# File 'ext/self_crypto/account.c', line 115

static VALUE one_time_keys(VALUE self)
{
    VALUE retval;
    size_t size;
    void *ptr;
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    size = olm_account_one_time_keys_length(this);

    // add an additional byte to stop this from overflowing
    if((ptr = malloc(size+1)) == NULL){

        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    if(olm_account_one_time_keys(this, ptr, size) != size){

        free(ptr);
        raise_olm_error(olm_account_last_error(this));
    }

    retval = rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new(ptr, size));

    free(ptr);

    return retval;
}

#one_time_keys_sizeObject



107
108
109
110
111
112
113
# File 'ext/self_crypto/account.c', line 107

static VALUE one_time_keys_size(VALUE self)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    return SIZET2NUM(olm_account_one_time_keys_length(this));
}

#outbound_session(identity_key, pre_key) ⇒ Object



206
207
208
209
# File 'ext/self_crypto/account.c', line 206

static VALUE outbound_session(VALUE self, VALUE identity_key, VALUE pre_key)
{
    return rb_funcall(rb_eval_string("SelfCrypto::OutboundSession"), rb_intern("new"), 3, self, identity_key, pre_key);
}

#remove_one_time_keys(session) ⇒ Object Also known as: update_otk



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'ext/self_crypto/account.c', line 190

static VALUE remove_one_time_keys(VALUE self, VALUE session)
{
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    OlmSession *s;
    Data_Get_Struct(session, OlmSession, s);

    if(olm_remove_one_time_keys(this, s) == olm_error()){

        raise_olm_error(olm_account_last_error(this));
    }

    return self;
}

#sign(message) ⇒ Object



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
# File 'ext/self_crypto/account.c', line 144

static VALUE sign(VALUE self, VALUE message)
{
    VALUE retval;
    size_t size;
    void *ptr;
    OlmAccount *this;
    Data_Get_Struct(self, OlmAccount, this);

    size = olm_account_signature_length(this);

    if((ptr = malloc(size)) == NULL){

        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    if(olm_account_sign(this, RSTRING_PTR(message), RSTRING_LEN(message), ptr, size) == olm_error()){

        free(ptr);
        raise_olm_error(olm_account_last_error(this));
    }

    retval = rb_str_new(ptr, size);

    free(ptr);

    return retval;
}

#to_pickle(*args) ⇒ Object



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
# File 'ext/self_crypto/account.c', line 220

static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
{
    VALUE password, retval;
    OlmAccount *this;
    void *ptr;
    size_t size;
    Data_Get_Struct(self, OlmAccount, this);

    (void)rb_scan_args(argc, argv, "01", &password);

    password = (password == Qnil) ? rb_str_new2("") : password;

    size = olm_pickle_account_length(this);

    if((ptr = malloc(size)) == NULL){

        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    if(olm_pickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size){

        free(ptr);
        raise_olm_error(olm_account_last_error(this));
    }

    retval = rb_str_new(ptr, size);

    free(ptr);

    return retval;
}