MD5
Cryptographic Library for Lua

## Introduction

MD5 offers basic cryptographic facilities for Lua 5.X: a hash (digest) function, a pair crypt/decrypt based on MD5 and CFB, and a pair crypt/decrypt based on DES with 56-bit keys.

MD5 is free software and uses the same license as Lua.

## Building

If you are using Unix MD5 offers a Makefile and a separate configuration file, `config`, which should be edited to suit the particularities of the target platform before running `make`. The file has some definitions like paths to the external libraries, compiler options and the like. One important definition is the Lua version, which is not obtained from the installed software.

If you want to build MD5 on Windows MD5 offers the equivalent `config.win` and `Makefile.win` files to be used with Microsoft Visual Studio 2005.

## Installation

The easiest way to install MD5 is to use LuaRocks:

    luarocks install md5

If you prefer to install the files manually and are using Unix, the build generates two compiled binary files, `core.so` and `des56.so`. The first should be copied to a directory called `md5` in your C path. Just copy the second to a directory in your Lua C path.

Windows users can use the binary version of MD5 available at LuaForge.

In both Unix and Windows systems the file `md5.lua` should be copied to a directory in your Lua path.

## Reference

### Lua API

All MD5-based functions are registered inside a table `md5`.

md5.sum (message)
  Computes the MD5 message-digest of the string `message`. This function takes as input a message of arbitrary length and content and returns as output a 128-bit "fingerprint" (or "message digest") of the input.
  The output is formated as a binary string with 16 characters. It is conjectured that it is computationally infeasible to produce two messages having the same message digest, or to produce any message having a given pre-specified target message digest. (see RFC 1321)
md5.sumhexa (message)
  Similar to `md5.sum`, but returns its value as a string of 32 hexadecimal digits.
md5.crypt (message, key [,seed])
  Encrypts a string, using MD5 in CFB (Cipher-feedback mode). `message` is an arbitrary binary string to be encrypted. `key` is an arbitrary binary string to be used as a key. `seed` is an arbitrary binary string to be used as a seed; Returns the cyphertext (as a binary string).
  If no seed is provided, the function uses the result of `os.time()` as a seed. It is recommended that you use different seeds for each message; the seed itself is not private, and should contain no private data, because it goes plain in the beginning of the encrypted message.
  The length of the cyphertext is the length of the message plus the length of the seed plus one.
  Note that MD5 based encryption should not be considered a strong solution. MD5 collision can cause to messages with different keys and the same seed to encrypt to the same value, which can then be decrypted with either key. The bigger the key the lesser the chance of this happening. More details about the weaknesses involved can be found here.
md5.decrypt (message, key)
  Decrypts a string. The input `message` must be the result of a previous call to `crypt`. For any `msg`, `key`, and `seed`, we have that

      md5.decrypt(md5.crypt(msg, key, seed), key) == msg

md5.exor (s1, s2)
  Does a bit-a-bit exclusive or of strings `s1` and `s2`. Both strings must have the same length, which will be also the length of the resulting string.

The DES-based functions are registered inside a table `des56`.

des56.crypt (message, key)
  Encrypts a string, using DES with a 56-bit key. `message` is an arbitrary binary string to be encrypted. `key` is an 8-byte binary string to be used as a key. Returns the cyphertext (as a binary string).
des56.decrypt (message, key)
  Decrypts a string. The input `message` must be the result of a previous call to `des56.crypt`. For any `msg` and `key`, we have that

      des56.decrypt(des56.crypt(msg, key), key) == msg

### C API

The following functions are declared in `md5.h`

int luaopen_md5_core (lua_State *L)
  Opens the library and registers the "md5" Lua functions in the given state.
void md5 (const char *message, long len, char *output)
  Computes the MD5 message-digest of `message`. `len` is the length of `message`. `output` is a buffer that receives the result; it must have at least 16 bytes (128 bits).

The following function is declared in `ldes56.h`

int luaopen_des56 (lua_State *L)
  Opens the library and registers the "des56" Lua functions in the given state.

Valid XHTML 1.0!

$Id: manual.html,v 1.15 2009/03/03 21:00:56 carregal Exp $