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.

rsa.lua 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- Test rsa signing
  2. context("RSA signature verification test", function()
  3. local rsa_privkey = require "rspamd_rsa_privkey"
  4. local rsa_pubkey = require "rspamd_rsa_pubkey"
  5. local rsa_signature = require "rspamd_rsa_signature"
  6. local rsa = require "rspamd_rsa"
  7. local hash = require "rspamd_cryptobox_hash"
  8. local pubkey = 'testkey.pub'
  9. local privkey = 'testkey'
  10. local data = 'test.data'
  11. local signature = 'test.sig'
  12. local test_dir = string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1")
  13. local rsa_key, rsa_sig
  14. test("RSA sign", function()
  15. -- Signing test
  16. rsa_key = rsa_privkey.load_file(string.format('%s/%s', test_dir, privkey))
  17. assert_not_nil(rsa_key)
  18. local h = hash.create_specific('sha256')
  19. local d = io.open(string.format('%s/%s', test_dir, data), "rb"):read "*a"
  20. h:update(d)
  21. local sig = rsa.sign_memory(rsa_key, h:bin())
  22. assert_not_nil(sig)
  23. sig:save(string.format('%s/%s', test_dir, signature), true)
  24. end)
  25. test("RSA verify", function()
  26. -- Verifying test
  27. local h = hash.create_specific('sha256')
  28. local d = io.open(string.format('%s/%s', test_dir, data), "rb"):read "*a"
  29. h:update(d)
  30. rsa_key = rsa_pubkey.load(string.format('%s/%s', test_dir, pubkey))
  31. assert_not_nil(rsa_key)
  32. rsa_sig = rsa_signature.load(string.format('%s/%s', test_dir, signature))
  33. assert_not_nil(rsa_sig)
  34. assert_true(rsa.verify_memory(rsa_key, rsa_sig, h:bin()))
  35. end)
  36. end)