# `Sinetex.Header`
[🔗](https://github.com/openspb/sinetex/blob/v0.1.0/lib/sinetex/header.ex#L1)

The 588-byte security header (*Manual de Segurança do SFN* §5.1, Table 1).

Field names follow the manual:

| Field | Size | Content |
|---|---:|---|
| `c01` | 2 | header size, always 588 |
| `c02` | 1 | protocol version (`0x02`, `0x03`; `0x00` for clear text) |
| `c03` | 1 | error code, `0x00` when there is no error |
| `c04` | 1 | special treatment indicator |
| `c05` | 1 | reserved, `0x00` |
| `c06` | 1 | recipient key algorithm (`0x02` = RSA-2048) |
| `c07` | 1 | symmetric algorithm (`0x01` = 3DES, `0x02` = AES-256) |
| `c08` | 1 | sender key algorithm (`0x02` = RSA-2048) |
| `c09` | 1 | hash algorithm (`0x03` = SHA-256) |
| `c10` | 1 | certification authority (PC) of the recipient certificate |
| `c11` | 32 | serial number of the recipient certificate (`Sinetex.Serial`) |
| `c12` | 1 | certification authority (PC) of the sender certificate |
| `c13` | 32 | serial number of the sender certificate |
| `c14` | 256 | symmetric key encrypted with RSAES-PKCS1-v1_5 |
| `c15` | 256 | signature, RSASSA-PKCS1-v1_5 |

`c01` is read and written as a big-endian 16-bit integer.

The header is **not** covered by the signature: the protocol only signs the
content. `Sinetex.open/2` checks every field against the certificates
involved, but bytes such as C03, C04 and C05 can be changed in transit
without breaking the signature.

# `t`

```elixir
@type t() :: %Sinetex.Header{
  c01: 0..65535,
  c02: byte(),
  c03: byte(),
  c04: byte(),
  c05: byte(),
  c06: byte(),
  c07: byte(),
  c08: byte(),
  c09: byte(),
  c10: byte(),
  c11: &lt;&lt;_::256&gt;&gt;,
  c12: byte(),
  c13: &lt;&lt;_::256&gt;&gt;,
  c14: &lt;&lt;_::2048&gt;&gt;,
  c15: &lt;&lt;_::2048&gt;&gt;
}
```

# `decode`

```elixir
@spec decode(binary()) :: {:ok, t(), body :: binary()} | {:error, :too_short}
```

Splits an envelope into its header and body.

Only the layout is decoded; field values are not validated. Returns
`{:error, :too_short}` if there are fewer than 588 bytes.

# `empty?`

```elixir
@spec empty?(t()) :: boolean()
```

Returns `true` for the "empty" header of §5.6.3: every field zeroed except
C01. The body of such an envelope is in clear text, neither signed nor
encrypted.

# `encode`

```elixir
@spec encode(t()) :: &lt;&lt;_::4704&gt;&gt;
```

Encodes a header into its 588 bytes.

Raises `ArgumentError` if a field does not fit its size.

# `size`

```elixir
@spec size() :: 588
```

The header size in bytes: 588.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
