Comparison

spec/util_hmac_spec.lua @ 9960:cedc1f646925

util.hmac: Generate test cases from RFC 4231
author Kim Alvefur <zash@zash.se>
date Thu, 11 Apr 2019 00:41:48 +0200
child 9961:d7c99694fc94
comparison
equal deleted inserted replaced
9959:45caa32992b6 9960:cedc1f646925
1 -- Test cases from RFC 4231
2
3 local hmac = require "util.hmac";
4 local hex = require "util.hex";
5
6 describe("Test case 1", function ()
7 local Key = hex.from("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
8 local Data = hex.from("4869205468657265");
9 describe("HMAC-SHA-256", function ()
10 it("works", function()
11 assert.equal("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", hmac.sha256(Key, Data, true))
12 end);
13 end);
14 describe("HMAC-SHA-512", function ()
15 it("works", function()
16 assert.equal("87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854", hmac.sha512(Key, Data, true))
17 end);
18 end);
19 end);
20 describe("Test case 2", function ()
21 local Key = hex.from("4a656665");
22 local Data = hex.from("7768617420646f2079612077616e7420666f72206e6f7468696e673f");
23 describe("HMAC-SHA-256", function ()
24 it("works", function()
25 assert.equal("5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", hmac.sha256(Key, Data, true))
26 end);
27 end);
28 describe("HMAC-SHA-512", function ()
29 it("works", function()
30 assert.equal("164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737", hmac.sha512(Key, Data, true))
31 end);
32 end);
33 end);
34 describe("Test case 3", function ()
35 local Key = hex.from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
36 local Data = hex.from("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
37 describe("HMAC-SHA-256", function ()
38 it("works", function()
39 assert.equal("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", hmac.sha256(Key, Data, true))
40 end);
41 end);
42 describe("HMAC-SHA-512", function ()
43 it("works", function()
44 assert.equal("fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb", hmac.sha512(Key, Data, true))
45 end);
46 end);
47 end);
48 describe("Test case 4", function ()
49 local Key = hex.from("0102030405060708090a0b0c0d0e0f10111213141516171819");
50 local Data = hex.from("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd");
51 describe("HMAC-SHA-256", function ()
52 it("works", function()
53 assert.equal("82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b", hmac.sha256(Key, Data, true))
54 end);
55 end);
56 describe("HMAC-SHA-512", function ()
57 it("works", function()
58 assert.equal("b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd", hmac.sha512(Key, Data, true))
59 end);
60 end);
61 end);
62 describe("Test case 5", function ()
63 local Key = hex.from("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c");
64 local Data = hex.from("546573742057697468205472756e636174696f6e");
65 describe("HMAC-SHA-256", function ()
66 it("works", function()
67 assert.equal("a3b6167473100ee06e0c796c2955552b", hmac.sha256(Key, Data, true):sub(1,128/4))
68 end);
69 end);
70 describe("HMAC-SHA-512", function ()
71 it("works", function()
72 assert.equal("415fad6271580a531d4179bc891d87a6", hmac.sha512(Key, Data, true):sub(1,128/4))
73 end);
74 end);
75 end);
76 describe("Test case 6", function ()
77 local Key = hex.from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
78 local Data = hex.from("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374");
79 describe("HMAC-SHA-256", function ()
80 it("works", function()
81 assert.equal("60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", hmac.sha256(Key, Data, true))
82 end);
83 end);
84 describe("HMAC-SHA-512", function ()
85 it("works", function()
86 assert.equal("80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598", hmac.sha512(Key, Data, true))
87 end);
88 end);
89 end);
90 describe("Test case 7", function ()
91 local Key = hex.from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
92 local Data = hex.from("5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e");
93 describe("HMAC-SHA-256", function ()
94 it("works", function()
95 assert.equal("9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2", hmac.sha256(Key, Data, true))
96 end);
97 end);
98 describe("HMAC-SHA-512", function ()
99 it("works", function()
100 assert.equal("e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58", hmac.sha512(Key, Data, true))
101 end);
102 end);
103 end);