CipherSharedPrefs


Source link: https://github.com/Ufkoku/CipherSharedPrefs

CipherSharedPrefs

CipherSharedPrefs implements SharedPreferences with encryption layer and some new features.

repositories {

  maven {
 url 'https://dl.bintray.com/ufkoku/maven/' 
}
 
}
  dependencies {

  compile 'com.ufkoku.cipher_sharedprefs:sharedprefs:version'
  //or
  compile 'com.ufkoku.cipher_sharedprefs:sharedprefs:version@aar'{

transitive = true
  
}
 
}

How it works

When you put value to CipherSharedPreferences it is transformed to String and encrypted by provided ICipherHolder object, encryped string is saved to SharedPreferences delegate.

new CipherSharedPreferences(

  context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE),

  iCipherHolder,

  iCacheHolder, //optional

  iTransformer //optional

)

Cipher

You must provide implementation of ICipherHolder to CipherSharedPreferences constructor. It is used for data encryption and decryption.

Library contains AES implementation AesCipher.

  • Default mode: ECB;
  • Supported modes: CBC, CFB, CTR, ECB, OFB;
  • IV requires modes: CBC, CFB, OFB, CTR;
  • Supported keys: Supported size 128/192/256 bits -> 16/24/32 bytes;
  • IV lenght: equals to key lenght;

IV - initialization vector

dependencies {

  compile 'com.ufkoku.cipher_sharedprefs:ciphers:version'
  //or
  compile 'com.ufkoku.cipher_sharedprefs:ciphers:version@aar'{

transitive = true
  
}
 
}

Cache (Optional)

You can provide ICacheHolder to CipherSharedPreferences.

If ICacheHolder exists, values WITHOUT encryption will be saved there, after get/put methods for key.
When you call get methods, CipherSharedPreferences checks cache, and if value exists - it will be returned, otherwise it will be decrypted from delegate, and saved to cache.
When you call put methods, CipherSharedPreferences saves value to cache WITHOUT encryption, and encrypted string to delegate.

PROS

  • When you getting value, that contains in cache, you are not spending time on decryption.

LIMITATIONS

  • If you use cache, you should use CipherSharedPreferences or ICacheHolder as singleton, cache won't be refreshed if you modify prefs from another instance.

There are already implemented cache holders, that you can use in module caches

dependencies {

  compile 'com.ufkoku.cipher_sharedprefs:caches:version'
  //or
  compile 'com.ufkoku.cipher_sharedprefs:caches:version@aar'{

transitive = true
  
}
 
}

Implementations:

  • SharedPrefsLruCache wrap over SizeBasedEnhancedLruCache, it limits cache by size in bytes.
  • SharedPrefsMapCache wrap over HashMap

Object saving (Optional)

CipherSharedPreferences can save objects and retrieve them back via methods

CipherSharedPreferences {

 //get object
  <T> getObject(key: String, defValue: T?, clazz: Class<T>): T?

CipherEditor{

//save object

putObject(key: String, value: Any?): SharedPreferences.Editor
  
}
  
}

To use this feature you need to provide ITransformer implementation to CipherSharedPreferences constructor.

//Example of ITransformer //This transformer uses GSON to transform input objects to string and back. transformer = new ITransformer() {

 private Gson gson = new Gson();

  @NotNull

 @Override

 public String toString(@NotNull Object o) {

  return gson.toJson(o);

 
}

  @Override

 public <T> T fromString(@NotNull String string, @NonNull Class<T> clazz) {

  return gson.fromJson(string, clazz);

 
}

 
}
;

Example of usage


  Set<String> value = new HashSet<>();

value.add("str1");

value.add("str2");

value.add("str3");

preferences.edit().putObject(KEY_1, value).commit();

preferences.getObject(KEY_1, null, value.getClass())

  

Resources

A Gradle plugin that creates FindBugs tasks for each variant of android application or library project.

Fluid Java interface to OpenStreetMap data through querying the Overpass API. No more query string forging by hand.

Generate build info for your Android project without breaking incremental compilation.

Reliable Android testing, at your service.

An Android RecyclerView.Adapter management library that makes creating and editing heterogeneous view based lists easy.

Http Connection calls made easy.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes