Coverage for gws-app/gws/lib/otp/_test.py: 0%
29 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1import base64
3import gws
4import gws.lib.otp as otp
5import gws.test.util as u
8def test_hotp():
9 # https://www.rfc-editor.org/rfc/rfc4226#appendix-D
11 # The following test data uses the ASCII string
12 # "12345678901234567890" for the secret:
13 #
14 # Table 2 details for each count the truncated values (both in
15 # hexadecimal and decimal) and then the HOTP value.
16 #
17 # Truncated
18 # Count Hexadecimal Decimal HOTP
19 # 0 4c93cf18 1284755224 755224
20 # 1 41397eea 1094287082 287082
21 # 2 82fef30 137359152 359152
22 # 3 66ef7655 1726969429 969429
23 # 4 61c5938a 1640338314 338314
24 # 5 33c083d4 868254676 254676
25 # 6 7256c032 1918287922 287922
26 # 7 4e5b397 82162583 162583
27 # 8 2823443f 673399871 399871
28 # 9 2679dc69 645520489 520489
29 #
30 #
31 #
32 #
33 #
35 secret = '3132333435363738393031323334353637383930'
36 r = [
37 '755224',
38 '287082',
39 '359152',
40 '969429',
41 '338314',
42 '254676',
43 '287922',
44 '162583',
45 '399871',
46 '520489',
47 ]
49 for c in range(10):
50 key = bytes.fromhex(secret)
51 a = otp.new_hotp(key, c)
52 assert a == r.pop(0)
55def test_totp():
56 # https://www.rfc-editor.org/rfc/rfc6238#appendix-B
58 # The test token shared secret uses the ASCII string value
59 # "12345678901234567890". With Time Step X = 30, and the Unix epoch as
60 # the initial value to count time steps, where T0 = 0, the TOTP
61 # algorithm will display the following values for specified modes and
62 # timestamps.
63 #
64 # +-------------+--------------+------------------+----------+--------+
65 # | Time (sec) | UTC Time | Value of T (hex) | TOTP | Mode |
66 # +-------------+--------------+------------------+----------+--------+
67 # | 59 | 1970-01-01 | 0000000000000001 | 94287082 | SHA1 |
68 # | | 00:00:59 | | | |
69 # | 59 | 1970-01-01 | 0000000000000001 | 46119246 | SHA256 |
70 # | | 00:00:59 | | | |
71 # | 59 | 1970-01-01 | 0000000000000001 | 90693936 | SHA512 |
72 # | | 00:00:59 | | | |
73 # | 1111111109 | 2005-03-18 | 00000000023523EC | 07081804 | SHA1 |
74 # | | 01:58:29 | | | |
75 # | 1111111109 | 2005-03-18 | 00000000023523EC | 68084774 | SHA256 |
76 # | | 01:58:29 | | | |
77 # | 1111111109 | 2005-03-18 | 00000000023523EC | 25091201 | SHA512 |
78 # | | 01:58:29 | | | |
79 # | 1111111111 | 2005-03-18 | 00000000023523ED | 14050471 | SHA1 |
80 # | | 01:58:31 | | | |
81 # | 1111111111 | 2005-03-18 | 00000000023523ED | 67062674 | SHA256 |
82 # | | 01:58:31 | | | |
83 # | 1111111111 | 2005-03-18 | 00000000023523ED | 99943326 | SHA512 |
84 # | | 01:58:31 | | | |
85 # | 1234567890 | 2009-02-13 | 000000000273EF07 | 89005924 | SHA1 |
86 # | | 23:31:30 | | | |
87 # | 1234567890 | 2009-02-13 | 000000000273EF07 | 91819424 | SHA256 |
88 # | | 23:31:30 | | | |
89 # | 1234567890 | 2009-02-13 | 000000000273EF07 | 93441116 | SHA512 |
90 # | | 23:31:30 | | | |
91 # | 2000000000 | 2033-05-18 | 0000000003F940AA | 69279037 | SHA1 |
92 # | | 03:33:20 | | | |
93 # | 2000000000 | 2033-05-18 | 0000000003F940AA | 90698825 | SHA256 |
94 # | | 03:33:20 | | | |
95 # | 2000000000 | 2033-05-18 | 0000000003F940AA | 38618901 | SHA512 |
96 # | | 03:33:20 | | | |
97 # | 20000000000 | 2603-10-11 | 0000000027BC86AA | 65353130 | SHA1 |
98 # | | 11:33:20 | | | |
99 # | 20000000000 | 2603-10-11 | 0000000027BC86AA | 77737706 | SHA256 |
100 # | | 11:33:20 | | | |
101 # | 20000000000 | 2603-10-11 | 0000000027BC86AA | 47863826 | SHA512 |
102 # | | 11:33:20 | | | |
103 # +-------------+--------------+------------------+----------+--------+
105 seed = "3132333435363738393031323334353637383930"
106 seed32 = (
107 "3132333435363738393031323334353637383930" +
108 "313233343536373839303132")
109 seed64 = (
110 "3132333435363738393031323334353637383930" +
111 "3132333435363738393031323334353637383930" +
112 "3132333435363738393031323334353637383930" +
113 "31323334")
115 r = [
116 '94287082',
117 '46119246',
118 '90693936',
119 '07081804',
120 '68084774',
121 '25091201',
122 '14050471',
123 '67062674',
124 '99943326',
125 '89005924',
126 '91819424',
127 '93441116',
128 '69279037',
129 '90698825',
130 '38618901',
131 '65353130',
132 '77737706',
133 '47863826',
134 ]
136 for ts in [59, 1111111109, 1111111111, 1234567890, 2000000000, 20000000000]:
137 key = bytes.fromhex(seed)
138 opts = otp.Options(start=0, step=30, length=8, algo='sha1')
139 a = otp.new_totp(key, ts, opts)
140 assert a == r.pop(0)
142 key = bytes.fromhex(seed32)
143 opts = otp.Options(start=0, step=30, length=8, algo='sha256')
144 a = otp.new_totp(key, ts, opts)
145 assert a == r.pop(0)
147 key = bytes.fromhex(seed64)
148 opts = otp.Options(start=0, step=30, length=8, algo='sha512')
149 a = otp.new_totp(key, ts, opts)
150 assert a == r.pop(0)