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:
Open the project files from the Handling tag-reading errors recipe.
Implement the
getMd5Hash
method in theNfcHelper
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...