LuaCheia Logo  

LuaCheia Reference Manual

md5

Synopsis

cheia.load “md5”
 
hash = md5.digest(data)
 
context = md5.new()
context:update(data)
...
hash = context:digest()
context:reset()

Description

This module provides a Lua interface to RSA's MD5 hash. This is a cryptographic algorithm which calculates a ‘signature’ of a data-stream, similar to a checksum. Unlike a checksum, MD5 is designed to make it very difficult to manufacture a set of data to match a given signature. This means that an MD5 digest can be used to verify that data has not been modified, maliciously or otherwise.

Two interfaces are available:

  • The md5.digest function provides a simple interface which returns the digest of a supplied string.
  • The md5.new function returns an object which will build a cumulative digest over many calls to its update method.

Examples

Using the simple interface:

print(md5.digest(“This is a test”)) » ce114e4501d2f4e2dcea3e17b546f339

Or equivalently, using the context interface:

context = md5.new()
context:update(“This is ”)
context:update(“a test”)
print(context:digest()) » ce114e4501d2f4e2dcea3e17b546f339

How the data is split does not alter the resulting digest, but its order does:

context = md5.new()
context:update(“a test”)
context:update(“This is ”)
print(context:digest()) » 76344f576e34d222fe0a8dad3daecc8f

To compute the digest of a file in the same fashion as the md5sum utility, one might write:

function md5sum(filename)
     local file = io.open(filename)
     if not file then return nil end
 
     -- Read the file in 4KB at a time
     local context = md5.new()
     local data = file:read(4096)
     while data do
          context:update(data)
          data = file:read(4096)
     end
 
     return context:digest()
end

Reference

md5.digest(data, [raw])

Computes the digest of the given string. If raw is true, returns the digest in binary form, otherwise returns a hex encoded version.

Parameters:
     data : string  

The data to be hashed.

     raw : boolean  

If true, the digest will be returned in binary form.

Returns:
     string  

The digest.

md5.new()

Creates a new context for cumulatively calculating a digest.

Returns:
     userdata  

An MD5 context.

The MD5 context

context:update(data)

Adds extra data to the digest. Can be called as many times as required.

Parameters:
     data : string  

Data to be included in the digest.

context:digest([raw])

Returns the digest of the concatentation of all the data added to this context. If raw is true, returns the digest in binary form, otherwise returns a hex encoded version.

Warning: Some extra data is added to the stream before the digest is returned. This means that this method should be called once only, once the stream is complete. It is not possible to obtain intermediate results without affecting the digest value.

Parameters:
     raw : boolean  

If true, the digest will be returned in binary form.

Returns:
     string  

The digest.

context:reset()

Restores the context to its starting state, where no data has been entered.

Issues

The digest method behaviour is unintuitive.

See also

RFC1321: The MD5 Message-Digest Algorithm, http://www.ietf.org/rfc/rfc1321.txt.

Revision history

Added in LuaCheia 5.0.

Credits

Based on the public domain C MD5 library by Colin Plumb.

Lua interface by Luiz Henrique de Figueiredo.

Modifications for LuaCheia by Thatcher Ulrich.

Documentation for LuaCheia by Jamie Webb.

Return to main site

Introduction

 » Writing LuaCheia modules

Module Reference

 » bit
 » cgi
 » fuzzy
 » md5
 » pack
 » rex
 » SDL
 » shelve
 » sqlite

Appendices

 » Module path conventions
 » Module names
 » Application Binary Interface
 » Credits