Git Product home page Git Product logo

md5.lua's Introduction

md5.lua Build Status

This pure-Lua module computes md5 in Lua 5.1, Lua 5.2 and LuaJIT, using native bit-manipulation libraries when available, and falling back to table-based manipulation of integers in 5.1.

It implements md5.sum and md5.sumhex like the kepler project md5 package, but it's done completely in Lua, with no dependencies on other libs or C files.

Usage

Simple example:

local md5 = require 'md5'

local md5_as_data  = md5.sum(message)       -- returns raw bytes
local md5_as_hex   = md5.sumhexa(message)   -- returns a hex string
local md5_as_hex2  = md5.tohex(md5_as_data) -- returns the same string as md5_as_hex

Incremental example (for computing md5 of streams, or big files which have to be loaded in chunks - new since 1.1.0):

local m = md5.new()
m:update('some bytes')
m:update('some more bytes')
m:update('etc')
return md5.tohex(m:finish())

Credits

This is a cleanup of an implementation by Adam Baldwin - https://gist.github.com/evilpacket/3647908

Which in turn was a mix of the bitwise lib, http://luaforge.net/projects/bit/ by hanzhao (abrash_han - at - hotmail.com), and http://equi4.com/md5/md5calc.lua, by Equi 4 Software.

Lua 5.2 and LuaJIT compatibility by Positive07

A very important fix and the incremental variant by pgimeno

License

This library, as well as all the previous ones in which is based, is released under the MIT license (See license file for details).

Specs

The specs for this library are implemented with busted. In order to run them, install busted and then:

cd path/to/where/the/spec/folder/is
busted

Install

Either copy the file or using luarocks:

luarocks install --server=http://luarocks.org/manifests/kikito md5

md5.lua's People

Contributors

besnoi avatar extremlapin avatar kikito avatar pablomayobre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

md5.lua's Issues

Use bitwise operators for Lua 5.4

The bit manipulation library has been removed in Lua 5.4, so md5.lua falls back to its custom bit manipulation functions, using tables. It would be better if md5.lua knew how to use the bitwise operators that were added in Lua 5.3.

Incorrect hashes on small strings

I was double checking my indexes, and found a collision. Surpised, I did some more research:

nothanks@DESKTOP-616042V ~/repos/roguecraft-squadron
$ cat test1.txt
"Computer, what happened to the IEE Bishop?"

nothanks@DESKTOP-616042V ~/repos/roguecraft-squadron
$ cat test1.txt  | md5sum.exe
5f0d58eb72664a1e2848b191edfd1bee *-

nothanks@DESKTOP-616042V ~/repos/roguecraft-squadron
$ cat test2.txt
"Computer, what is Miho Takeda's current status?"

nothanks@DESKTOP-616042V ~/repos/roguecraft-squadron
$ cat test2.txt | md5sum.exe
03f3a365b8dfc21ddad179289a0138f6 *-


nothanks@DESKTOP-616042V ~/repos/roguecraft-squadron
$ cat test.lua
md5 = require"src.libs.md5"

print(md5.tohex('"Computer, what happened to the IEE Bishop?"'))
print(md5.tohex('"Computer, what is Miho Takeda\'s current status?"'))


nothanks@DESKTOP-616042V ~/repos/roguecraft-squadron
$ lua test.lua
22436f6d70757465722c207768617420
22436f6d70757465722c207768617420

It seems that I not only found a collision, but also that hashes for smaller strings are not correct.

Am I doing something wrong, or is this a bug?

Add incremental MD5 function (feature request)

The suggestion by @stejacob in #4 (comment) would not have fixed the issue reported there, but it is sound. Usual C implementations use md5_init, md5_update and md5_done (the names may vary), where md5_update can be called as often as necessary, and md5_done does the padding and calculates the final result. With Lua, the API can be something that is used like this:

-- this should print the MD5 of "Hello, World!" which is 65a8e27d8879283831b664bd8b7f0ad4
state = md5.new()
state:update("Hello")
state:update(", World!")
result = state:finish()
print(md5.tohex(result))

The advantage is that when the files are long, it's not necessary to send the whole file in one go: it can be calculated on the fly instead (only the last A,B,C,D and up to 63 bytes of buffer are necessary to be kept between calls, thus saving memory). This would allow calculating the MD5 of files larger than those that fit in memory

An error running at openwrt with lua5.1.5

openwrt version: CHAOS CALMER (15.05.1, r48532)
my router CPU: ar71xx
lua version: Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio (double int32)
when I run, some thing happened, as shown blow.

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio (double int32)
> md5 = require("md5")
> print(md5.sumhexa(""))
./md5.lua:385: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
	[C]: in function 'format'
	./md5.lua:385: in function <./md5.lua:384>
	(tail call): ?
	stdin:1: in main chunk
	[C]: ?
> 

OK, I can bear, then I improve the function md5.tohex().

function md5.tohex(s)                                                               
  -- return format("%08x%08x%08x%08x", str2bei(sub(s, 1, 4)), str2bei(sub(s, 5, 8)), str2bei(sub(s, 9, 12)), str2bei(sub(s, 13, 16)))                                                
  local ret = ""                                                           
  local step = 2                                                 
  for i = 1, 16-step+1, step do                                        
      ret = ret..format("%04x", str2bei(sub(s, i, i+step-1)))          
  end                                                                 
  return ret                                                                                      
end

I kown it is so slow in lua, but it can work corretly.
When I run it at my pc, no issue.

Incorrect md5 value for strings greater than 823 bytes?

@lxss has reported here that md5 is returning the wrong md5 value for long enough strings.

require "md5"
local md5 = require 'md5'

local str = "";
for i=1,823 do
  str = str .. "1";
end
print(md5.sumhexa(str)); --823 correct
str = str .. "1";
print(md5.sumhexa(str)); --824 error

@lxss: How are you calculating the "correct" value? What values are you getting with md5.lua?

does not work with lnum patch

this library does not work with lnum patch. (i'm work with lua 5.1.5 + lnum patch)

lua: /usr/lib/lua/md5.lua:376: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
    [C]: in function 'format'
    /usr/lib/lua/md5.lua:376: in function 'sumhexa'
    /usr/lib/lua/md5.lua:380: in function 'sum'

similar error reported here, http://lua-users.org/lists/lua-l/2012-08/msg00170.html . i tried the workaround, but no luck....

Result is not the same when generating MD5 in another way

Generating using terminal

mac:Desktop mac$ echo test | md5
d8e8fca2dc0f896fd7cb4cb0031ba249

When i try to implement it using this lib:

local md5 = require 'md5'
print(md5.sumhexa("test"))

Result would be: 098f6bcd4621d373cade4e832627b4f6

Am i doing something wrong ?

签名后结果好像有问题哎。。。。

比如data = "cGB2CeC3YmwSWGoVz0kAvQ=="
签名后得到的摘要是:e133402da1436fc446f6209e697ec981
但是我在别的地方验证却是这个啊: 1f854ddeab80f94b928bd4883ff900b2

Hi kikto

I would like to use your script to md5sum a file. However, it doesn't work really well. I read the file into a string, However, even I change the part of file. The value is same and different from the Linux md5sum

README.md

local md5_as_hex = md5.sumhex(message) -- returns a hex string

change to

local md5_as_hex = md5.sumhexa(message) -- returns a hex string

:D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.