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
|
# File 'lib/pq_crypto/seal/format.rb', line 111
def parse(bytes)
source = String(bytes).b
prefix = Binary::Reader.new(source)
expect!(prefix.bytes(MAGIC.bytesize), MAGIC, "invalid envelope magic")
expect!(prefix.u8, VERSION, "unsupported format version")
= prefix.u32
(, prefix.offset)
Binary.ensure_available!(source, 0, )
raw = source.byteslice(0, ).b
fields = Binary::Reader.new(raw, prefix.offset)
content_suite = fields.u16
expect_suite!(content_suite, CONTENT_SUITE_AEGIS256, "content")
lookup_mode = fields.u8
expect_suite!(lookup_mode, LOOKUP_HINT, "lookup mode")
flags = fields.u16
expect!(flags, FLAGS, "unknown header flags")
padding_policy_id = Format.validate_padding_policy_id!(fields.u8, error: FormatError)
payload_id = fields.bytes(PAYLOAD_ID_BYTES)
payload_nonce = fields.bytes(NONCE_BYTES)
capacity = Format.validate_capacity!(fields.u16, error: FormatError)
slot_size = Format.validate_slot_size!(fields.u16, error: FormatError)
padded_inner_length = fields.u64
raise FormatError, "padded inner frame is too short" if padded_inner_length < INNER_PREFIX_BYTES
public_length = fields.u32
raise FormatError, "public metadata is too large" if public_length > MAX_PUBLIC_METADATA_BYTES
public_metadata = fields.bytes(public_length)
raise FormatError, "non-canonical header length" unless fields.finished?
Header.new(
raw: raw,
content_suite_id: content_suite,
lookup_mode: lookup_mode,
flags: flags,
padding_policy_id: padding_policy_id,
payload_id: payload_id,
payload_nonce: payload_nonce,
recipient_capacity: capacity,
slot_size: slot_size,
padded_inner_length: padded_inner_length,
public_metadata: public_metadata
)
end
|