# `EncryptedValue`

A value sealed by `crypto.encrypt`.

Use it to store something you must read back later, such as a provider's token. `crypto.encrypt(value, key: key, context: context)` gives the bytes, with a key from your settings. For a value you only need to compare, use [`Hash`](/docs/types/type-hash.md) or [`PasswordHash`](/docs/types/type-password-hash.md).

## Shape

One value, held as bytes, with no parts. As text it reads `AQGhssPUGBkaGxwdHh8gISIjJCUmJygpKissLS4vaGVsbG+QkZKTlJWWl5iZmpucnZ6f`.

## Example

```jadpo
type NotSealed = Failure {
    kind: FailureKind.invalid_value
    message: "That isn't a sealed value."
}

var example: EncryptedValue = attempt text.parse(
    "AQGhssPUAAECAwQFBgcICQoLDA0ODxAREhMUFRYXgIGCg4SFhoeIiYqLjI2Ojw==",
    as: EncryptedValue
) {
    malformed: NotSealed
}
assert example == example
```

`EncryptedValue(bytes)` checks them, and bytes that aren't one break the `format` rule.

`text.parse(raw, as: EncryptedValue)` reads one from text that comes from outside. Text that isn't one at all fails `malformed`.

## Good to know

- It may be stored and sent. Only `crypto.decrypt`, with the key, reads what it holds.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it.

## In JSON

A string, in a request, a response or `json.write`:

```json
"AQGhssPUAAECAwQFBgcICQoLDA0ODxAREhMUFRYXgIGCg4SFhoeIiYqLjI2Ojw=="
"AQGhssPUGBkaGxwdHh8gISIjJCUmJygpKissLS4vaGVsbG+QkZKTlJWWl5iZmpucnZ6f"
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`EncryptedValue` is in the group Security, and has been in Jadpo since 0.1.0.

### Text form

`text.from` writes one standard text form, the same form `text.parse` reads back: `AQGhssPUAAECAwQFBgcICQoLDA0ODxAREhMUFRYXgIGCg4SFhoeIiYqLjI2Ojw==` and `AQGhssPUGBkaGxwdHh8gISIjJCUmJygpKissLS4vaGVsbG+QkZKTlJWWl5iZmpucnZ6f`.

### How it is stored

It is stored in one column named for the field, of type `bytea`. `AQGhssPUGBkaGxwdHh8gISIjJCUmJygpKissLS4vaGVsbG+QkZKTlJWWl5iZmpucnZ6f` is stored as `E'\\x0101a1b2c3d418191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f68656c6c6f909192939495969798999a9b9c9d9e9f'::bytea`.

### How it is shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `AQGhssPUAAECAwQFBgcICQoLDA0ODxAREhMUFRYXgIGCg4SFhoeIiYqLjI2Ojw==` | `AQGhssPUAAECAwQFBgcICQoLDA0ODxAREhMUFRYXgIGCg4SFhoeIiYqLjI2Ojw==` |
| `AQGhssPUGBkaGxwdHh8gISIjJCUmJygpKissLS4vaGVsbG+QkZKTlJWWl5iZmpucnZ6f` | `AQGhssPUGBkaGxwdHh8gISIjJCUmJygpKissLS4vaGVsbG+QkZKTlJWWl5iZmpucnZ6f` |

### Operations

It also takes `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
