| 1 | <html><body> |
| 2 | <h1>Cryptographic Library for Lua 5.0</h1> |
| 3 | |
| 4 | <p> |
| 5 | This library offers basic cryptographic facilities for Lua 5.0: |
| 6 | a hash (digest) function, an a pair crypt/decrypt. |
| 7 | (<a href=md5.tar.gz>download</a>) |
| 8 | |
| 9 | |
| 10 | <h2>Lua API</h2> |
| 11 | |
| 12 | All functions are registered inside a table <code>md5</code>. |
| 13 | |
| 14 | <ul> |
| 15 | |
| 16 | <li><code>md5.sum (message)</code> |
| 17 | <p> |
| 18 | Computes the MD5 message-digest of the string <code>message</code>. |
| 19 | This function takes as input a message of arbitrary length and content |
| 20 | and returns as output a 128-bit "fingerprint" (or "message digest") |
| 21 | of the input. |
| 22 | The output is formated as a binary string with 16 characters. |
| 23 | It is conjectured that it is computationally infeasible to produce |
| 24 | two messages having the same message digest, or to produce any |
| 25 | message having a given pre-specified target message digest. |
| 26 | (see <a href="ftp://ftp.isi.edu/in-notes/rfc1321.txt"> |
| 27 | RFC 1321</a>) |
| 28 | <p> |
| 29 | |
| 30 | <li><code>md5.sumhexa (message)</code> |
| 31 | <p> |
| 32 | Similar to <code>md5.sum</code>, |
| 33 | but returns its value as a string of 32 hexadecimal digits. |
| 34 | |
| 35 | <li><code>md5.crypt (message, key [,seed])</code> |
| 36 | <p> |
| 37 | Encrypts a string, using MD5 in CFB (Cipher-feedback mode). |
| 38 | <code>message</code> is an arbitrary binary string to be encrypted. |
| 39 | <code>key</code> is an arbitrary binary string to be used as a key. |
| 40 | <code>seed</code> is an arbitrary binary string to be used as a seed; |
| 41 | Returns the cyphertext (as a binary string). |
| 42 | |
| 43 | <p> |
| 44 | If no seed is provided, |
| 45 | the function uses the result of <code>os.time()</code> as a seed. |
| 46 | It is recommended that you use different seeds for each message; |
| 47 | the seed itself is not private, and should contain no private data, |
| 48 | because it goes plain in the beginning of the encrypted message. |
| 49 | |
| 50 | <p> |
| 51 | The length of the cyphertext is the length of the message plus the |
| 52 | length of the seed plus one. |
| 53 | <p> |
| 54 | |
| 55 | <li><code>md5.decrypt (message, key)</code> |
| 56 | <p> |
| 57 | Decrypts a string. |
| 58 | The input <code>message</code> must be the result of a previous call |
| 59 | to <code>crypt</code>. |
| 60 | For any <code>msg</code>, <code>key</code>, |
| 61 | and <code>seed</code>, we have that |
| 62 | <pre> |
| 63 | md5.decrypt(md5.crypt(msg, key, seed), key) == msg |
| 64 | </pre> |
| 65 | <p> |
| 66 | |
| 67 | <li><code>md5.exor (s1, s2)</code> |
| 68 | <p> |
| 69 | Does a bit-a-bit exclusive or of strings <code>s1</code> and <code>s2</code>. |
| 70 | Both strings must have the same length, |
| 71 | which will be also the length of the resulting string. |
| 72 | <p> |
| 73 | |
| 74 | </ul> |
| 75 | |
| 76 | |
| 77 | <h2>C API</h2> |
| 78 | |
| 79 | The following functions are declared in <code>md5.h</code> |
| 80 | <ul> |
| 81 | |
| 82 | <li><code>int luaopen_md5 (lua_State *L);</code> |
| 83 | <p> |
| 84 | Opens the library: |
| 85 | Registers the above Lua functions in the given state. |
| 86 | <p> |
| 87 | |
| 88 | <li><code>void md5 (const char *message, long len, char *output);</code> |
| 89 | <p> |
| 90 | Computes the MD5 message-digest of <code>message</code>. |
| 91 | <code>len</code> is the length of <code>message</code>. |
| 92 | <code>output</code> is a buffer that receives the result; |
| 93 | it must have at least 16 bytes (128 bits). |
| 94 | <p> |
| 95 | |
| 96 | </ul> |
| 97 | |
| 98 | <h2>INSTALL</h2> |
| 99 | |
| 100 | The following instructions assume that your Lua installation has a dynamic |
| 101 | linking facility (function <code>loadlib</code>): |
| 102 | |
| 103 | <ul> |
| 104 | |
| 105 | <li>Build a shared library (<code>.so</code>, <code>.dll</code>, etc.) |
| 106 | with files <code>md5.c</code> and <code>md5lib.c</code>. |
| 107 | (There is a <code>makefile</code> for Linux and similar systems.) |
| 108 | |
| 109 | <li>Put the shared library somewhere, |
| 110 | and edit file <code>md5.lua</code> to point to it. |
| 111 | |
| 112 | <li>Put the file <code>md5.lua</code> in a directory of your |
| 113 | <code>LUA_PATH</code>. |
| 114 | |
| 115 | </ul> |
| 116 | |
| 117 | <p><br><small><em> |
| 118 | $Id: md5.html,v 1.4 2003/04/28 16:40:42 roberto Exp $ |
| 119 | </em></small> |
| 120 | |
| 121 | </body></html> |
| 122 |