Book Image

Near Field Communication with Android Cookbook

By : Subtil
Book Image

Near Field Communication with Android Cookbook

By: Subtil

Overview of this book

An easy-to-follow guide, full of hands-on examples of and real-world applications. Each recipe is explained and placed in context. If you want to learn how to create NFC-enabled Android applications, this is the book for you. Perhaps you already know a bit about Android application developments but have never used NFC, or perhaps you know a little about NFC android development but want some more advanced features and examples. In either case, this book will get you up and running quickly. You are expected to have Android programming knowledge.
Table of Contents (12 chapters)
11
Index

Testing the tag data for integrity


In this recipe, we will create a simple project to demonstrate a simple way to test for data integrity using and MD5 hash.

Getting ready

We will be using the project from the previous recipe, so make sure you have gone through it.

How to do it...

In this application, we will be adding an additional NdefRecord instance into our NdefMessage, which contains the MD5 hash of the main message that will allow us to test for data integrity. Perform the following steps:

  1. Open the project files from the Handling tag-reading errors recipe.

  2. Implement the getMd5Hash method in the NfcHelper class:

      private byte[] getMd5Hash(byte[] message) {
    
        byte[] salt = "nfcbook.ch8.example3".getBytes(Charset.forName("US-ASCII"));
    
        try {
    
          MessageDigest md = MessageDigest.getInstance("MD5");
    
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
    
          stream.write(message);
          stream.write(salt);
    
          return md.digest(stream.toByteArray());
    
        } catch (Exception...