Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
TOTP2FA
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ed8g20
TOTP2FA
Commits
ae34ef8e
Commit
ae34ef8e
authored
2 years ago
by
ed8g20
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
812f828a
Branches
Answer
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
totp.py
+31
-0
31 additions, 0 deletions
totp.py
with
31 additions
and
0 deletions
totp.py
0 → 100644
+
31
−
0
View file @
ae34ef8e
import
struct
from
sha1
import
hmac_sha1
from
base32
import
base32_decode
def
totp
(
time
,
key
,
step_secs
=
30
,
digits
=
6
):
"""
Time-based One-Time Password (TOTP) implementation based on https://tools.ietf.org/id/draft-mraihi-totp-timebased-06.html
>>>
totp
(
1602659430
,
"
DWRGVKRPQJLNU4GY
"
,
step_secs
=
30
,
digits
=
6
)
(
'
846307
'
,
30
)
>>>
totp
(
1602659435
,
"
DWRGVKRPQJLNU4GY
"
,
step_secs
=
30
,
digits
=
6
)
(
'
846307
'
,
25
)
>>>
totp
(
1602659430
,
"
DWRGVKRPQJLNU4GY
"
,
step_secs
=
30
,
digits
=
4
)
(
'
6307
'
,
30
)
>>>
totp
(
1602659430
,
"
DWRGVKRPQJLNU4GY
"
,
step_secs
=
15
,
digits
=
6
)
(
'
524508
'
,
15
)
"""
hmac
=
hmac_sha1
(
base32_decode
(
key
),
struct
.
pack
(
"
>Q
"
,
time
//
step_secs
))
offset
=
hmac
[
-
1
]
&
0xF
code
=
((
hmac
[
offset
]
&
0x7F
)
<<
24
|
(
hmac
[
offset
+
1
]
&
0xFF
)
<<
16
|
(
hmac
[
offset
+
2
]
&
0xFF
)
<<
8
|
(
hmac
[
offset
+
3
]
&
0xFF
))
code
=
str
(
code
%
10
**
digits
)
return
(
"
0
"
*
(
digits
-
len
(
code
))
+
code
,
step_secs
-
time
%
step_secs
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment