md5.lua 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. ----------------------------------------------------------------------------
  2. -- Modify version from https://github.com/keplerproject/md5
  3. ----------------------------------------------------------------------------
  4. local core = require "md5.core"
  5. ----------------------------------------------------------------------------
  6. -- @param k String with original message.
  7. -- @return String with the md5 hash value converted to hexadecimal digits
  8. function core.sumhexa (k)
  9. k = core.sum(k)
  10. return (string.gsub(k, ".", function (c)
  11. return string.format("%02x", string.byte(c))
  12. end))
  13. end
  14. local function get_ipad(c)
  15. return string.char(c:byte() ~ 0x36)
  16. end
  17. local function get_opad(c)
  18. return string.char(c:byte() ~ 0x5c)
  19. end
  20. function core.hmacmd5(data,key)
  21. if #key>64 then
  22. key=core.sum(key)
  23. key=key:sub(1,16)
  24. end
  25. local ipad_s=key:gsub(".", get_ipad)..string.rep("6",64-#key)
  26. local opad_s=key:gsub(".", get_opad)..string.rep("\\",64-#key)
  27. local istr=core.sum(ipad_s..data)
  28. local ostr=core.sumhexa(opad_s..istr)
  29. return ostr
  30. end
  31. return core