rspamd/test/lua/unit/rsa.lua

41 lines
1.4 KiB
Lua
Raw Normal View History

-- Test rsa signing
2015-02-28 01:24:13 +01:00
context("RSA signature verification test", function()
2015-02-27 16:55:46 +01:00
local rsa_privkey = require "rspamd_rsa_privkey"
local rsa_pubkey = require "rspamd_rsa_pubkey"
local rsa_signature = require "rspamd_rsa_signature"
local rsa = require "rspamd_rsa"
2018-03-29 16:24:39 +02:00
local hash = require "rspamd_cryptobox_hash"
2015-02-27 16:55:46 +01:00
local pubkey = 'testkey.pub'
local privkey = 'testkey'
local data = 'test.data'
local signature = 'test.sig'
2015-02-28 01:24:13 +01:00
local test_dir = string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1")
local rsa_key, rsa_sig
2018-10-23 18:02:12 +02:00
2015-02-28 01:24:13 +01:00
test("RSA sign", function()
-- Signing test
2018-03-29 16:24:39 +02:00
rsa_key = rsa_privkey.load_file(string.format('%s/%s', test_dir, privkey))
2015-02-28 01:24:13 +01:00
assert_not_nil(rsa_key)
2018-03-29 16:24:39 +02:00
local h = hash.create_specific('sha256')
local d = io.open(string.format('%s/%s', test_dir, data), "rb"):read "*a"
h:update(d)
local sig = rsa.sign_memory(rsa_key, h:bin())
assert_not_nil(sig)
sig:save(string.format('%s/%s', test_dir, signature), true)
2015-02-28 01:24:13 +01:00
end)
2018-10-23 18:02:12 +02:00
2015-02-28 01:24:13 +01:00
test("RSA verify", function()
-- Verifying test
2018-03-29 16:24:39 +02:00
local h = hash.create_specific('sha256')
local d = io.open(string.format('%s/%s', test_dir, data), "rb"):read "*a"
h:update(d)
2015-02-28 01:24:13 +01:00
rsa_key = rsa_pubkey.load(string.format('%s/%s', test_dir, pubkey))
assert_not_nil(rsa_key)
rsa_sig = rsa_signature.load(string.format('%s/%s', test_dir, signature))
assert_not_nil(rsa_sig)
2018-03-29 16:24:39 +02:00
assert_true(rsa.verify_memory(rsa_key, rsa_sig, h:bin()))
2015-02-28 01:24:13 +01:00
end)
2015-02-27 16:55:46 +01:00
end)