X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fhmac.lua;h=51211c7a2fe52aa23f9d2c4814cb4287c368579f;hb=533fc1fa57ce10fc0821f8edaeb247f2710a543c;hp=b309838050bc1047eecd486789c3329d9a1f5154;hpb=b53216e7117a3c5dbb3920eb185a1e27634bc5f4;p=prosody.git diff --git a/util/hmac.lua b/util/hmac.lua index b3098380..51211c7a 100644 --- a/util/hmac.lua +++ b/util/hmac.lua @@ -1,67 +1,15 @@ -local hashes = require "util.hashes" -local xor = require "bit".bxor - -module "hmac" - -local function arraystr(array) - t = {} - for i = 1,table.getn(array) do - table.insert(t, string.char(array[i])) - end - - return table.concat(t) -end - ---[[ -key - the key to use in the hash -message - the message to hash -hash - the hash function -blocksize - the blocksize for the hash function in bytes -hex - return raw hash or hexadecimal string ---]] -function hmac(key, message, hash, blocksize, hex) - local opad = {} - local ipad = {} - - for i = 1,blocksize do - opad[i] = 0x5c - ipad[i] = 0x36 - end - - if #key > blocksize then - key = hash(key) - end +-- Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- - for i = 1,#key do - ipad[i] = xor(ipad[i],key:sub(i,i):byte()) - opad[i] = xor(opad[i],key:sub(i,i):byte()) - end +-- COMPAT: Only for external pre-0.9 modules - opad = arraystr(opad) - ipad = arraystr(ipad) - - if hex then - return hash(opad..hash(ipad..message), true) - else - return hash(opad..hash(ipad..message)) - end -end - -function md5(key, message, hex) - return hmac(key, message, hashes.md5, 64, hex) -end - -function sha1(key, message, hex) - return hmac(key, message, hashes.sha1, 64, hex) -end - -function sha256(key, message, hex) - return hmac(key, message, hashes.sha256, 64, hex) -end +local hashes = require "util.hashes" -return _M +return { md5 = hashes.hmac_md5, + sha1 = hashes.hmac_sha1, + sha256 = hashes.hmac_sha256 };