Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F7889983
group_sessions.rs
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
group_sessions.rs
View Options
use
pyo3
::
{
prelude
::
*
,
types
::
PyType
};
use
vodozemac
::
megolm
::
{
ExportedSessionKey
,
MegolmMessage
,
SessionKey
};
use
crate
::
error
::
{
LibolmPickleError
,
MegolmDecryptionError
,
PickleError
,
SessionKeyDecodeError
};
#[pyclass]
pub
struct
GroupSession
{
pub
(
super
)
inner
:
vodozemac
::
megolm
::
GroupSession
,
}
#[pymethods]
impl
GroupSession
{
#[new]
fn
new
()
->
Self
{
Self
{
inner
:
vodozemac
::
megolm
::
GroupSession
::
new
(),
}
}
#[getter]
fn
session_id
(
&
self
)
->
&
str
{
self
.
inner
.
session_id
()
}
#[getter]
fn
session_key
(
&
self
)
->
String
{
self
.
inner
.
session_key
().
to_base64
()
}
fn
encrypt
(
&
mut
self
,
plaintext
:
&
str
)
->
String
{
self
.
inner
.
encrypt
(
plaintext
).
to_base64
()
}
fn
pickle
(
&
self
,
pickle_key
:
&
[
u8
])
->
Result
<
String
,
PickleError
>
{
let
pickle_key
:
&
[
u8
;
32
]
=
pickle_key
.
try_into
()
.
map_err
(
|
_
|
PickleError
::
InvalidKeySize
(
pickle_key
.
len
()))
?
;
Ok
(
self
.
inner
.
pickle
().
encrypt
(
pickle_key
))
}
#[classmethod]
fn
from_pickle
(
_cls
:
&
PyType
,
pickle
:
&
str
,
pickle_key
:
&
[
u8
])
->
Result
<
Self
,
PickleError
>
{
let
pickle_key
:
&
[
u8
;
32
]
=
pickle_key
.
try_into
()
.
map_err
(
|
_
|
PickleError
::
InvalidKeySize
(
pickle_key
.
len
()))
?
;
let
pickle
=
vodozemac
::
megolm
::
GroupSessionPickle
::
from_encrypted
(
pickle
,
pickle_key
)
?
;
let
session
=
vodozemac
::
megolm
::
GroupSession
::
from_pickle
(
pickle
);
Ok
(
Self
{
inner
:
session
})
}
}
#[pyclass]
pub
struct
DecryptedMessage
{
#[pyo3(get)]
plaintext
:
String
,
#[pyo3(get)]
message_index
:
u32
,
}
#[pyclass]
pub
struct
InboundGroupSession
{
pub
(
super
)
inner
:
vodozemac
::
megolm
::
InboundGroupSession
,
}
#[pymethods]
impl
InboundGroupSession
{
#[new]
fn
new
(
session_key
:
&
str
)
->
Result
<
Self
,
SessionKeyDecodeError
>
{
let
key
=
SessionKey
::
from_base64
(
session_key
)
?
;
Ok
(
Self
{
inner
:
vodozemac
::
megolm
::
InboundGroupSession
::
new
(
&
key
),
})
}
#[classmethod]
fn
import
(
_cls
:
&
PyType
,
session_key
:
&
str
)
->
Result
<
Self
,
SessionKeyDecodeError
>
{
let
key
=
ExportedSessionKey
::
from_base64
(
session_key
)
?
;
Ok
(
Self
{
inner
:
vodozemac
::
megolm
::
InboundGroupSession
::
import
(
&
key
),
})
}
#[getter]
fn
session_id
(
&
self
)
->
String
{
self
.
inner
.
session_id
()
}
fn
export_at
(
&
mut
self
,
index
:
u32
)
->
Option
<
String
>
{
self
.
inner
.
export_at
(
index
).
map
(
|
k
|
k
.
to_base64
())
}
fn
decrypt
(
&
mut
self
,
ciphertext
:
&
str
)
->
Result
<
DecryptedMessage
,
MegolmDecryptionError
>
{
let
message
=
MegolmMessage
::
from_base64
(
ciphertext
)
?
;
let
ret
=
self
.
inner
.
decrypt
(
&
message
)
?
;
Ok
(
DecryptedMessage
{
plaintext
:
ret
.
plaintext
,
message_index
:
ret
.
message_index
,
})
}
fn
pickle
(
&
self
,
pickle_key
:
&
[
u8
])
->
Result
<
String
,
PickleError
>
{
let
pickle_key
:
&
[
u8
;
32
]
=
pickle_key
.
try_into
()
.
map_err
(
|
_
|
PickleError
::
InvalidKeySize
(
pickle_key
.
len
()))
?
;
Ok
(
self
.
inner
.
pickle
().
encrypt
(
pickle_key
))
}
#[classmethod]
fn
from_pickle
(
_cls
:
&
PyType
,
pickle
:
&
str
,
pickle_key
:
&
[
u8
])
->
Result
<
Self
,
PickleError
>
{
let
pickle_key
:
&
[
u8
;
32
]
=
pickle_key
.
try_into
()
.
map_err
(
|
_
|
PickleError
::
InvalidKeySize
(
pickle_key
.
len
()))
?
;
let
pickle
=
vodozemac
::
megolm
::
InboundGroupSessionPickle
::
from_encrypted
(
pickle
,
pickle_key
)
?
;
let
session
=
vodozemac
::
megolm
::
InboundGroupSession
::
from_pickle
(
pickle
);
Ok
(
Self
{
inner
:
session
})
}
#[classmethod]
fn
from_libolm_pickle
(
_cls
:
&
PyType
,
pickle
:
&
str
,
pickle_key
:
&
str
,
)
->
Result
<
Self
,
LibolmPickleError
>
{
let
inner
=
vodozemac
::
megolm
::
InboundGroupSession
::
from_libolm_pickle
(
pickle
,
pickle_key
)
?
;
Ok
(
Self
{
inner
})
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Oct 2, 2:50 AM (21 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
487016
Default Alt Text
group_sessions.rs (3 KB)
Attached To
Mode
rVB vodozemac-bindings
Attached
Detach File
Event Timeline
Log In to Comment