The application of QR Codes for file integrity checking purposes

Today I would like to continue the discussion on the use of QR codes for security purposes in document management and present two new ideas concerning their practical application, illustrated with prototypical scripts.

QR codes – as I wrote earlier – can be used to visualize data classification labels. But that doesn’t exhaust the potential range of their applications. You can quite easily take advantage of them, for example, to visualize the documents cryptographic checksums and thereby to provide a pretty practical documents integrity verification mechanism. Further, there is also no reason why not to label the documents with their electronic signatures, visualized subsequently into QR codes. It is worth to mention here, that digital signature would provide more than just integrity control solution for the documents. It’ll also allow us to effectively check their authenticity.

Both scripts presented in this article, don’t claim to be ready-made solutions for immediate, practical application. They should be considered merely as an illustration of how to achieve them. Needless to say, that any target solution doesn’t need to be built on the scripting language basis. I use scripting languages ​​because they are best suited for rapid ideas prototyping and because I like them very much. In addition, the application development in scripting languages ​​has also one major advantage – it is fast. For me personally, it is very important, because I don’t have enough time and patience for laborious development. Instead of that, I prefer to spend my time on generating some more new ideas.

Both presented scripts have also educational value because they were just developed with educational applications in mind, as a part of the training materials for workshop focused on developing useful scripting tools, that could facilitate day-to-day security management tasks.

The first script presents, how to generate the QR code based visualization of the file checksums and how to fully automatically create file integrity PDF reports (with embedded QR code graphics). Taking this opportunity, I’d like to also mention, that people interested in non-interactive, automatic creation of PDF documents would be perhaps also very interested in powerful functionality of Prawn library, prepared by developers team to which Gregory Brown, James Healy, Brad Ediger, Daniel Nelson and Jonathan Greenberg belong (thanks guys, it is fantastic job!) I used  this library to process PDF documents, controlled from within the code of my Ruby scripts.

# Author: Janusz Nawrat (2013)
require 'digest/md5'
require 'rqrcode_png'
require 'prawn'
# Don't forget to install the proper gems: gem install <module>
# before you try to execute of this script

# Syntax checking: script should be executed with a list of files
# for which the intergrity report will be generated.
# Globbing is of course allowed. For instance ruby this_script_filename *.rb 
if ARGV.length < 1
         puts "Proper syntax: ruby #{$0} file1 file2 ..."
         exit
end

# Simple function to generate MD5 checksum of the file
def hash_gener (file)
         begin
                   hash = Digest::MD5.hexdigest(File.read(file))
         rescue => e
                   puts "Problem, problem.... See the details:"
                   p e.message
                   p e.backtrace
         end
         return hash
end

# Easy way to generate QR code on the arbitrary data basis,
# saved subsequently to PNG graphic file
def qr_code_generate (text, output_file)
         begin
                   qr = RQRCode::QRCode.new( text, :size =>8, :level => :h )
                   png = qr.to_img                                           
                   png.resize(100, 100).save(output_file)
         rescue => e
                   puts "Problem, problem.... See the details:"
                   p e.message
                   p e.backtrace
         end
end

$hashes = Hash.new
$output_file = ""

ARGV.each do |file|
         begin
                   $hashes[file] = hash_gener(file)
         rescue => e
                   puts "Problem, problem.... See the details:"
                   p e.message
                   p e.backtrace
         end
end

# There is a very easy way to automatically generate PDF file.
# The Prawn library is that one I can recommend you
Prawn::Document.generate("report.pdf") do |pdf|
         pdf.text "File integrity report with QR codes", :align => :center, :size => 18
         pdf.text "Time and date: #{time = Time.new; time.inspect}", :align => :center, :size => 14
         pdf.move_down 12
         $hashes.each do |key,value|
                   printf("%30.30s - %s\n", key, value)
                   tekst = "#{key}: #{value}"
                   $output_file = "#{key}.png"
                   qr_code_generate(tekst,$output_file)
                   pdf.text "File: #{key}; Checksum (MD5): #{value}"
                   pdf.image $output_file
                   File.delete($output_file)
                   pdf.move_down 20

         end
         print "\n\nData was saved to report.pdf file"
end

Executing of this script results in generating a PDF file (report.pdf) and the following messages appearing on the console:

E:\Documents\scripts>ruby pdf_qrcode_checksum_reporter.rb *.rb

             qrcode_to_dbms.rb - a6b940ef212f4dd2814b17c24ed7f015
                regexp_tool.rb - c27b09cf8756128406589217660e123a
     simple_encrypt_with_tk.rb - 2f1fd14eb1cd99f85d4c59632273de9a
                subnet_calc.rb - a2605fdf6b1cc77c0723ce2efe432437
      blowfish_file_encrypt.rb - 84823a50db5ebfc46764dccf161bd689
        encryptor-decryptor.rb - 0a4fc24cff5f792574d32505d5ebf4ca
        gtk_encrypt_decrypt.rb - 60892c7df446ea94d2b59005c3b391a5
      office-doc_qr_tagging.rb - e654037378c27c5c7d9893fffd22df27
pdf_qrcode_checksum_reporter.r - ea9ebc023705a14000be7db4504ffcac
               pdf_workshop.rb - f879b18e8ac42149433c4945c1190a36
               qrcode_batch.rb - 536385388b363bbee0433457671cb7a4
           qrcode_character.rb - f90db5a2dd5a28778a5b901dbc785ef4
           qrcode_generator.rb - 6827e667b0f1656f3516f373225e5bdc

Data was saved to report.pdf file

The PDF report (report.pdf) may look like that:

Files integrity report

The files integrity report with QR codes as a checksum visualization.

The second script shows, how to calculate the MD5 checksum of MS Word document and how to automatically embed its QR Code visualization into the body of this particular document. While discussing this script, I would like to draw your attention this time to the Win32OLE library, that can really come in handy in many other applications, to control from within your Ruby script the operation of various applications and processes running in a Windows environment (for example, to automate document processing).

# Author: Janusz Nawrat (2013)
require 'win32ole'
require 'digest/md5'
require 'rqrcode_png'

# Syntax checking
# You have to cal this script along with the full path of the particular
# MS Word document on the command line
if ARGV.length < 1
         puts "Proper syntax: ruby #{$0} filename_of_document"
         exit
end

# Function to checksum generation for particular file
# You can replace MD5 with any other supported hashing algorithm
# for instance SHA
def hash_gener (file)
         begin
                   hash = Digest::MD5.hexdigest(File.read(file))
         rescue => e
                   puts "Problem, problem.... See the details:"
                   p e.message
                   p e.backtrace
         end
         return hash
end

# Function for QR Code generation in the form of PNG graphic
# You can change both the size of the code itself and the output
# PNG picture resolution to best fit you needs  
def qr_code_generate (text, output_file)
         begin
                   qr = RQRCode::QRCode.new( text, :size =>10, :level => :h )
                   png = qr.to_img                                           
                   png.resize(160, 160).save(output_file)
         rescue => e
                   puts "Problem, problem.... See the details:"
                   p e.message
                   p e.backtrace
         end
end

# Checksum generation for the document (you have to
# provide with the full path to this doc) specified
# on the command line.
# Generation of QR Code on the MD5 checksum basis
# and insertion it at the beginning of the document.
# Then saving this doc and closing the word processor.
begin
         word = WIN32OLE.new('Word.Application')
         document = word.Documents.Open(ARGV[0])
         word.visible = true # You can turn off the visibility of the word processor
         hash_of_file = hash_gener(ARGV[0])
         qrcode_checksum_file = "#{ARGV[0]}.png"
         data_to_qr = "File: #{ARGV[0]}; MD5 Checksum: #{hash_of_file}"
         qr_code_generate(data_to_qr, qrcode_checksum_file)
         document.range(0,0).Text = "\n"
         pic = document.InlineShapes.AddPicture('FileName' => qrcode_checksum_file)
rescue => e
         puts "Problem, problem.... See the details:"
         p e.message
         p e.backtrace
ensure
         word.documents.save()
         word.documents.close()
         word.Quit()
end

This script should be executed with one parameter – the full path to the MS Word document for which you want to generate the checksum, prepare its QR code visualization and finally embed it into the document.

It may ultimately look like that:

Insert QR code into MS Word doc

QR Code as a checksum visualization in the MS Word doc.

This script calculates the document’s MD5 checksum, generates PNG graphic with QR Code visualizing it, then calls the word processor, inserts a graphic into the document, automatically saves and closes both the document and the word processor as well.

Finally, just remember that before you will be able to use any of the libraries declared in the scripts (by ‘require’ command) you need – of course – to install first the appropriate Ruby gems. (gem install module_name).

I encourage you to further develop the ideas presented in this article and – the most important – just have fun!

Informacje Janusz Nawrat
Just ordinary man who likes thinking...

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

TOMASZ WEŁNA

artysta grafik | wykładowca

PRACOWNIA OKO

Szkoła Rysunku Malarstwa i Grafiki DR TOMASZA WEŁNY | KRAKÓW | Plac Matejki 10 | tel 691 81 75 74

Piękno neurobiologii

Blog Jerzego Vetulaniego

Teoria muzyki, zasady muzyki, podstawy muzyki

Teoria muzyki, zasady muzyki, podstawy muzyki - czyli to co każdy amator muzyki wiedzieć powinien :)

Personal Development & Inspirations

Przemyślenia i refleksje, którymi warto się podzielić (blog by Janusz Nawrat)

Business IT Cooperation Platform

Biznes i IT - dwa światy, które muszą współdziałać

%d bloggers like this: