Giter VIP home page Giter VIP logo

salmaanahmed / encryptedprefs Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 715 KB

Default shared prefrences in android comes with limitations. Developers cant save objects to it, neither are those encrypted. This is to make the storage much more easy, you can now save the models to local storage by calling a single function. And guess what, its also encrypted! Zero boilerplate!

License: MIT License

Java 4.17% Ruby 25.27% Objective-C 8.62% Dart 61.94%

encryptedprefs's Introduction

EncryptedPrefs

Description

Default shared prefrences in android comes with limitations. Developers cant save objects to it, neither are those encrypted. This is to make the storage much more easy, you can now save the models to local storage by calling a single function. And guess what, its also encrypted! Zero boilerplate!



Installation

For installation you may see: https://pub.dartlang.org/packages/encrypted_prefs

How to use

Add serialization/deserialization technique to your object

/// Sample class used for storing locally
class User {
  final String username;
  final String password;

  User(this.username, this.password);

  bool operator == (user) => user is User && user.username == username && user.password == password;

  /// Serializing technique
  User.fromJson(Map<String, dynamic> json)
      : username = json['Username'],
        password = json['Password'];

  /// And deserializing
  Map<String, dynamic> toJson() => {
        'Username': username,
        'Password': password,
      };
}

Generate a secure key and keep it in the safe place. EncryptedPrefs also provide you the method for generating a secure random key.

/// You can generate encryption key using EncryptedPrefs
encryptionKey = await prefs.generateRandomKey();

Extend your adapter with FlagChatAdapter and pass context to the adapter

class ChatAdapter(context: Context, private var list: ArrayList<Any>) : FlagChatAdapter(context)

Store and retrieve objets with encryption

Its one liner code To encrypt and save, you just need to call set method

/// Save data in local storage with your object against key and encryption key
    await prefs.set("User", user, encryptionKey);

To get the decrypted object all you need to do is following

/// Retrieve your data from local storage with your key, encryption key and the serialization technique you are using
    prefs.get("User", encryptionKey, (json) => User.fromJson(json)).then((value) {
        if (user == value) {
          /// Do whatever you want :)
        }
    });

Yes! Is it async

Yes! EncryptedPrefs are created by using the best technique and approaches availabe. This is async and you dont have to worry about anything.

Contributions

EncryptedPrefs wouldnt be here without help of Stefano Rodriguez. Special thanks to him for the encryption/decryption.

Pull requests are welcome! The best contributions will consist of substitutions or configurations for classes/methods known to block the main thread during a typical app lifecycle.

I would love to know if you are using EncryptedPrefs in your app, send an email to Salmaan Ahmed

encryptedprefs's People

Contributors

salmaanahmed avatar

Stargazers

Basel Ajarmeh avatar  avatar Khurram Shehzad avatar  avatar Abdul Rehman avatar Zain Ul Abbas avatar Bilal Rashid avatar Ahsan Ali avatar Syed Ali Salman Raza avatar  avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.