forked from tilkinsc/LuaOTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
123 lines (86 loc) · 4.1 KB
/
test.lua
File metadata and controls
123 lines (86 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
----------------------------------------------------------------
-- Initialization Stuff --
----------------------------------------------------------------
local INTERVAL = 30;
local DIGITS = 6;
local BASE32_SECRET = "JBSWY3DPEHPK3PXP";
local DIGEST = "SHA1";
local OTP = require("otp")
local TOTP = require("totp")
local HOTP = require("hotp")
-- Create OTPData struct, which decides the environment
OTP.type = "totp"
local tdata = OTP.new(BASE32_SECRET, DIGITS, DIGEST, 30) -- TODO: needs hmac algo, fix differentiation
OTP.type = "hotp"
local hdata = OTP.new(BASE32_SECRET, DIGITS, DIGEST)
-- Dump data members of struct OTPData tdata
print("\\\\ totp tdata \\\\")
print("tdata.digits: `" .. tdata.digits .. "`")
print("tdata.interval: `" .. tdata.interval .. "`")
-- TODO: print("tdata.bits: `", .. tdata.bits .. "`")
-- TODO: print("tdata.method: `" .. tdata.method .. "`") -- is actually .type
-- TODO: print("tdata.algo: `" .. tdata.algo .. "`")
print("tdata.digest: `" .. tdata.digest .. "`")
print("tdata.secret: `" .. tdata.secret .. "`") -- TODO: base32_secret
print("// totp tdata //\n")
-- Dump data members of struct OTPData hdata
print("\\\\ hotp hdata \\\\")
print("hdata.digits: `" .. hdata.digits .. "`")
-- print("hdata.bits: `" .. hdata.bits .. "`")
-- print("hdata.method: `" .. hdata.method .. "`")
-- print("hdata.algo: `" .. hdata.algo .. "`")
print("hdata.digest: `" .. hdata.digest .. "`")
print("hdata.secret: `" .. hdata.secret .. "`")
print("// hotp hdata //\n")
print("Current Time: `" .. os.time() .. "`")
----------------------------------------------------------------
-- URI Example --
----------------------------------------------------------------
local name1 = "name1"
local name2 = "name2"
local whatever1 = "account@whatever1.com"
local whatever2 = "account@whatever2.com"
local uri1 = OTP.util.build_uri(tdata.secret, name1, nil, whatever1, DIGEST, DIGITS, INTERVAL)
print("TOTP URI 1: `" .. uri1 .. "`\n")
local counter = 52;
local uri2 = OTP.util.build_uri(hdata.secret, name2, counter, whatever2, DIGEST, DIGITS, nil)
print("HOTP URI 2: `" .. uri2 .. "`\n")
----------------------------------------------------------------
-- BASE32 Stuff --
----------------------------------------------------------------
-- seed random generator
math.randomseed(os.time())
math.random(1, 2)
local base32_len = 16
local base32_new_secret = OTP.random_base32(base32_len, OTP.util.default_chars)
print("Generated BASE32 Secret: `" .. base32_new_secret .. "`")
print("") -- line break for readability
----------------------------------------------------------------
-- TOTP Stuff --
----------------------------------------------------------------
-- totp.now
local totp_err_1 = TOTP.now(tdata)
print("TOTP Generated: `" .. totp_err_1 .. "`")
-- totp.at
local totp_err_2 = TOTP.at(tdata, 1, 0)
print("TOTP Generated: `" .. totp_err_1 .. "`")
-- Do a verification for a hardcoded code
-- Won't succeed, this code is for a timeblock far into the past
local tv1 = TOTP.verify(tdata, 576203, os.time(), 4)
-- Will succeed, timeblock 0 for JBSWY3DPEHPK3PXP == 282760
local tv2 = TOTP.verify(tdata, 282760, 0, 4)
print("TOTP Verification 1: `" .. tostring(tv1) .. "`")
print("TOTP Verification 2: `" .. tostring(tv2) .. "`")
print("") -- line break for readability
----------------------------------------------------------------
-- HOTP Stuff --
----------------------------------------------------------------
-- Get HOTP for token 1
-- 1. Generate and load hotp key into buffer
-- 2. Check for error
local hotp_err_1 = HOTP.at(hdata, 1)
print("HOTP Generated at 1: `" .. hotp_err_1 .. "`")
-- Do a verification for a hardcoded code
-- Will succeed, 1 for JBSWY3DPEHPK3PXP == 996554
local hv = HOTP.verify(hdata, 996554, 1)
print("HOTP Verification 1: `" .. tostring(hv) .. "`")