hurshi / dio-http-cache

http cache lib for Flutter dio like RxCache
Apache License 2.0
274 stars 223 forks source link

Any example on cipher to encrypt cache? #103

Open zdnet opened 2 years ago

zdnet commented 2 years ago

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, );

hsul4n commented 2 years ago

Anybody can explain an example for encrypt/decrypt?

hsul4n commented 2 years ago

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,
      );
    },
  );
}