You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.lua 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. --[[
  2. Copyright (c) 2020, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. local ts = require("tableshape").types
  14. local exports = {}
  15. local cr_hash = require 'rspamd_cryptobox_hash'
  16. local blake2b_key = cr_hash.create_specific('blake2'):update('rspamd'):bin()
  17. local function digest_schema()
  18. return {ts.one_of{'hex', 'base32', 'bleach32', 'rbase32', 'base64'}:is_optional(),
  19. ts.one_of{'blake2', 'sha256', 'sha1', 'sha512', 'md5'}:is_optional()}
  20. end
  21. exports.digest_schema = digest_schema
  22. local function create_raw_digest(data, args)
  23. local ht = args[2] or 'blake2'
  24. local h
  25. if ht == 'blake2' then
  26. -- Hack to be compatible with various 'get_digest' methods
  27. h = cr_hash.create_keyed(blake2b_key):update(data)
  28. else
  29. h = cr_hash.create_specific(ht):update(data)
  30. end
  31. return h
  32. end
  33. local function encode_digest(h, args)
  34. local encoding = args[1] or 'hex'
  35. local s
  36. if encoding == 'hex' then
  37. s = h:hex()
  38. elseif encoding == 'base32' then
  39. s = h:base32()
  40. elseif encoding == 'bleach32' then
  41. s = h:base32('bleach')
  42. elseif encoding == 'rbase32' then
  43. s = h:base32('rfc')
  44. elseif encoding == 'base64' then
  45. s = h:base64()
  46. end
  47. return s
  48. end
  49. local function create_digest(data, args)
  50. local h = create_raw_digest(data, args)
  51. return encode_digest(h, args)
  52. end
  53. local function get_cached_or_raw_digest(task, idx, mime_part, args)
  54. if #args == 0 then
  55. -- Optimise as we already have this hash in the API
  56. return mime_part:get_digest()
  57. end
  58. local ht = args[2] or 'blake2'
  59. local cache_key = 'mp_digest_' .. ht .. tostring(idx)
  60. local cached = task:cache_get(cache_key)
  61. if cached then
  62. return encode_digest(cached, args)
  63. end
  64. local h = create_raw_digest(mime_part:get_content('raw_parsed'), args)
  65. task:cache_set(cache_key, h)
  66. return encode_digest(h, args)
  67. end
  68. exports.create_digest = create_digest
  69. exports.create_raw_digest = create_raw_digest
  70. exports.get_cached_or_raw_digest = get_cached_or_raw_digest
  71. exports.encode_digest = encode_digest
  72. return exports