CryptEnvelope.fromJson constructor

CryptEnvelope.fromJson(
  1. Map<String, dynamic> json
)

Reconstrói um CryptEnvelope a partir de um Map JSON.

Lança ArgumentError se version estiver ausente, não for inteiro, ou for diferente de currentVersion. Lança ArgumentError se algorithm for desconhecido. Lança TypeError/FormatException para campos estruturalmente inválidos.

Implementation

factory CryptEnvelope.fromJson(Map<String, dynamic> json) {
  final rawVersion = json['version'];
  if (rawVersion is! int) {
    throw ArgumentError(
      'CryptEnvelope: campo "version" ausente ou inválido '
      '(esperado inteiro, recebido: ${rawVersion.runtimeType}). '
      'Payloads legados do EncryptedPayload não possuem "version" — use '
      'AllCrypto.migrateLegacy para convertê-los.',
    );
  }
  if (rawVersion != currentVersion) {
    throw ArgumentError(
      'CryptEnvelope: versão de envelope desconhecida: $rawVersion. '
      'Versão suportada por esta versão do pacote: $currentVersion.',
    );
  }

  final rawAlgorithm = json['algorithm'];
  if (rawAlgorithm is! String) {
    throw const FormatException(
      'CryptEnvelope: missing or invalid "algorithm" field.',
    );
  }

  return CryptEnvelope(
    version: rawVersion,
    algorithm: CryptAlgorithm.fromString(rawAlgorithm),
    ciphertext: _decodeBytes(json, 'ciphertext'),
    nonce: _decodeBytes(json, 'nonce'),
    tag: _decodeBytes(json, 'tag'),
    aad: _decodeBytes(json, 'aad'),
  );
}