dio-http-cache icon indicating copy to clipboard operation
dio-http-cache copied to clipboard

Any example on cipher to encrypt cache?

Open zdnet opened this issue 3 years ago • 2 comments

Could add an example on this config usage? thanks!

final options = const CacheOptions( store: MemCacheStore(), policy: CachePolicy.request, hitCacheOnErrorExcept: [401, 403], maxStale: const Duration(days: 7), priority: CachePriority.normal, cipher: null, keyBuilder: CacheOptions.defaultCacheKeyBuilder, allowPostMethod: false, );

zdnet avatar Oct 14 '21 01:10 zdnet

Anybody can explain an example for encrypt/decrypt?

hsul4n avatar Nov 25 '21 18:11 hsul4n

I got this..

import 'dart:typed_data';

import 'package:encrypt/encrypt.dart';
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';

main() {
  final key = Key.fromUtf8('my32lengthsupersecretnooneknows1'); // Just example
  final iv = IV.fromLength(16);

  final encrypter = Encrypter(AES(key));

  final cacheConfig = CacheConfig(
    /// ...
    encrypt: (bytes) async {
      return encrypter.encryptBytes(bytes, iv: iv).bytes;
    },
    decrypt: (bytes) async {
      return encrypter.decryptBytes(
        Encrypted(Uint8List.fromList(bytes)),
        iv: iv,
      );
    },
  );
}

hsul4n avatar Nov 26 '21 21:11 hsul4n