encrypt method

String encrypt({
  1. SecretWalletEncoding encoding = SecretWalletEncoding.json,
})

Encrypts the sensitive wallet data using the specified encoding format and returns the encrypted representation.

  • encoding: The encoding format to use for the encrypted output (default is JSON).

Returns the encrypted wallet data as a string in the chosen encoding format.

Implementation

String encrypt({SecretWalletEncoding encoding = SecretWalletEncoding.json}) {
  // Encrypt the wallet data and obtain the ciphertext bytes.
  final ciphertextBytes = _encryptPassword();
  // print("cipher ${BytesUtils.toHexString(ciphertextBytes)}");
  if (encoding == SecretWalletEncoding.cbor) {
    return _toCbor(ciphertextBytes);
  }

  // Prepare the JSON representation of the encrypted data.
  final Map<String, dynamic> toJson = {
    'crypto': {
      'cipher': 'aes-128-ctr',
      'cipherparams': {'iv': BytesUtils.toHexString(_iv)},
      'ciphertext': BytesUtils.toHexString(ciphertextBytes.item1),
      'kdf': _derivator.name,
      'kdfparams': _derivator.encode(),
      'mac': _mac(ciphertextBytes.item2, ciphertextBytes.item1),
    },
    'id': uuid,
    'version': 3,
  };

  // Convert the JSON to a string.
  final toString = StringUtils.fromJson(toJson);

  // Based on the specified encoding format, return the encrypted data as a string.
  if (encoding == SecretWalletEncoding.json) {
    return toString;
  }
  return StringUtils.decode(
      StringUtils.encode(toString), StringEncoding.base64);
}