toJson method

  1. @override
Map<String, dynamic> toJson()
override

Serializes the JsonWebSignature or JsonWebEncryption to a JSON representation.

For JsonWebSignature, returns a flattened JSON serialization when it contains only one signature and a general JSON serialization otherwise.

Implementation

@override
Map<String, dynamic> toJson() {
  var aad = additionalAuthenticatedData;
  var v = {
    'protected': sharedProtectedHeader?.toBase64EncodedString(),
    'unprotected': sharedUnprotectedHeader?.toJson(),
    'iv': encodeBase64EncodedBytes(initializationVector),
    if (aad != null) 'aad': encodeBase64EncodedBytes(aad),
    'ciphertext': encodeBase64EncodedBytes(data),
    'tag': encodeBase64EncodedBytes(authenticationTag),
  };
  if (recipients.length == 1) {
    v.addAll(recipients.first.toJson());
  } else {
    v['recipients'] = recipients.map((r) => r.toJson()).toList();
  }
  return Map.fromEntries(v.entries.where((e) => e.value != null));
}