kasten/kasten/generator/pack.py

86 lines
2.7 KiB
Python
Raw Normal View History

"""Compile Kasten elements into a packed (msgpack) sequence.
KastenPacked sequences are prepared sequences used by Kasten instances and
their generators to apply rules and validation to Kasten.
Note that encryption and signing are not handled by anything in kasten, only
hash authentication is dependending on the generator used.
Packed bytes structure:
2020-04-11 07:48:28 +00:00
0: type: str: 4bytesmax
1: enc-mode: int: 0, 1, 2 (0=plaintext, 1=asymmetic, 2=symmetric).
2. Timestamp: int
encrypted with specified mode:
3. signer: bytes (max 256bits)
4. signature: bytes (max 256bits)
5. app_metadata: arbitrary JSON
2020-04-11 07:48:28 +00:00
\n
data: bytes
"""
2020-04-14 09:33:17 +00:00
from math import floor
from time import time
2020-04-11 07:48:28 +00:00
from msgpack import packb
2020-04-14 09:33:17 +00:00
from kasten import exceptions
from kasten.types import KastenPacked
"""
Copyright (C) <2020> Kevin Froman
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
2020-04-11 07:48:28 +00:00
def pack(data: bytes, data_type: '',
2020-04-11 07:48:28 +00:00
enc_mode: 'KastenEncryptionModeID',
signer: bytes = None, signature: bytes = None,
2020-04-14 09:33:17 +00:00
app_metadata: 'KastenSerializeableDict' = None,
timestamp: int = None
) -> KastenPacked:
"""Create KastenPacked bytes sequence but do not ID or run through generator"""
2020-04-11 07:48:28 +00:00
# Ensure data type does not exceed 4 characters
if not data_type or len(data_type) > 4:
raise exceptions.InvalidKastenTypeLength
2020-04-13 00:53:57 +00:00
# Ensure encryption mode is in [0, 100)
try:
enc_mode = int(enc_mode)
except (TypeError, ValueError):
raise exceptions.InvalidEncryptionMode
if not enc_mode >= 0 or enc_mode >= 100:
raise exceptions.InvalidEncryptionMode
2020-04-14 09:33:17 +00:00
2020-04-11 07:48:28 +00:00
try:
data = data.encode('utf8')
except AttributeError:
pass
2020-04-14 09:33:17 +00:00
if timestamp is None:
timestamp = floor(time())
2020-04-16 07:07:05 +00:00
timestamp = int(timestamp)
2020-04-11 07:48:28 +00:00
2020-04-14 09:33:17 +00:00
kasten_header = [data_type, enc_mode, timestamp]
2020-04-11 07:48:28 +00:00
if signer:
if signature is None:
raise ValueError("Signer specified without signature")
kasten_header.extend((signer, signature))
if app_metadata is not None:
kasten_header.append(app_metadata)
2020-04-16 07:07:05 +00:00
kasten_header = packb(kasten_header) + b'\n'
2020-04-11 07:48:28 +00:00
return kasten_header + data