Coder Social home page Coder Social logo

hawk's Introduction

Android Arsenal API Join the chat at https://gitter.im/orhanobut/hawk

#Hawk Secure, simple key-value storage for android

Hawk uses:

  • AES for the crypto
  • SharedPreferences for the storage
  • Gson for parsing

Hawk provides:

  • Secure data persistence
  • Save any type
  • Save list of any type

###Add dependency

compile 'com.orhanobut:hawk:1.6'

Initialize the hawk

Hawk.init(context, PASSWORD);

init takes 200-400ms depends on the device. You may want to use async solution in order to avoid this. Add a callback to init and it will work asynchronous.

Hawk.init(context, PASSWORD, new Hawk.Callback() {
        @Override
        public void onSuccess() {
        }

        @Override
        public void onFail(Exception e) {
        }
    }
);

Save

Hawk.put(key, T); // Returns the result as boolean

or

Hawk.put(key, List<T>); // Returns the result as boolean

You can also store multiple items at once by using chain feature. Remember to use commit() at the end. Either all of them will be saved or none.

// Returns the result as boolean
Hawk.chain()
     .put(KEY_LIST, List<T>)
     .put(KEY_ANOTHER,"test")
     .commit();

Get

T result = Hawk.get(key);

or with default value

T result = Hawk.get(key, T);

Remove

Hawk.remove(key); // Returns the result as boolean

or you can remove multiple items at once

Hawk.remove(KEY_LIST, KEY_NAME); // Returns the result as boolean

Contains

boolean contains = Hawk.contains(key);

Set the log output (optional)

Hawk.init(context,PASSWORD, LogLevel.FULL); // as default it is NONE
More samples for save
Hawk.put("key", "something"); // Save string
Hawk.put("key", true); // save boolean
Hawk.put("key", new Foo()); // save an object
Hawk.put("key", List<String>); // save list
Hawk.put("key", List<Foo>); // save list of any type
Hawk.put("key", 1234); // save numbers
More samples for get
String value = Hawk.get(key);
int value = Hawk.get(key);
Foo value = Hawk.get(key);
boolean value = Hawk.get(key);
List<String> value = Hawk.get(key);
List<Foo> value = Hawk.get(key);

or with the defaults

String value = Hawk.get(key, "");
int value = Hawk.get(key, 0);
Foo value = Hawk.get(key, new Foo());
boolean value = Hawk.get(key, false);
List<String> value = Hawk.get(key, Collections.emptyList());
List<Foo> value = Hawk.get(key, new ArrayList<Foo>);
Benchmark result (ms)

Done with Nexus 4, Android L. Note that this is not certain values, I just made a few runs and show it to give you an idea.

How Hawk works

Notes
  • Password should be provided by the user, we try to find better solution for this.
  • Hawk.init() takes around 200-500ms depends on the phone.
  • Salt key is stored plain text in the storage currently. We are checking to find a better solution for this. Any contribution about this will be great help as well.
Credits

I use the following implementation for the crypto and I believe it should get more attention. Thanks for this great hard work. https://github.com/tozny/java-aes-crypto and a great article about it : http://tozny.com/blog/encrypting-strings-in-android-lets-make-better-mistakes/

You might also like

  • Wasp All-in-one network solution
  • Bee QA/Debug tool
  • DialogPlus Easy,simple dialog solution
  • SimpleListView Simple basic listview implementation with linearlayout

###License

Copyright 2015 Orhan Obut

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

hawk's People

Contributors

orhanobut avatar tslamic avatar kardeslik avatar gitter-badger avatar bosgood avatar

Watchers

James Cloos avatar  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.